/** * DeFi — Swaps, positions, approvals, funding rates. * * Run: npx tsx examples/06-defi.ts */ import { Spectrum } from '@spectrumnodes/sdk'; const spectrum = new Spectrum({ api: process.env.SPECTRUM_API! }); const WETH = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'; const WALLET = '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'; async function main() { // --- getUniswapV3Quote --- const v3 = await spectrum.defi.getUniswapV3Quote('ethereum', { tokenIn: WETH, tokenOut: USDC, amount: '1000000000000000000', fee: 3000, }); console.log(`V3 (3000 fee): ${v3.amountOut}`); // --- getUniswapV2Quote --- const v2 = await spectrum.defi.getUniswapV2Quote('ethereum', { tokenIn: WETH, tokenOut: USDC, amount: '1000000000000000000', }); console.log(`V2 quote: ${v2.amountOut}`); // --- getUniswapV4Quote --- // NOTE — this endpoint requires live pool key args (`hooks`/`tickSpacing`). const v4 = await spectrum.defi.getUniswapV4Quote('ethereum', { tokenIn: WETH, tokenOut: USDC, amount: '1000000000000000000', tickSpacing: 60, }); console.log(`V4 quote: ${v4.amountOut}`); // --- getJupiterQuote (Solana) --- const jupQuote = await spectrum.defi.getJupiterQuote({ inputMint: 'So11111111111111111111111111111111111111112', // SOL outputMint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC amount: '1000000000', // 1 SOL in lamports slippageBps: 50, }); console.log('Jupiter quote:', jupQuote); // --- getJupiterPrice --- const jupPrice = await spectrum.defi.getJupiterPrice({ inputMint: 'So11111111111111111111111111111111111111112', outputMint: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', }); console.log('Jupiter price:', jupPrice); // --- getPosition --- const position = await spectrum.defi.getPosition('ethereum', 'aave-v3', WALLET); console.log(`Aave V3 position: ${JSON.stringify(position).slice(0, 200)}...`); // --- getCurvePools --- const pools = await spectrum.defi.getCurvePools('ethereum'); console.log(`Curve pools on Ethereum: ${pools.pools.length}`); // --- getCurveLlamaLendMarkets --- const llamaMarkets = await spectrum.defi.getCurveLlamaLendMarkets('ethereum'); console.log(`Llama Lend markets: ${llamaMarkets.markets.length}`); // --- getCurveLlamaLendPosition --- const llamaPos = await spectrum.defi.getCurveLlamaLendPosition('ethereum', WALLET); console.log('Llama Lend position:', llamaPos); // --- getPendleInfo --- const pendle = await spectrum.defi.getPendleInfo('ethereum'); console.log('Pendle markets:', pendle); // --- getSummary --- const summary = await spectrum.defi.getSummary('ethereum', WALLET); console.log(`DeFi summary for wallet: ${JSON.stringify(summary).slice(0, 200)}...`); // --- getApprovals --- const approvals = await spectrum.defi.getApprovals('ethereum', WALLET); console.log(`Token approvals: ${approvals.approvals.length} active`); for (const a of approvals.approvals.slice(0, 3)) { console.log(` ${a.token} -> ${a.spender}: ${a.allowance}`); } // --- getRevokeTransaction --- if (approvals.approvals.length > 0) { const first = approvals.approvals[0]; // `approval.token` / `.spender` may be either a plain address string or // an enriched `{ address, name?, symbol? }` object — normalize to address. const tokenAddr = typeof first.token === 'string' ? first.token : first.token.address; const spenderAddr = typeof first.spender === 'string' ? first.spender : first.spender.address; const revokeTx = await spectrum.defi.getRevokeTransaction('ethereum', WALLET, { token: tokenAddr, spender: spenderAddr, }); console.log(`Revoke tx data: ${JSON.stringify(revokeTx).slice(0, 200)}`); } // --- getAllFundingRates --- // Server returns the aggregate shape `{ hyperliquid: [...], dydx: [...], timestamp }`. // Splat both arrays for a flat list, or iterate per provider as below. const funding = await spectrum.defi.getAllFundingRates(); console.log(`\nHyperliquid funding rates: ${funding.hyperliquid.length}`); for (const f of funding.hyperliquid.slice(0, 3)) { console.log(` ${f.coin}: ${f.fundingRate} (annualized ${f.annualizedRate})`); } console.log(`dYdX funding rates: ${funding.dydx.length}`); for (const f of funding.dydx.slice(0, 3)) { console.log(` ${f.ticker}: ${f.fundingRate} (annualized ${f.annualizedRate})`); } // --- getHyperliquidFunding --- const hlFunding = await spectrum.defi.getHyperliquidFunding({ coin: 'ETH' }); console.log('Hyperliquid ETH funding:', hlFunding); // --- getDydxFunding --- const dydxFunding = await spectrum.defi.getDydxFunding({ ticker: 'ETH-USD' }); console.log('dYdX ETH-USD funding:', dydxFunding); } main().catch(console.error);