Move Scripts Tutorial
Last updated
Last updated
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
The following example calls functions on the module to confirm the balance of the destination account is less than desired_balance
, and if so, tops it up to desired_balance
.
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.
Make a new directory for your work:
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:
From this same directory, initialize a new Move project:
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:
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
:
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
.
You may execute a script in a more streamlined fashion; instead of running endless move compile
and then endless move run-script --compiled-script-path
separately, you can just do this:
See the 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 instead of the Endless CLI in Endless Developer Discussions.
This will conduct both steps with a single CLI command yet has . For this reason, we recommend using the previous two-step approach for now.