UNPKG

lending-apy-fetcher-ts

Version:

TypeScript library for fetching APYs from DeFi lending protocols

31 lines (28 loc) 1.17 kB
import { Protocol, ApyData, NetworkError } from '../types'; import { getAaveSupplyRates } from '../utils/aave-helpers'; import { ChainId, UiPoolDataProvider } from '@aave/contract-helpers'; import * as markets from '@bgd-labs/aave-address-book'; import { ethers } from 'ethers'; import { OPTIMISM } from './index'; export class AaveOptimismProtocol implements Protocol { public readonly name = 'Aave'; public readonly network = OPTIMISM; public readonly provider: ethers.providers.JsonRpcProvider; constructor( rpc_url: string = 'https://mainnet.optimism.io', ) { this.provider = new ethers.providers.JsonRpcProvider(rpc_url); } async fetchApys(): Promise<ApyData[]> { try { let contract =new UiPoolDataProvider({ uiPoolDataProviderAddress: markets.AaveV3Optimism.UI_POOL_DATA_PROVIDER, provider: this.provider, chainId: ChainId.optimism, }); return await getAaveSupplyRates(this.network, contract, markets.AaveV3Optimism.POOL_ADDRESSES_PROVIDER); } catch (error) { throw new NetworkError(`Failed to fetch Aave Optimism APYs: ${error}`, this.name, error as Error); } } }