Modules
There are two types of smart contracts in Pontem: modules and scripts.
The difference between them is that a module is published into blockchain storage and is stored under the publisher account, while a script is simply a transaction-as-script and can only operate with existing modules. This means that you and other developers can use your modules in their own scripts or modules.
A Module - like a class in object oriented programming - stores resources, structures, functions and constants. Typically, a module name start with an uppercase letter and uses a camelback naming convention. A module named MyModule should be stored in a source file named MyModule.move.
Each module is stored under the user address and it's own name, e.g. 'address::Math'. The address of a deployer is required during compilation because the Move VM verifies during deployment if a module being compiled is using the deployer's address.
To include a module into your script/module use the following construction:
1
use <address>::<module name>;
Copied!
You can read more about modules in the Diem modules documentation.
You can use the {{sender}} literal that represents the account address from Dove.toml.

Write a module

🧙‍♂️ Install the Dove before continue with this tutorial.
Let's see an example of a small module that will just add two numbers (a and b).
Create a new project using Dove:
1
dove new my_modules --dialect pont --address <address>
2
cd my_modules
Copied!
🧙‍♂️ Don't forget to replace <address> with your SS58 Polkadot address!
In the modules folder create a Math.move file and put the following code inside:
1
address {{sender}} {
2
module Math {
3
// Sum two numbers and return result.
4
public fun add(a: u64, b: u64): u64 {
5
a + b
6
}
7
}
8
}
Copied!
As you can see, the function add accepts two u64 numbers and returns the sum of the numbers provided.
Let's compile this module using the Dove. The compiler requires the sender's address as it's included into the bytecode. This address will then be verified when the module is published.
1
dove build
Copied!
See compiled module ready for deployment:
1
ls -la ./artifacts/modules
Copied!
You will see 0_Math.mv, use this file to deploy your new module.
✈️ See our instruction on how to send a transaction to execute/publish modules using the Polkadot JS UI or CLI.

Module with resource

A resource is the main feature of the Move VM. A resource is a special type in Move VM which has strict rules of usage for safety, and it was created to work with digital assets. Usually a resource is represented as a structure with abilities.
A resource type can only be defined and managed in a single module. This module sets rules for accessing, destroying, transferring and checking the existence of resources defined in it.
This documentation doesn't go in depth into resources, but we can still make a module containing resources for demonstration purposes. You can read more about resources in the Move language documentation and in the Move Book documentation.
This module stores the sum of two numbers into a resource. Create a new module calling Storage.move and put the following code inside:
1
address {{sender}} {
2
module StorageSum {
3
// Include Math module.
4
use {{sender}}::Math;
5
6
// Store U64 number into struct that has key (resource).
7
struct Sum has key {
8
val: u64
9
}
10
11
// The function 'store_sum' to store the sum of a + b into a resource.
12
public fun store_sum(account: &signer, a: u64, b: u64) {
13
let sum = Sum {val: Math::add(a, b)};
14
move_to<Sum>(account, sum);
15
}
16
17
// Get the sum stored under address resource.
18
public fun get_sum(addr: address): u64 acquires Sum {
19
borrow_global<Sum>(addr).val
20
}
21
}
22
}
Copied!
Compile it and deploy it like the previous module.
Congrats! You just published your first modules. In the next part of this documentation we will write scripts and use our modules. If you want more advanced examples of modules, like creating new tokens, see our tutorials.
Last modified 1mo ago