UNPKG

lending-apy-fetcher-ts

Version:

TypeScript library for fetching APYs from DeFi lending protocols

79 lines (65 loc) 2.62 kB
#!/usr/bin/env ts-node import { getApys, getBestApyForToken, ApyFetcher, Protocol } from '../src'; import { AaveEthProtocol } from '../src/protocols/aave-eth'; import { AavePolygonProtocol } from '../src/protocols/aave-polygon'; import { AaveArbitrumProtocol } from '../src/protocols/aave-arbitrum'; const protocols: Protocol[] = [ new AaveEthProtocol(), new AavePolygonProtocol(), new AaveArbitrumProtocol() ]; async function testFullFetcher() { console.log('\nTesting full APY fetcher...'); try { const fetcher = new ApyFetcher(protocols); const apys = await fetcher.fetchApys(); console.log(`Found ${apys.length} APY opportunities`); // Show Aave results const aaveApysArbitrum = apys.filter(apy => apy.protocol_name === 'Aave' && apy.network === 'Arbitrum'); const aaveApysEth = apys.filter(apy => apy.protocol_name === 'Aave' && apy.network === 'Ethereum'); const aaveApysOptimism = apys.filter(apy => apy.protocol_name === 'Aave' && apy.network === 'Optimism'); console.log(`\nAave APYs (${aaveApysArbitrum.length} tokens):`); aaveApysArbitrum.slice(0, 5).forEach(apy => { console.log(` ${apy.token_symbol}: ${(apy.apy * 100).toFixed(2)}% (${apy.network})`); }); console.log(`\nAave APYs (${aaveApysEth.length} tokens):`); aaveApysEth.slice(0, 5).forEach(apy => { console.log(` ${apy.token_symbol}: ${(apy.apy * 100).toFixed(2)}% (${apy.network})`); }); console.log(`\nAave APYs (${aaveApysOptimism.length} tokens):`); aaveApysOptimism.slice(0, 5).forEach(apy => { console.log(` ${apy.token_symbol}: ${(apy.apy * 100).toFixed(2)}% (${apy.network})`); }); aaveApysEth.forEach(apy => { console.log(` ${apy.token_symbol}: ${(apy.apy * 100).toFixed(2)}% (${apy.network})`); }); // best apy for token and network for (const token of ['DAI', 'USDT', 'ETH', 'WETH', 'WBTC', 'USDC', 'rsETH', 'USDS', 'USDe', 'weETH', 'wstETH', 'LINK']) { const bestApyForToken = await getBestApyForToken(protocols, token); console.log(`\nBest APY for ${token}: ${(bestApyForToken?.apy ?? 0 * 100).toFixed(2)}% (${bestApyForToken?.network})`); } } catch (error) { console.error('Full fetcher failed:', error); } } async function main() { await testFullFetcher(); } // Run the example if (require.main === module) { main() .then(() => { console.log('\n✅ Example completed successfully!'); process.exit(0); }) .catch((error) => { console.error('\n💥 Example failed:', error); process.exit(1); }); } export { main };