Generating Transactions with Move Scripts
Overview:
Section titled “Overview:”This section outlines how to create test transactions with Move scripts.
Prerequisites
Section titled “Prerequisites”- Clone the aptos-core repository:
- Navigate to the
aptos-core/ecosystem/indexer-grpc/indexer-transaction-generator
directory.
- Navigate to the
How to Generate Test Transactions using Move Script
Section titled “How to Generate Test Transactions using Move Script”-
Set up move_fixtures folder
Before proceeding, ensure you have the
move_fixtures
folder set up in the appropriate location:-
Location: The
move_fixtures
folder should be created in theaptos-core/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions
directory. This is the folder where Move scripts and their configurations for test transactions will be stored. -
Steps to set up the folder:
- if starting fresh, remove all existing files and projects in the
move_fixtures
folder in the aptos-core repo - Create your own Move projects/scripts in the move_fixtures folder (detailed in the next step)
- if starting fresh, remove all existing files and projects in the
-
-
Create Your Move Project and Write your Move Script
Create your Move project and write a module to output the scenario that you would like to test in your processor. You can refer to an example here.
-
Set Up Test Accounts
- These accounts will be used to deploy your module.
- Set up as many accounts as you need. These accounts will be used to send the scripted transactions. Refer to the guide here to create accounts.
- Update
aptos-core/ecosystem/indexer-grpc/indexer-transaction-generator/imported_transactions/testing_accounts.yaml
with your accounts.
-
Create a Configuration File
Each configuration file defines a sequences of transactions for a test scenario.
-
Create a configuration file in the
move_fixtures
directory. Name the configuration file according to the test scenario it corresponds to. -
This configuration file should contain unique transaction names and details for each transaction. The transactions should be listed in the order they are to be executed. The configuration file should be structured like this:
- output_name: This field specifies the name of the output file where the results of the transaction will be saved.
- script_path: This field holds the path to the Move script file that will be executed as part of the transaction.
- sender_address: : This field contains the address of the account that will send the transaction.
The number of output is totally up to you, but the output name should be unique for each transaction. Add as many transactions as you need to test your processor.
transactions:- output_name: simple_user_script1script_path: simple_user_scriptsender_address: <account_address>- output_name: simple_user_script2script_path: simple_user_script2sender_address: <account_address>
-
-
Generate JSON Files and Rust File
Once the Move files and configuration are set up, run the same command used to import transactions but with extra flag
mode
:- testing-folder is where your Move files are stored.
- output-folder can be set to any folder where you want to store the generated files.
- The
--mode=script
flag specifies that the transaction generator should operate in script mode, meaning it will execute Move scripts and generate corresponding transaction data. By default, the mode is set to import, which fetches transactions from the network.
Terminal window cd ~/aptos-core/ecosystem/indexer-grpc/indexer-transaction-generatorcargo run -- --testing-folder ./imported_transactions --output-folder ../indexer-test-transactions/src/ --scriptThis command will:
- Read the configuration in the
move_fixtures
folder. - Execute the specified Move scripts.
- Output the generated JSON files to the designated folder (
~/aptos-core/ecosystem/indexer-grpc/indexer-test-transactions/src/json_transactions
). - Overwrite
generated_transactions.rs
with the new transaction data based on the generated JSON files. This file contains the transaction constants that can be used in tests.
-
Verification
Verify that the json_transactions folder in the target directory contains the generated JSON files with the specified names from the configuration file, and ensure that generated_transactions.rs has been updated accordingly.
How to Use Test Transactions
Section titled “How to Use Test Transactions”Export the Generated File
Section titled “Export the Generated File”Update the mod.rs
file to include the generated Rust file containing the transaction constants. If mod.rs
doesn’t exist, create one in the target folder:
Export the json_transactions
Folder
Section titled “Export the json_transactions Folder”Since the generated_transactions.rs
relies on the json_transactions
Ensure the json_transactions
folder is properly exported in the library file for your tests have direct access to the transaction data.
Integrate into Test Cases
Section titled “Integrate into Test Cases”If you decided to output the rust file in a different crate, you can update you cargo.toml to import the crate containing the generated file as a dependency. Otherwise, you can simply import the generated file directly in your test file. Example.
Next Steps
Section titled “Next Steps”Once the transaction constants are integrated, you can use them in processor tests to validate functionality. For detailed instructions on writing processor tests, refer to Writing Processor Tests.