How to compile and deploy Move transaction script on Libra local network?
In my previous article, I’ve shown how to test and compile Move scripts or modules. But many people were asking how to deploy them to the network? On the Libra Forum Runtian from Calibra Team has described how to compile and deploy peer to peer transfer script on local testnet. Using his answer, I have prepared detailed instruction how to step by step compile and deploy Move transaction script to the local Libra network.
1. Preparing environment
At the beginning, we have to run our Libra blockchain. First, clone Libra from GitHub:
git clone https://github.com/libra/libra.git && cd libra
Checkout testnet branch:
git checkout testnet
Install dependencies:
./scripts/dev_setup.sh
Run local network of Libra blockchain:
cargo run -p libra_swarm -- -s
Now we can create two accounts to make peer to peer transfer possible — we will use following commands in libra%
CLI
// create first account
account create
//or
a c// create second account
account create
//or
a c//mint libra coins to first account (with "0" index)
account mint 0 1000
//or
a m 0 1000
//check account balance of first account (with "0" index)
query balance 0
//or
q b 0
Remember to change accounts numbers in the next steps!
2. Preparing .mvir file with transaction script
In our example we will use peer_to_peer_transfer.mvir
script from Libra GitHub page - yo can find it in libra/language/stdlib/modules/transaction_scripts
directory.
import 0x0.LibraAccount;
main (payee: address, amount: u64) {
LibraAccount.pay_from_sender(move(payee), move(amount));
return;
}
Now we can open new terminal window and create test
directory, where we copy our file (peer_to_peer_transfer.mvir
)
cd libra
mkdir test
cp ./language/stdlib/transaction_scripts/peer_to_peer_transfer.mvir ./test
3. Compiling transaction script
We will use Move IR compiler to compile our transaction script.
The Move IR compiler compiles modules and scripts written in Move down to their respective bytecode representations
First, we have to build compiler binary:
cargo build --bin compiler
You can see more info about IR compiler commands by running:
./target/debug/compiler -h
You can also go to ./target/debug/
dierctory and run ./compiler
instead of ./target/debug/compiler
from main directory (only remember to change files paths).
Now we can compile our transaction script and write it to file in our test directory by using the following command
compiler -o <output_path> <source_path>
In our case we run:
./target/debug/compiler -o ./test/out.program ./test/peer_to_peer_transfer.mvir
4. Building transaction
We will use transaction_builder
to build our transaction. You can find in libra/language/transaction_builder
directory
In the beginning we will build transaction builder binary:
cd language/transaction_builder/
cargo build --features=’build-binary’
cd ../..
Next we can build transaction as:
transaction_builder <sender> <sequence_number> <program> <output> --args <payee> <amount>
So in our case command can look like following:
<sender> — our first account (#0)
<sequence_number> — sequence number of sender’s account. Our account was newly created so it will be 0. If you want to check your sequence number use in Libra CLI account la
<program> — our out.program
file
<output> — newly created out.txn
file
<payee> — account number we want to transfer coins to, in our case it is second account. Important! remember to add 0x at the beginning
<amount> — value of transaction in the smallest Libra unit (1 000 000 equals 1 Libra)
So in our case command can look like following:
./target/debug/transaction_builder 609abf4e33bd8bbd72e02221b9d01bdbb84aa50cc472bb002c31cde4dcd3d593 0 ./test/out.program out.txn —-args 0x3783851e714802c5d2294a602ae4507be1b22b6b4be1c0d71e9f858e4038db24 1000000
Of course you can also go to ./target/debug/
dierctory and run ./transaction_builder
instead of ./target/debug/transaction_builder
from main directory (only remember to change files paths).
5. Sending transaction
In the end we can send transaction via client CLI, so we have to move to our terminal window with running %libra
. We will load a RawTransaction from file and submit to the network, by:
submitb <sender> <path_to_raw_transaction>
In our case:
submitb 609abf4e33bd8bbd72e02221b9d01bdbb84aa50cc472bb002c31cde4dcd3d593 out.txn
Important! If you didn’t change your branch from master
to testnet
you have to use command with dev in the beginning:
dev submitb 609abf4e33bd8bbd72e02221b9d01bdbb84aa50cc472bb002c31cde4dcd3d593 out.txn
Now we can check balances of our accounts:
query balance 0
query balance 1
If transfer succeeds balances should look like this:
And that’s all! In simmilar way you can compile and deploy more advanced transaction scripts. If you have any question, feel free to leave a comment.