Let's say I have a Move script like this:
script {
use std::signer;
use aptos_framework::aptos_account;
use aptos_framework::aptos_coin;
use aptos_framework::coin;
fun main(src: &signer, dest: address, desired_balance: u64) {
let src_addr = signer::address_of(src);
let balance = coin::balance<aptos_coin::AptosCoin>(src_addr);
if (balance < desired_balance) {
aptos_account::transfer(src, dest, desired_balance - balance);
};
addr::my_module::do_nothing();
}
}
This is calling functions on the aptos_coin.move module, which is deployed on chain. What it does isn't so important for this question, but in short, it checks that the balance of the destination account is less than desired_balance
, and if so, tops it up to desired_balance
.
Notice also how it calls a function in a Move module I've defined:
module addr::my_module {
public entry fun do_nothing() { }
}
Where do I put these files? Do I need a Move.toml
? How do I run my script with the CLI?
Let's run through how to execute a Move script with a step by step example, this should answer all your questions.
Make a new directory to work from:
Setup the Aptos CLI:
The CLI will ask you which network you want to work with (e.g.
devnet
,testnet
,mainnet
). It will also ask you for your private key (which looks like this:0xf1adc8d01c1a890f17efc6b08f92179e6008d43026dd56b71e7b0d9b453536be
), or it can generate a new one for you, as part of setting up your account.From here, initialize a new Move project:
Now you need to make a file for your script. So, take the script you created above, and put it in
sources/
, e.g. like this:In other words,
top_up.move
should contain the script you included in the question.Now do the same thing with the Move module, leaving you with this:
Now you can compile the script:
Note how I use the
--named-addresses
argument. This is necessary because in your code, you refer to this named address calledaddr
. The compiler needs to know what this refers to. Instead of using this CLI argument, you could put something like this in yourMove.toml
:Finally you can run the compiled script:
Note that the path of the compiled script is under
build/my_script/
, notbuild/top_up/
. This is because it uses the name of the project contained inMove.toml
, which ismy_script
from when we ranaptos move init --name my_script
.So to answer one of your questions, yes you need a
Move.toml
, you can't currently just execute a script file on its own with the CLI. The compiler needs this determine what Aptos framework to use for example.See the code used in this answer here: https://github.com/banool/move-examples/tree/main/run_script.
See also how to do this with the Rust SDK instead of the CLI: How do I execute a Move script on Aptos using the Rust SDK?.
P.S. There is a more streamlined way to execute a script. Instead of running
aptos move compile
and thenaptos move run-script --compiled-script-path
separately, you can just do this:This will do both steps with a single CLI command. Note however that there are some major footguns with this approach, see https://github.com/aptos-labs/aptos-core/issues/5733. So I'd recommend using the previous two-step approach for now.