Part 3: Building a Financial Advisor dApp on Avalanche
Let’s recap from the previous post the data we’re trying to collect
- Current Swap Fee distribution for Liquidity Providers
- List of Pangolin Pairs
- Liquidity and Volume of Pangolin Pairs
- PNG Token distribution per Pool
- USD historical price of Pangolin Pairs
We’ve already covered 1 in the manual constants.ts
file in the previous post. If you remember it’s a fixed percent of 0.05%, so currently there’s no need to find the value programmatically.
Let’s now start looking at getting the data from the Graph.
Please note: for the purpose of this post we will only focus on the AVAX — PNG Pair
Before we get into writing any code, let’s use a GUI to query the data we require.
GraphiQL is a web application that allows you to perform queries, subscriptions and mutations against the Graph endpoint.
Let’s now test how we can get the data we want through GraphiQL. Let’s navigate to https://graph-node.avax.network/subgraphs/name/dasconnor/pangolindex
It’s important to note that this GraphQL endpoint is controlled by the following repo: https://github.com/pangolindex/subgraph
Please note: That publicly available URL constantly goes down for me. I’m not sure what’s causing it, but I’m looking into it. The alternative for now is to run your queries directly in Postman
Let’s now look at how we can get the data we require
List of Pangolin Pairs
We can then add the following query
query pairs {
pairs(first: 10, orderBy: trackedReserveETH, orderDirection: desc) {
id
token0 {
name
}
token1 {
name
}
}
}
This query will return the top 10 pairs. You can easily query more, but for the purposes of this exercise, this should be sufficient.
Liquidity and Volume of Pangolin Pairs
What’s important to note about the data that we want to return, is that we want historical data. So as an example, we’d like to get the past week’s worth of figures. Since the Graph uses Timestamp and not Date format we need to get last week’s date in the Timestamp format. We can do this via this website https://timestamp.online/ or else by using the following Javascript code
new Date().getTime()/1000;
query pairs {
pairs(first: 1, orderBy: trackedReserveETH, orderDirection: desc) {
id
token0 {
name
}
token1 {
name
}
reserve0
reserve1
totalSupply
liquidityPositionSnapshots(where: {timestamp_gt: 1614264938}) {
timestamp
token0PriceUSD
token1PriceUSD
liquidityPosition {
id
}
liquidityTokenBalance
liquidityTokenTotalSupply
}
}
}
PNG Token distribution per Pool
Unfortunately this data is not available through the GraphQL endpoint. This means we’re going to need to dig a bit deeper to find this information. We’re going on chain!
If I look through the Interface codebase we can see that there is a Staking Hook which uses ABI’s. This is a good opportunity to talk through ABI’s.
ABI stands for Application Binary Interface. This only makes sense if you understand how smart contract code is deployed. In the C Chain on Avalanche, the chain runs an EVM. An EVM is an Ethereum Virtual Machine and it’s the reason why it’s so easy to port Ethereum applications onto Avalanche. In Ethereum you use Solidity as the programming language to write smart contracts. You then deploy the smart contracts onto the blockchain where they are stored in a Binary format. So, Application Binary Interface, allows us to communicate with these binary representations of the smart contract in a more human readable way.
Make sense? If not, let me know in the comments and I’ll do my best to explain further.
Ok let’s now look at how we can gain access to this information.
I spent a fair bit of time trawling through Github to get this. So the Solidity contract is available through an npm module called @pangolindex/governance however, the code isn’t actually available on Github publicly. So to get the ABI, I needed to install the governance module and then trawl through the node_modules to finally get the ABI.
Within the ABI, we have an a function called “getReward”, we can use this to query the Avalanche Mainnet to get the relevant Rewards.
So now we need to get the illusive Staking Rewards.
Now instead of finding this data, I’m going to propose to add this to the Graph.
So in the next post I’ll detail adding another data point to the Pangolin subgraph and then after that I’ll hopefully be able to carry on building this Financial Advisor.
Take care