DeFi Llama
Search…
How to write a fetch adapter
This is the simplest type of adapter, it's just an adapter that exports a function called fetch which returns a project's total TVL (in USD) as a number.
The following basic adapter would just always return a TVL of 100$:
1
async function fetch() {
2
return 100;
3
}
4
5
module.exports = {
6
fetch
7
}
Copied!
While most of the old adapters are of this type, now we have migrated to SDK adapters and the only fetch adapters we accept are those for projects that are not on Ethereum nor BSC.

Examples

projects/balancer.js
1
const retry = require('async-retry')
2
const { GraphQLClient, gql } = require('graphql-request')
3
4
async function fetch() {
5
var endpoint ='https://api.thegraph.com/subgraphs/name/balancer-labs/balancer';
6
var graphQLClient = new GraphQLClient(endpoint)
7
8
var query = gql`
9
{
10
balancers(first: 5) {
11
totalLiquidity,
12
totalSwapVolume
13
}
14
}
15
`;
16
const results = await retry(async bail => await graphQLClient.request(query))
17
return parseFloat(results.balancers[0].totalLiquidity);
18
}
19
20
module.exports = {
21
fetch
22
}
Copied!
projects/ren.js
1
const retry = require('async-retry')
2
const axios = require("axios");
3
const BigNumber = require("bignumber.js");
4
5
async function fetch() {
6
let price_feed = await retry(async bail => await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true'))
7
let response = await retry(async bail => await axios.get('https://api.etherscan.io/api?module=stats&action=tokensupply&contractaddress=0xeb4c2781e4eba804ce9a9803c67d0893436bb27d&apikey=H6NGIGG7N74TUH8K2X31J1KB65HFBH2E82'))
8
let tvl = new BigNumber(response.data.result).div(10 ** 8).toFixed(2);
9
return (tvl * price_feed.data.bitcoin.usd);
10
}
11
12
module.exports = {
13
fetch
14
}
Copied!
projects/thorchain.js
1
const retry = require('async-retry')
2
const axios = require("axios");
3
const BigNumber = require("bignumber.js");
4
5
async function fetch() {
6
var price_feed = await retry(async bail => await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=thorchain&vs_currencies=usd&include_market_cap=true&include_24hr_vol=true&include_24hr_change=true'))
7
8
var res = await retry(async bail => await axios.get('https://chaosnet-midgard.bepswap.com/v1/network'))
9
var tvl = await new BigNumber((parseFloat(res.data.totalStaked) * 2) + parseFloat(res.data.bondMetrics.totalActiveBond) + parseFloat(res.data.bondMetrics.totalStandbyBond)).div(10 ** 8).toFixed(2);
10
tvl = tvl * price_feed.data.thorchain.usd;
11
return tvl;
12
}
13
14
module.exports = {
15
fetch
16
}
Copied!
Last modified 6mo ago
Copy link
Contents
Examples