-
Originally from Stack Overflow. 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 Notice also how it calls a function in a Move module I defined:
Where do I put these files? Do I need a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
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: mkdir testing
cd testing Setup the Aptos CLI: aptos init The CLI will ask you which network you want to work with (e.g. From here, initialize a new Move project: aptos move init --name my_script Now you need to make a file for your script. So, take the script you created above, and put it in
In other words, Now do the same thing with the Move module, leaving you with this:
First, publish
Now you can compile the script:
Note how I use the
Finally you can run the compiled script:
Note that the path of the compiled script is under So to answer one of your questions, yes you need a 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: https://stackoverflow.com/questions/74452702/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
This will do both steps with a single CLI command. Note however that there are some major footguns with this approach, see aptos-labs/aptos-core#5733. So I'd recommend using the previous two-step approach for now. |
Beta Was this translation helpful? Give feedback.
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:
mkdir testing cd testing
Setup the Aptos CLI:
The CLI will ask you which network you want to work with (e.g.
local
,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…