🖊️Defining Actions in Blockflow Instances
Actions in Blockflow Instances are defined using TypeScript, allowing you to write powerful and expressive code to handle blockchain events and perform desired tasks. Before you start coding your actions, it's essential to understand the following concepts:
Context
Database Interactions
Secrets
Available Libraries
Context
The context
variable is injected into the action function as a parameter, providing you with crucial information about the event that triggered the action. It includes details such as:
Event arguments
Contract address
Transaction details
Block details
The context
object has the following schema:
You can access the event arguments using the context.event
object. For example, if the event has an argument named from
, you can access it as follows:
Database Interactions
Blockflow provides a seamless way to interact with your managed databases within your action code. To learn how to read from and write to your databases, refer to this guide.
Secrets
Secrets allow you to securely store and access sensitive information, such as API keys or private keys, without hardcoding them in your action code. You can access secrets using the secret
variable.
For example, to access a private key stored as a secret:
Secrets can be maintained using Blockflow's secret manager. Check out the secret manager section to know more:
Libraries
Blockflow provides several pre-imported libraries that you can use in your action code:
Ethers.js: A popular Ethereum library for interacting with the Ethereum blockchain and smart contracts
You can check out the library here.
Example usage:
BigNumber.js: A library for handling large numbers and performing arithmetic operations.
You can check out the library here.
Example usage:
PushUtils: A utility library provided by Blockflow for interacting with the Push API.
You can check out the library here.
Example usage:
Writing Your Action Code
Now that you understand the key concepts, you can start writing your action code. Here's a basic template to get you started:
In the handler
function, you can access the context
variable to retrieve event details, interact with databases using the bind
function, and perform any desired actions based on the event.
Remember to handle errors appropriately and return a result if needed.
Deploying Your Action
Once you have written your action code, you can deploy it to your Blockflow Instance by saving the code in the appropriate event file within the Instance's action definition interface.
Blockflow will automatically compile and deploy your action code, making it ready to handle incoming events.
Defining actions in Blockflow Instances using TypeScript provides a flexible and powerful way to automate workflows and respond to blockchain events. By leveraging the context
variable, database interactions, secrets, and available libraries, you can create sophisticated actions that suit your specific needs.