DeFi Llama
Search…
Functions we've written so you don't have to

Exports Helpers

Exporting empty TVL - if your project has filtered TVL only, here's an easy way to export core TVL as empty.
1
module.exports = {
2
bsc: {
3
tvl: () => ({}),
4
staking
5
}
6
};
Copied!
DefiLlama-Adapters/arcx.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example adapter

General API Calls

Using the retry function can be useful to make graphQL and REST API requests more reliable, by repeating the call a number of times when an unsuccessful response is received.
1
const retry = require('./helper/retry');
2
3
const pools = (
4
await retry(async (bail) => await graphQLClient.request(query))
5
).pools.map((pool) => pool.id);
6
7
const programList = await retry(
8
async (bail) =>
9
await axios.get("https://api.btcpx.io/api/v1/prxy-staking/list/0/10")
10
);
Copied!
DefiLlama-Adapters/wbtc.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example Adapter

Solana Helpers

getTokenBalance is used for getting a solana account's balance of a particular token.
1
const { getTokenBalance } = require("../helper/solana");
Copied!
DefiLlama-Adapters/index.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example Solana Adapter

Transforming Tokens That Aren't On CoinGecko

We value tokens through CoinGecko. If you export token balances for addresses that aren't listed on CoinGecko, you'll need to transform the addresses to something that is on CoinGecko. Usually the best way to do this is through the getChainTransform helper.
1
const { getChainTransform } = require("../helper/portedTokens");
2
3
const chainTransform = await getChainTransform(chain);
4
const transformedAssetId = await chainTransform(assetId);
5
sdk.util.sumSingleBalance(balances, transformedAssetId, balance);
Copied!
DefiLlama-Adapters/index.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example Address Transform
To count the TVL of LP token balances, the positions must be unwrapped into their underlying tokens.
1
const { unwrapUniswapLPs } = require('./helper/unwrapLPs');
2
3
const balances = {};
4
const transform = await transformBscAddress();
5
6
await unwrapUniswapLPs(
7
balances,
8
[
9
{balance: balance1, token: tokenAddress1},
10
{balance: balance2, token: tokenAddress2}
11
],
12
chainBlocks['bsc'],
13
'bsc',
14
transform
15
);
Copied!
DefiLlama-Adapters/index.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example Unwrap Uni V2 Adapter
If you have a list of contracts that hold a range of different assets, it could be easiest to use sumTokensAndLPsSharedOwners. This will check all contracts for all tokens, and try to unwrap all uni v2 LP tokens.
1
const { sumTokensAndLPsSharedOwners } = require("./helper/unwrapLPs");
2
const balances = {};
3
4
await sumTokensAndLPsSharedOwners(
5
balances,
6
[
7
[token1, isLP],
8
[token2, isLP],
9
],
10
[contracts],
11
block,
12
chain,
13
transform
14
);
Copied!
DefiLlama-Adapters/index.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example Sum Tokens and LPs Adapter

Getting Block Heights

For lesser known EVM chains sometimes the block height wont be available in the third parameter passed to the adapter's TVL function. In this case you can use getBlock to fetch the block height.
1
const { getBlock } = require('./helper/getBlock');
2
block = await getBlock(timestamp, chain, chainBlocks);
Copied!
DefiLlama-Adapters/bitBTC.js at main · DefiLlama/DefiLlama-Adapters
GitHub
Example Get Block Adapter