Scripts
Pontem supports transaction scripting. This means users can compile and execute scripts. The Difference between a modules and a script is that you can't publish a script and use it again in the future. Each script is executed by a new transaction every time. Scripts are programmable transactions, one script can use several modules.
You can read more about scripts in the Diem script documentation.

Write a script

🧙‍♂️ Install Dove before continuing with this tutorial.
Let's write a basic script that accept two arguments: values a and b, and then using math from a module, makes a sum from these two numbers and then fires events. For this example, we are going to use the module we deployed in the previous modules section (Math.move and Storage.move).
🧙‍♂️ A script code block must start with all of its use declarations followed by any constants and in the end, the main function declaration. The main function can have any name (i.e. it could have different names, not only main), is the only function in a script block, can have any number of arguments, and must not return a value.
  1. 1.
    Create a new script file called sum.move in the ./scripts/ folder and put the following code in there:
1
script {
2
use 0x01::Event;
3
use {{sender}}::Math;
4
5
fun sum(account: signer, a: u64, b: u64) {
6
let sum = Math::add(a, b);
7
8
// Create event emitter.
9
let event_handle = Event::new_event_handle(&account);
10
11
// Emit event.
12
Event::emit_event(
13
&mut event_handle,
14
sum
15
);
16
17
// Destroy event emitter.
18
Event::destroy_handle(event_handle);
19
}
20
}
Copied!
The script accepts two arguments in the function "sum" then calculates the sum with the provided arguments and fires the event with this sum. Both arguments are u64 integers.
  1. 1.
    Compile the script using Dove to create a new binary containing both the script and arguments for the script:
1
dove tx 'sum(10, 20)'
Copied!
After compilation, inspect the transaction file:
1
ls -la ./artifacts/transactions
Copied!
There is now a sum.mvt script transaction file you can use to send a new execute transaction to the network.
  1. 1.
    Execute transactions using Polkadot JS
✈️ See our instructions on how to send transactions (execute/publish module) using the Polkadot JS UI or CLI.

Store resource

Now let's store new resources using the Storage module we built in the modules section.
  1. 1.
    Create a new script file store_sum.move in the ./scripts/ folder and put the following code inside:
1
script {
2
use 0x1::Signer;
3
use {{sender}}::StorageSum;
4
5
fun store_sum(account: signer, a: u64, b: u64) {
6
// Store sum.
7
StorageSum::store_sum(&account, a, b);
8
9
// Get sum from resource.
10
let sum = StorageSum::get_sum(Signer::address_of(&account));
11
12
// Throw error if sums don't match.
13
// Error code is 101.
14
assert((a + b) == sum, 101);
15
}
16
}
Copied!
  1. 1.
    Compile the script like in the previous example:
1
dove tx 'store_sum(10, 20)'
Copied!
  1. 1.
    Execute transactions using Polkadot JS
✈️ See our instructions on how to send transactions (execute/publish module) using the Polkadot JS UI or CLI.
After the transaction executes, your sum resource will be stored in Substrate storage, and you can query it any time using the following script:
1
script {
2
use 0x1::Signer;
3
use {{sender}}::StorageSum;
4
5
fun get_sum(account: signer) {
6
// Get sum from resource.
7
let sum = StorageSum::get_sum(Signer::address_of(&account));
8
9
// Do something with sum.
10
...
11
}
12
}
Copied!
Read more about resources in our Move and in the Move Book documentation.
Last modified 1mo ago