A TypeScript library for interacting with the DeFi Llama API, providing easy access to DeFi protocol and chain TVL (Total Value Locked) data with optional analysis features.
npm install @cfxdevkit/defillama
import { DeFiLlama } from '@cfxdevkit/defillama';
const defiLlama = new DeFiLlama();
// Get TVL data for a specific protocol
const protocolTVL = await defiLlama.getProtocolTVL('uniswap');
// Get historical TVL data for a chain with analysis
const chainTVL = await defiLlama.getHistoricalChainTVL('ethereum', true);
The library includes comprehensive error handling for invalid inputs:
const defiLlama = new DeFiLlama();
try {
// This will throw an error for non-existent protocol
await defiLlama.getProtocolTVL('non-existent-protocol');
} catch (error) {
console.error('Error:', error.message);
// Output: Error: HTTP error! status: 400
}
try {
// This will throw an error for invalid chain
await defiLlama.getHistoricalChainTVL('invalid-chain');
} catch (error) {
console.error('Error:', error.message);
// Output: Error: HTTP error! status: 400
}
const protocols = await defiLlama.getProtocols();
// Returns array of protocols with details like:
// [
// {
// id: '2269',
// name: 'Binance CEX',
// url: 'https://www.binance.com',
// description: 'Binance is a cryptocurrency exchange...',
// chain: 'Multi-Chain',
// tvl: 144318054315.48734,
// chainTvls: {
// Ethereum: 40481829144.37302,
// Bitcoin: 55798161484.458565,
// // ... other chains
// }
// },
// // ... other protocols
// ]
// Get formatted TVL data with analysis
const swappiTVL = await defiLlama.getProtocolTVL('swappi', true);
// Returns detailed analysis:
// {
// protocolInfo: {
// name: 'Swappi',
// currentChainTvls: { Conflux: '$9.71M' }
// // ... other protocol info
// },
// tvlAnalysis: {
// overall: {
// currentTVL: '$9.71M',
// averageTVL: '$14.39M',
// totalChange: '-76.15%'
// },
// yearlyAnalysis: [
// {
// year: 2023,
// average: '$15.94M',
// percentageChange: '+348.77%'
// }
// // ... other years
// ]
// }
// }
const currentTVL = await defiLlama.getCurrentProtocolTVL('abc-pool');
// Returns: 9611050.44
const chains = await defiLlama.getChains();
// Returns array of chains with details like:
// [
// {
// name: 'Harmony',
// chainId: 1666600000,
// tvl: 1790390.10,
// tokenSymbol: 'ONE'
// }
// // ... other chains
// ]
const ethereumTVL = await defiLlama.getHistoricalChainTVL('ethereum', true);
// Returns detailed analysis:
// {
// chainAnalysis: {
// overall: {
// currentTVL: '$56.24B',
// averageTVL: '$29.97B',
// totalChange: '+13918890.65%'
// },
// yearlyAnalysis: [
// {
// year: 2023,
// average: '$20.70B',
// percentageChange: '+429.86%'
// }
// // ... other years
// ]
// }
// }
const protocols = await defiLlama.getProtocols();
// Get raw TVL data
const rawTVL = await defiLlama.getProtocolTVL('protocol-name');
// Get formatted TVL data with analysis
const formattedTVL = await defiLlama.getProtocolTVL('protocol-name', true);
const currentTVL = await defiLlama.getCurrentProtocolTVL('protocol-name');
const chains = await defiLlama.getChains();
// Get raw historical TVL data
const rawChainTVL = await defiLlama.getHistoricalChainTVL('chain-name');
// Get formatted historical TVL data with analysis
const formattedChainTVL = await defiLlama.getHistoricalChainTVL('chain-name', true);
git clone https://github.com/cfxdevkit/defillama.git
cd defillama
npm install
npm run build
- Build the librarynpm run test
- Run testsnpm run lint
- Lint the codenpm run format
- Format the codenpm run docs
- Generate documentationnpm run example
- Run example usage scriptThe library includes example usage in the examples
directory. To run the examples:
npm run example
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/AmazingFeature
)git commit -m 'Add some AmazingFeature'
)git push origin feature/AmazingFeature
)This project is licensed under the MIT License - see the LICENSE file for details.