Move Scripts Tutorial

Move Scripts

This tutorial explains how to write and execute a Move script. You can use Move scripts to execute a series of commands across published Move module interfaces.

For more information about scripts, see the Move scripts docs

Example use case

The following example calls functions on the endless_coin.movearrow-up-right module to confirm the balance of the destination account is less than desired_balance, and if so, tops it up to desired_balance.

script {
    use std::signer;
    use endless_framework::endless_account;
    use endless_framework::endless_coin;
    use endless_framework::coin;

    fun main(src: &signer, dest: address, desired_balance: u64) {
        let src_addr = signer::address_of(src);

        addr::my_module::do_nothing();

        let balance = coin::balance<endless_coin::EndlessCoin>(src_addr);
        if (balance < desired_balance) {
            endless_account::transfer(src, dest, desired_balance - balance);
        };
    }
}

Execution

Now that you know what you would like to accomplish, you need to determine:

  • Where do I put these files?

  • What do I name them?

  • Do I need a Move.toml?

  • How do I run my script with the CLI?

Let us run through how to execute a Move script with a step-by-step example using the Endless CLI.

  1. Make a new directory for your work:

  2. Set up the Endless CLI and create an account:

    You may reuse an existing private key (which looks like this: 0xbd944102bf5b5dfafa7fe865d8fa719da6a1f0eafa3cd600f93385482d2c37a4), or it can generate a new one for you, as part of setting up your account. Let's say your account looks like the example below:

  3. From this same directory, initialize a new Move project:

  4. Create a my_script.move file containing the example script above in a sources/ subdirectory of your testing/ directory. Also, create a my_module.move file as seen in the example below:

    This results in the following file structure:

  5. Compile the script:

    Note how we use the --named-addresses argument. This is necessary because in the code we refer to this named address called addr. The compiler needs to know what this refers to. Instead of using this CLI argument, you could put something like this in your Move.toml:

  6. Run the compiled script:

Note that the path of the compiled script is under build/run_script/, not build/my_script/. This is because it uses the name of the project contained in Move.toml, which is run_script from when we ran endless move init --name run_script.

See the codearrow-up-right used for this document. The full example explains how to use a Move script that relies on a user-created Move module as well.

See also how to do this with the Rust SDKarrow-up-right instead of the Endless CLI in Endless Developer Discussions.

Last updated