Writing a subgraph to get the friend.tech real-time trading data
TLDR
- Shows how to index the friend.tech trading activities on Coinbase’s L2 Base using Chainstack Subgraphs.
- Demonstrates how to capture key transaction data (e.g., trader, influencer, share price) from the protocol’s main contract and store it as queryable entities.
- Offers a practical example schema for analyzing fees, transaction details, and supply changes in a social DApp.
- Deploys on Chainstack for a robust and scalable subgraph solution powering analytics or trading automation.
Introduction
The decentralized social network friend.tech has gathered substantial attention within the Web3 space lately. It enables the trading of users’ tokenized stocks and is constructed on Coinbase’s L2 network Base. With a surge in queries concerning the accessibility of this application’s data, and considering the forthcoming release of the Base protocol in Chainstack Subgraphs hosting, we have prepared this tutorial. It aims to assist traders and analysts in launching their own subgraph, granting access to internal transactions, and if desired, facilitating the development of trading bots or financial analytics.
Getting started
First, install the Graph CLI with either npm or yarn:
After successful installation, initialize your subgraph project:
You will be prompted the questions that should be answered as follows:
Here we’ve chosen the protocol type, the subgraph name, the chain name, and the address of the related smart contract. The ABI has been automatically fetched from the BaseScan.
After you get a message that the start block couldn’t be found on the block explorer you can write the block number which is before the first transaction of this application. We can find this transaction block following the link in the section More info in the BaseScan called ContractCreator: 0xdd9176ea3e7559d6b68b537ef555d3e89403f742 at txn [0xa7eba644182d78c4568364](https://basescan.org/tx/0xa7eba644182d78c4568364e00b0320a9fde9c1fe779cdbec6941fb7443d14c01)
:
Then choose the name of a smart contract (you can use any) and approve automatic code generation for the events indexing.:
In the end you need to refuse adding more contracts:
Launching the subgraph
Let’s check what we have generated in the friendtech-by-chainstack
directory. Specifically, we’re going to focus on the following files:
subgraph.yaml
schema.graphql
src/friendtech-shares-v-1.ts
In the subgraph.yaml
file, you can find the following code:
You can see that all the parameters that we put in the command line have been used here. But along with them, there are entities
that will be created in the database to store the smart contract events data and eventHandlers
that also can be used in the subgraph code and the paths to the other source code files.
If we open the file called schema.graphql
, we see the actual structure of the tables with data:
And the Trade
table is what we need. It contains the following entries:
trader
— the address of the tradersubject
— the address of the influencerisBuy
— the direction of saleshareAmount
— the number of shares in the tradeethAmount
— the price of the share in the dealsubjectEthAmount
— the fee that is received by the influencerprotocolEthAmount
field is the fee that is received by the protocolsupply
— the current total number of issued tokensblockNumber
,blockTimestamp
, andtransactionHash
are the block data that is not related to the protocol but useful for data analysis
The last file with the source code is src/friendtech-shares-v-1.ts
which contains the straightforward AssemblyScript code transferring the smart contract events data into the tables with the described structure.
Again, the handleTrade
function is what we are interested in. We see that each event emitting leads to the new entity created with the fields that exactly correspond to the table structure without any changes. But if you need any additional operations you can add them in the function.
Finally, to start using the subgraph, run the command:
Then generate the binaries and deploy the subgraph into the hosting using the command from the UI (the detailed process is described here):
After successful deployment, you can run queries via the UI interface (the link can be found in the platform UI) and get, for example, all deals with your account:
And that is how you use subgraphs to index all the share trades of friend.tech platform.
Conclusion
Chainstack Subgraphs is a powerful tool that allows Web3 developers to index and query data from decentralized networks like Ethereum. They allow developers to easily access and use the data stored on these networks in their decentralized applications without building and maintaining their own infrastructure for data indexing and querying. This can save developers significant time and resources and make BUIDL-efficient DApps easier.
The aim of this article was to showcase the scale of these subgraphs by using them to easily fetch, process, and organize data that is as scattered as social DApp trades.