♾️ INFINIT CLI Alpha release is now available!
Guides
Action & Script

Action & Script

Action is a class that represents a specific action that can be executed in the project either on/off chain. Each action has a set of parameters that requires the user to input to execute the action.

  • Actions are designed to be modular and reusable, allowing users to easily pick and choose the actions they need for their project.

Script is a TypeScript file that defines an action the user wants to perform within the project. Leveraging TypeScript ensures 100% type safety and provides flexibility for users to implement various actions confidently.

  • TypeScript-Based: The script file is written in TypeScript, which ensures type safety throughout the script, reducing runtime errors and enhancing code reliability.
  • Flexibility: The script enables users to import and define any actions and parameters from the modules as they want, making it adaptable to a wide range of scenarios.
  • Extensibility: Users can perform the any pre-processing with any library they want within a single script file, allowing them to interact with the system in a manner tailored to their specific needs.

Usage

To use an action, you need to import the action class from the module and define the parameters required for the action through the script file.

You can check the available actions in the API References section or use the following command in the project.

bunx infinit action list

Writing a script

The user can either write a script from scratch by following the Script Structure themselves or use the following command to generate a script file with the necessary boilerplate code.

bunx infinit script generate

Example

This is an example of the script file that is generated from the following command.

bunx infinit script generate setFactoryOwnerAction
src/scripts/set-owner-action.script.ts
import { SetFactoryOwnerAction, type actions } from '@infinit-xyz/uniswap-v3/actions'
import type { z } from 'zod'
 
type Param = z.infer<typeof actions['setFactoryOwnerAction']['paramSchema']>
 
// TODO: Replace with actual params
const params: Param = {
 
  // TODO: 
  "uniswapV3Factory": undefined,
 
  // TODO: 
  "newOwner": undefined
}
 
// TODO: Replace with actual signer id
const signer = {
  "factoryOwner": ""
}
 
export default { params, signer, Action: SetFactoryOwnerAction }

Script Structure

  • The script file must be located in the src/scripts folder of the project and named as the following pattern [script_name].script.ts.
  • The script file must import the action class from the module to define the operation the user wants to execute within the project.
  • The script file must export an object with the following structure:
export default {
  Action: Action; // The action class imported from the corresponding module to be performed.
 
  params: Record<string, any>; // An object containing the parameters required for the action. These parameters are dynamically defined based on the action.
 
  signer: { 
    [string]: string;  // An object containing the account ID that will be used to execute the action. 
  };
}
  • The action exported from the file must be imported from the corresponding protocol_module defined in the infinit.config.yaml file. See Configuration for more details.
  • The signer object must include the account ID that corresponds to the role required for executing the specific action. In some cases, the account used may differ from the deployer account. You can import an external account to perform the transaction using the account import command.

Execute a script

To apply the action defined in the script file, the user can execute the following command.

bunx infinit script execute [script_filename]

Example

bunx infinit script execute set-owner-action.script.ts
 
🏃 Starting Execution...
 
 Configuration and registry loaded.
 Signer validated.
 Action file and chain validated.
 
? Enter password for my-acc
 
 Account my-acc loaded.
 Signer wallets initialized.
 
? Do you want to simulate the transactions to estimate the gas cost? yes
 Simulating _SetFactoryOwnerAction... (1 transactions)
 Simulation Completed.
 Total Transactions: 1
 Gas Used: 25700 gas
 Simulate Gas Price: 1 gwei
 Estimated Cost: 0.0000276676562757 ETH
 
? Confirm execution? yes
 Executing _SetFactoryOwnerAction -  (1/1 sub-actions, 1 transactions).
 Successfully execute SetFactoryOwnerAction-1726496600.script.ts, go to infinit.registry.json to see the contract addesses.

After the script is successfully executed, the script file will be moved to the scripts-history folder and the infinit.registry.json file will be updated with the new contract addresses (if any).