Introduction
The decentralized finance (DeFi) ecosystem has gained significant traction in recent years, offering a range of financial services and opportunities to users around the world. To make informed decisions and navigate the DeFi landscape effectively, it is crucial to have access to accurate and up-to-date data. In this tutorial, we will explore how to retrieve DeFi data using the Chainbase dataset.
We will cover important metrics such as lending pool amounts, buy/sell data for specific addresses, and querying data related to deposits, withdrawals, borrowings, repayments, and liquidations. Additionally, we will learn how to obtain information about the total assets, interest rates, collateralized ratios, and liquidity of various lending pools. So, let's dive in and discover the power of Chainbase for obtaining valuable DeFi insights.
Select Dataset
First of all, go to the Chainbase official website and register for a free account.
Go to our dash board indexer page and click deploy to choose the data set you want to fetch.

We will start learning how to fetch data on the dataset by following the protocols: Rari Fuse, UwU Lend, Aura, and BadgerDAO.
Rari Fuse
- Indexing about address deposits, withdrawals, loans, lends, and liquidation data.
query GetAccountInfo($accountId: ID!) {
account(id: $accountId) {
id
openPositionCount
closedPositionCount
depositCount
deposits {
id
market {
id
name
}
amount
}
withdrawCount
withdraws {
id
market {
id
name
}
amount
}
borrowCount
borrows {
id
market {
id
name
}
amount
}
repayCount
repays {
id
market {
id
name
}
amount
}
liquidateCount
liquidates {
id
liquidatee {
id
}
market {
id
name
}
amount
}
liquidationCount
liquidations {
id
liquidator {
id
}
market {
id
name
}
amount
}
}
}

- Indexing the TVL, liquidationThreshold, totalDepositBalance and borrow rate
query GetLendingPools {
markets (first: 10) {
id
name
inputToken {
id
symbol
}
totalValueLockedUSD
rates {
id
rate
duration
maturityBlock
side
type
}
maximumLTV
liquidationThreshold
totalDepositBalanceUSD
totalBorrowBalanceUSD
}
}

- Query the status of a specific asset in the lending pool, including price, supply, loan-to-value ratio, and more.
query GetAssetStatusInLendingPool($tokenSymbol: String!) {
markets(
where: {inputToken_: {symbol_contains: $tokenSymbol}}
) {
id
name
inputToken {
id
symbol
lastPriceUSD
}
totalValueLockedUSD
rates {
id
rate
duration
maturityBlock
side
type
}
totalDepositBalanceUSD
totalBorrowBalanceUSD
}
}

UwU Lend
- Query market name, activity status, interest rates, total locked value (TVL), token balances, and other information.
query QueryMarket {
markets {
id
name
isActive
totalValueLockedUSD
}
}
Aura & BadgerDao
- Indexing the vaults data.
query GetVaults {
vaults {
id
name
symbol
inputToken {
id
name
symbol
decimals
}
outputToken {
id
name
symbol
decimals
}
rewardTokens {
id
token {
id
name
symbol
decimals
}
type
}
depositLimit
fees {
id
feePercentage
feeType
}
createdTimestamp
createdBlockNumber
totalValueLockedUSD
cumulativeSupplySideRevenueUSD
cumulativeProtocolSideRevenueUSD
cumulativeTotalRevenueUSD
inputTokenBalance
outputTokenSupply
outputTokenPriceUSD
pricePerShare
stakedOutputTokenAmount
rewardTokenEmissionsAmount
rewardTokenEmissionsUSD
_gauge
_lpToken
_balRewards
_active
deposits {
id
hash
logIndex
to
from
blockNumber
timestamp
asset {
id
name
symbol
decimals
}
amount
amountUSD
}
withdraws {
id
hash
logIndex
to
from
blockNumber
timestamp
asset {
id
name
symbol
decimals
}
amount
amountUSD
}
}
}
Data from Aura

Data from BadgerDAO

- Query the withdraw and deposit information from a specific address.
query GetWalletActions($wallet: String) {
withdraws(where: {from: $wallet}) {
amount
amountUSD
blockNumber
from
hash
id
logIndex
timestamp
to
}
deposits(where: {from: $wallet}) {
amount
amountUSD
blockNumber
from
hash
id
logIndex
timestamp
to
}
}
Data from Aura

Data from BadgerDAO

Write script usingĀ Chainbase API
Click on "Generate API" and copy your API for future usage.
In this case, we are using the API generated by the third example from Rari Fuse dataset.
UsingĀ axios
Ā in JavaScript. You need to installĀ axios
Ā usingĀ npm install axios --save
Ā in the terminal first.
const axios = require('axios');
axios.post('https://api.chainbase.online/v1/subgraphs/eth_rari_fuse/1.0.0', {
"operationName": "GetAssetStatusInLendingPool",
"query": "query GetAssetStatusInLendingPool($tokenSymbol: String!) {\n markets(\n where: {inputToken_: {symbol_contains: $tokenSymbol}}\n ) {\n id\n name\n inputToken {\n id\n symbol\n lastPriceUSD\n }\n totalValueLockedUSD\n rates {\n id\n rate\n duration\n maturityBlock\n side\n type\n }\n totalDepositBalanceUSD\n totalBorrowBalanceUSD\n }\n}",
"variables": {
"tokenSymbol": "ETH" // Replace with the desired token symbol
}
}, {
headers: {
'x-api-key': 'your chainbase api'
}
})
.then(response => {
const data = response.data.data;
//console.log(JSON.stringify(data, null, 2));
console.log(data);
})
.catch(error => {
console.error(error);
});
To get data printed, run commandĀ node <filename>.js
Ā in the terminal. ****
{
markets: [
{
id: '0x01e83e0090819905560f43c171439c377c1998b3',
name: 'Half Moon Foundations Ethereum Network Token',
inputToken: [Object],
totalValueLockedUSD: '0',
rates: [Array],
totalDepositBalanceUSD: '0',
totalBorrowBalanceUSD: '0'
},
{
id: '0x04b3fd942217a1ed9d4b0d5fe49800de6c14ab63',
name: 'šæ Money Flavours Lido Staked Ether',
inputToken: [Object],
totalValueLockedUSD: '0',
rates: [Array],
totalDepositBalanceUSD: '0',
totalBorrowBalanceUSD: '0'
},
{
id: '0x071a3433a43524d0af58b49ae1b9b14e1795395d',
name: 'w9317 Ethereum',
inputToken: [Object],
totalValueLockedUSD: '1.495647454219614330197169',
rates: [Array],
totalDepositBalanceUSD: '1.495647454219614330197169',
totalBorrowBalanceUSD: '0'
},
{
id: '0x0b7f8c9b205d3a1a1358baa98e78ac672b34d1b5',
name: "Harvest Farmers' Market FARM_WETH",
inputToken: [Object],
totalValueLockedUSD: '0',
rates: [Array],
totalDepositBalanceUSD: '0',
totalBorrowBalanceUSD: '0'
},
Now you can fetch DeFi data by using our Chainbase dataset API.
About Chainbase
Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.
Want to learn more about Chainbase?
Visit our websiteĀ chainbase.com Sign up for aĀ free account, and Check out ourĀ documentation.
Websiteļ½Blogļ½Twitterļ½Discordļ½Link3