Avocado
In this tutorial, we will show you how to index transactions on the avocado ecosystem. Avocado is the next-generation smart contract wallet, which enables you to perform multi-network transactions, with an in-built account abstraction utility - while you're connected to a single network, Avocado. This completely eliminates the need of switching between different networks.
Project Overview
The purpose of this tutorial is to teach you how you can index transactions broadcasted by avocado by creating the subgraphs with the aid of Blockflow's utilities. A short breakdown on the upcoming steps is -
Creating the project directory of your choice and
cd
into it. Initializing the blockflow project in the directory.Supplying the appropriate schema.
Writing the handler functions and add data into a
utils.ts
file for code refactoring.Test the code and verify the data being populated into your MongoDB.
Setting up the project
Download the Blockflow CLI using the command:npm i -g @blockflow-labs/cli
Create a project directory and
cd
into it to make it your working directory.Use the
blockflow init
command to initialize the project and get the standard Blockflow template.Below are the fields that need to be entered for the project. You can also let the default values for each field by pressing [ENTER]. The last field requires you to select the instance trigger type with 4 choices. You can also add more than one trigger for a project.
Setting up the schema
The below-mentioned schema has field which track broadcaster, network actions, etc. which are relevant for a transaction that has taken place. We also use an action
type for a field which has multiple values because of being a transaction action field.
On filling the studio.schema.ts with the appropriate schema, use the command blockflow typegen
to update the schema.ts file in src/types
. Now, we will move onto writing the handler functions.
Writing the handler function
There's a single event Executed
which we need to track while writing code in the executed.ts
. Use the command blockflow codegen
to generate the template in the executed.ts file in src/handlers
. We first import the schema using the import method:
This imports the avoData schema we created earlier. We also import the getAllTransactionActions function, which will be explained later. Below the template, which was auto-generated by blockflow-cli, we write the handler code logic. First, we bind the database connection and create a unique ID for each transaction that takes place.
Now, we add the getAllTransactionAction function to get the transaction action data depending on each transaction log.
Going onto the src/utils
file, we have an index.ts file which contains data regarding the above function. The file contains the topic0 value for the Transfer
event that occurs. Inside the function logic, it gets the transfer event data my matching the topic0 value with various different logs of the transaction. It then returns the data asscociated with the transfer event in the form of topics. Below is the code for the index.ts:
Now we move onto creating the database table and populating the field values. We get the transactionHash, broadcaster, time from blockflow's util which hold the transaction data.
Testing
We use the below command to test if the handler logic is correct and the data gets stored on our specified collection in Mongo.
blockflow instance-test --startBlock --clean --range 10 --uri
The <block number> can be put of any range, but we can put it to latest
if no opinion provided. The — uri
holds the MongoDB connection URL which is tested locally shall be filled with mongodb://localhost:27017/blockflow_studio
.
To check the complete code for indexing Avocado, check out Blockflow's repository on github.
Last updated