@cfxdevkit/defillama - v0.1.4

@cfxdevkit/defillama

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 version Build Status Coverage Status npm bundle size License: MIT TypeScript Node Version PRs Welcome

  • 🚀 Full TypeScript support with comprehensive type definitions
  • 📊 Access to DeFi Llama's protocol and chain TVL data
  • 📈 Optional data analysis and formatting capabilities
  • 🔍 Detailed error handling and logging
  • 📝 Well-documented API methods
  • ⚡ Modern ES6+ syntax
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);
  • Node.js >= 16.0.0
  • npm
  1. Clone the repository:
git clone https://github.com/cfxdevkit/defillama.git
cd defillama
  1. Install dependencies:
npm install
  • npm run build - Build the library
  • npm run test - Run tests
  • npm run lint - Lint the code
  • npm run format - Format the code
  • npm run docs - Generate documentation
  • npm run example - Run example usage script

The 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

This project is licensed under the MIT License - see the LICENSE file for details.