Simplifies blockchain app development with tools for smart accounts, blockchain interaction, and workflow management.
Actions
Actions are the building blocks of a workflow. They are the steps that are executed by triggers. Each action has a configuration that defines how it should be executed. Here are available actions:
Uniswap Swap Action
The UniSwap swap action is an action that swaps tokens on UniSwap. It can be combined with price-based triggers to build a limit order workflow.
items: Array of objects, each representing a recipient with the address, amount, and asset.
Example
constmultiSenderActionConfig= { items: [ { to: '0xRecipientAddress1', amount: '1000000000000000000', asset: { address: '0x...', decimals: 18 } }, // 1 token in wei
{ to: '0xRecipientAddress2', amount: '2000000', asset: { address: '0x...', decimals: 6 } }, // 2 tokens in smallest unit
],};constmultiSenderAction=newMultiSenderAction(multiSenderActionConfig, commonConfig);
Custom Contract Call Action
The CustomContractCall action allows you to interact with any smart contract by specifying the contract address, ABI, function name, and arguments. This action is useful for executing custom logic or interacting with contracts not directly supported by the SDK.
Configuration for Custom Contract Call Action:
typeActionConfig= { address:Address; functionName:string; abi:any; args:any[]; value?:bigint; // native amount to send with the call};
address: The address of the target contract.
functionName: The name of the function to be called on the contract.
abi: The ABI of the target contract.
args: An array of arguments to be passed to the function.
value: (Optional) The native amount to send with the call, default is 0.
Example usage
Here is an example of a workflow that uses the CustomContractCall action to disperse Ether to multiple recipients:
Creating a custom action allows you to extend the functionality of your workflow beyond predefined actions like MultiSenderAction. You can define custom actions to interact with smart contracts or execute specific logic required for your application.
Steps to Implement a Custom Action
Define the Configuration: Specify the parameters required for your action.
Implement the Action Class: Create a class that implements the CallDataBuilder interface.
Build Call Data: Encode the function calls to interact with the blockchain.
Example SendTokens Action
The following example demonstrates how to create a custom action that sends tokens to a recipient.
import { CallDataBuilder } from'@ditto-network/core';// 1. Define the configuration for the SendTokens action// This type specifies the parameters required to send tokenstypeSendTokensConfig= { to:string; // The recipient's address token:Erc20Token; // The ERC20 token to be sent amount:string; // The amount of tokens to send, specified as a string};// 2. Implement the CallDataBuilder interface for the SendTokens action// This class will build the call data necessary to perform the token transferexportclassSendTokensimplementsCallDataBuilder {// Constructor to initialize the SendTokens class with configuration and common builder optionsconstructor(protectedreadonly config:SendTokensConfig,protectedreadonly commonCallDataBuilderConfig:CommonBuilderOptions ) {}// The build method is required by the CallDataBuilder interface// It constructs the call data for the token transferpublicasyncbuild():Promise<CallDataBuilderReturnData> {// Initialize a Set to hold the call dataconstcallData=newSet<CallData>();// Get the contract interface for the Vault contract from the providerconstvaultInterface=this.commonCallDataBuilderConfig.provider.getContractFactory().getContractInterface(JSON.stringify(VaultABI));// Extract the chain ID from the common builder optionsconst { chainId } =this.commonCallDataBuilderConfig;// Add the call data for withdrawing ERC20 tokenscallData.add({ to:this.commonCallDataBuilderConfig.vaultAddress,// The address of the Vault contract callData:vaultInterface.encodeFunctionData('withdrawERC20', [this.config.token.address,// The address of the ERC20 tokenthis.config.to,// The recipient's addressthis.config.amount,// The amount of tokens to send ]), });// Return the call data and value (value is set to the token amount converted to BigInt)return { callData, value:BigInt(this.config.amount) }; }}// 3. Use the Custom Action in a Workflowconstwf=awaitworkflowFactory.create({ name:'Custom Action Example', triggers: [newInstantTrigger()], actions: [ newSendTokens({ to:'0x',// recipient address token: { address:'0x', decimals:18 },// token address and decimals amount:'1000000000000000000',// 1 token in wei }, commonConfig) ], chainId,});consttx=awaitwf.buildAndDeploy(swAddress, account asAddress);
Implementing custom actions allows you to tailor workflows to your specific requirements, making it possible to automate a wide range of blockchain interactions. By following the steps to define the configuration, implement the action class, and build call data, you can extend the capabilities of your workflows beyond predefined actions.
Triggers
Triggers define the conditions under which actions are executed. Here are the available triggers:
Instant Trigger
The Instant Trigger executes an action immediately without any conditions.
Example
constinstantTrigger=newInstantTrigger();
Price-Based Trigger
A price-based trigger executes an action when the price of a specified asset meets certain conditions.
In this example, the trigger is set to execute an action when the price of the specified token is greater than 2 tokens.
Time-Based Trigger
A time-based trigger executes an action at specified intervals.
Configuration for Time-Based Trigger:
typeTimeTriggerConfig= { startAtTimestamp:number; // Unix timestamp when the trigger should start repeatTimes?:number; // Number of times the trigger should repeat cycle: { frequency:number; scale:TimeScale }; providerStrategy:| { type:'nodejs'; rpcUrl:string; chainId:number }| { type:'browser'; provider:ethers.providers.ExternalProvider };};
startAtTimestamp: Unix timestamp when the trigger should start.
repeatTimes: Optional number of times the trigger should repeat.
cycle: Object defining the frequency and scale (e.g., minutes, hours, days) of the trigger.
ProviderStrategy: Configuration for the provider used to manage the time-based execution.