/** * Yields — DeFi lending, vaults, staking rates. * * Run: npx tsx examples/04-yields.ts */ import { Spectrum } from '@spectrumnodes/sdk'; const spectrum = new Spectrum({ api: process.env.SPECTRUM_API! }); async function main() { // --- getLending --- const lending = await spectrum.yields.getLending({ chain: 'ethereum', protocol: 'aave', pool: 'USDC', }); console.log('Aave USDC lending rates:'); for (const pool of lending.results) { console.log(` ${pool.chain} — APY: ${pool.apy?.totalApy}%`); } // --- getVaults --- const vaults = await spectrum.yields.getVaults({ chain: 'polygon', protocol: 'beefy', pool: 'USDC', }); console.log(`\nBeefy vaults on Polygon: ${vaults.results.length} found`); for (const v of vaults.results.slice(0, 3)) { console.log(` ${v.pool}: ${v.apy?.totalApy}% APY`); } // --- getStaking --- // Returns staking-type yields ranked by APY. `token` filters by underlying // token symbol (e.g. 'ETH', 'SOL'); `chain` filters client-side after fetch. const staking = await spectrum.yields.getStaking({ token: 'ETH' }); console.log('\nETH staking yields:'); for (const s of staking.results) { console.log(` ${s.protocol} ${s.pool} on ${s.chain}: ${s.apy.totalApy}% APY`); } // --- getBest --- const best = await spectrum.yields.getBest({ token: 'USDC', limit: 5 }); console.log('\nTop 5 USDC yields:'); for (const b of best.results) { console.log(` #${b.rank} ${b.protocol} (${b.chain}): ${b.apy.totalApy}% APY`); } // --- getBalance (yield-bearing token balance) --- const yieldBal = await spectrum.yields.getBalance({ chain: 'ethereum', protocol: 'aave', pool: 'USDC', address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', }); console.log(`\nAave USDC position: ${yieldBal.results.length} entries`); // --- getPrice (yield-bearing token price) --- const yieldPrice = await spectrum.yields.getPrice({ chain: 'ethereum', protocol: 'aave', pool: 'USDC', }); console.log(`Aave USDC price data: ${yieldPrice.results.length} entries`); // --- getPendleImpliedApy --- const pendle = await spectrum.yields.getPendleImpliedApy({ chain: 'ethereum', pool: 'stETH', address: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045', }); console.log(`Pendle implied APY: ${pendle.results.length} entries`); } main().catch(console.error);