Back to Aptos Core

Transaction Code Generator

ecosystem/indexer-grpc/indexer-test-transactions/README.md

latest5.5 KB
Original Source

Transaction Code Generator

This dynamically generates code for constants that represent transactions stored as JSON files. It builds a set of Rust constants that point to these JSON files and optionally creates a function to retrieve the name of a transaction based on its constant value.

Features

  • Transaction Constants: Automatically generates pub const declarations for each JSON file found in the specified directories.
  • Modular Support: The code generation supports different transaction directories, such as imported_mainnet_txns, imported_testnet_txns, and scripted_transactions.
  • Name Function Generation (Optional): For certain directories, the project can also generate a function that maps constant data to transaction names.

Directories for Transactions

The JSON files must be organized into specific directories within the json_transactions folder. The following directories are supported by default:

  • imported_mainnet_txns: Holds mainnet transaction JSON files.
  • imported_testnet_txns: Holds testnet transaction JSON files.
  • scripted_transactions: Holds scripted transaction JSON files and has a corresponding name lookup function.

How It Works

Code Generation

The main purpose of this project is to automatically generate Rust code at build time. The generated code includes constants for each transaction JSON file and a function to retrieve transaction names, if applicable. The code is generated by the TransactionCodeBuilder struct and written to the OUT_DIR environment directory at compile time.

The steps include:

  1. Scanning Directories: The project scans the directories for .json files.
  2. Constant Generation: It creates a Rust constant for each JSON file, allowing them to be easily referenced in your code.
  3. Name Function (Optional): For certain directories (e.g., scripted_transactions), a function get_transaction_name is generated to map the constant data back to the transaction name.

Example generate_transaction.rs Output


                pub const IMPORTED_MAINNET_TXNS_308783012_FA_TRANSFER: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_mainnet_txns/308783012_fa_transfer.json"));
                pub const ALL_IMPORTED_MAINNET_TXNS: &[&[u8]] = &[IMPORTED_MAINNET_TXNS_308783012_FA_TRANSFER,];

                pub const IMPORTED_TESTNET_TXNS_5979639459_COIN_REGISTER: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_testnet_txns/5979639459_coin_register.json"));
                
                pub const IMPORTED_TESTNET_TXNS_1255836496_V2_FA_METADATA_: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_testnet_txns/1255836496_v2_fa_metadata_.json"));
                
                pub const IMPORTED_TESTNET_TXNS_5992795934_FA_ACTIVITIES: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_testnet_txns/5992795934_fa_activities.json"));
                
                pub const IMPORTED_TESTNET_TXNS_278556781_V1_COIN_REGISTER_FA_METADATA: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_testnet_txns/278556781_v1_coin_register_fa_metadata.json"));
                
                pub const IMPORTED_TESTNET_TXNS_5523474016_VALIDATOR_TXN: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_testnet_txns/5523474016_validator_txn.json"));
                
                pub const IMPORTED_TESTNET_TXNS_1_GENESIS: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/imported_testnet_txns/1_genesis.json"));
                pub const ALL_IMPORTED_TESTNET_TXNS: &[&[u8]] = &[IMPORTED_TESTNET_TXNS_5979639459_COIN_REGISTER,IMPORTED_TESTNET_TXNS_1255836496_V2_FA_METADATA_,IMPORTED_TESTNET_TXNS_5992795934_FA_ACTIVITIES,IMPORTED_TESTNET_TXNS_278556781_V1_COIN_REGISTER_FA_METADATA,IMPORTED_TESTNET_TXNS_5523474016_VALIDATOR_TXN,IMPORTED_TESTNET_TXNS_1_GENESIS,];

                pub const SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT4: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/scripted_transactions/simple_user_script4.json"));
                
                pub const SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT2: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/scripted_transactions/simple_user_script2.json"));
                
                pub const SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT3: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/scripted_transactions/simple_user_script3.json"));
                
                pub const SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT1: &[u8] = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/json_transactions/scripted_transactions/simple_user_script1.json"));
                pub const ALL_SCRIPTED_TRANSACTIONS: &[&[u8]] = &[SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT4,SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT2,SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT3,SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT1,];

        pub fn get_transaction_name(const_data: &[u8]) -> Option<&'static str> {
            match const_data {
                SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT4 => Some("simple_user_script4"),
        SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT2 => Some("simple_user_script2"),
        SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT3 => Some("simple_user_script3"),
        SCRIPTED_TRANSACTIONS_SIMPLE_USER_SCRIPT1 => Some("simple_user_script1"),

                _ => None,
            }
        }