lending-apy-fetcher-ts
Version:
TypeScript library for fetching APYs from DeFi lending protocols
31 lines (28 loc) • 1.17 kB
text/typescript
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 { POLYGON } from './index';
export class AavePolygonProtocol implements Protocol {
public readonly name = 'Aave';
public readonly network = POLYGON;
public readonly provider: ethers.providers.JsonRpcProvider;
constructor(
rpc_url: string = 'https://polygon-rpc.com',
) {
this.provider = new ethers.providers.JsonRpcProvider(rpc_url);
}
async fetchApys(): Promise<ApyData[]> {
try {
let contract =new UiPoolDataProvider({
uiPoolDataProviderAddress: markets.AaveV3Polygon.UI_POOL_DATA_PROVIDER,
provider: this.provider,
chainId: ChainId.polygon,
});
return await getAaveSupplyRates(this.network, contract, markets.AaveV3Polygon.POOL_ADDRESSES_PROVIDER);
} catch (error) {
throw new NetworkError(`Failed to fetch Aave Polygon APYs: ${error}`, this.name, error as Error);
}
}
}