lending-apy-fetcher-ts
Version:
TypeScript library for fetching APYs from DeFi lending protocols
142 lines (122 loc) • 4.38 kB
text/typescript
// import { Protocol, ApyData, ValidationError, NetworkError } from '../types';
// import { KaminoConfig, AddressConfig } from '../config';
// import { HttpClient } from '../utils/http';
// interface KaminoLendingReserve {
// reserveId: string;
// address: string;
// symbol: string;
// depositApy: number;
// borrowApy: number;
// totalSupplyUsd: number;
// totalBorrowUsd: number;
// }
// interface KaminoStakingYield {
// token: string;
// apy: number;
// tvl: number;
// type: string;
// }
// interface KaminoLendingResponse {
// reserves: KaminoLendingReserve[];
// }
// interface KaminoStakingResponse {
// yields: KaminoStakingYield[];
// }
// export class KaminoProtocol implements Protocol {
// public readonly name = 'Kamino';
// public readonly network = 'Solana';
// constructor(
// private config: KaminoConfig,
// private addressConfig: AddressConfig,
// private httpClient: HttpClient
// ) {}
// async fetchApys(): Promise<ApyData[]> {
// try {
// // Fetch both lending and staking data concurrently
// const [lendingData, stakingData] = await Promise.all([
// this.fetchLendingApys(),
// this.fetchStakingApys(),
// ]);
// return [...lendingData, ...stakingData];
// } catch (error) {
// if (error instanceof Error) {
// throw new NetworkError(`Failed to fetch Kamino APYs: ${error.message}`, this.name, error);
// }
// throw error;
// }
// }
// private async fetchLendingApys(): Promise<ApyData[]> {
// try {
// const response = await this.httpClient.get<KaminoLendingResponse>(
// this.config.getLendingUrl()
// );
// if (!response.reserves || !Array.isArray(response.reserves)) {
// throw new ValidationError('Invalid lending response format', this.name);
// }
// const apyData: ApyData[] = [];
// for (const reserve of response.reserves) {
// if (!reserve.symbol || typeof reserve.depositApy !== 'number') {
// continue; // Skip invalid reserves
// }
// // Add deposit APY
// if (reserve.depositApy > 0) {
// apyData.push({
// token_symbol: reserve.symbol,
// apy: reserve.depositApy,
// protocol_name: this.name,
// network: this.network,
// additional_info: {
// type: 'lending',
// reserve_id: reserve.reserveId,
// address: reserve.address,
// total_supply_usd: reserve.totalSupplyUsd,
// borrow_apy: reserve.borrowApy,
// total_borrow_usd: reserve.totalBorrowUsd,
// },
// });
// }
// }
// return apyData;
// } catch (error) {
// if (error instanceof ValidationError || error instanceof NetworkError) {
// throw error;
// }
// throw new NetworkError(`Failed to fetch Kamino lending APYs: ${error}`, this.name, error as Error);
// }
// }
// private async fetchStakingApys(): Promise<ApyData[]> {
// try {
// const response = await this.httpClient.get<KaminoStakingResponse>(
// this.config.getStakingUrl()
// );
// if (!response.yields || !Array.isArray(response.yields)) {
// throw new ValidationError('Invalid staking response format', this.name);
// }
// const apyData: ApyData[] = [];
// for (const stakingYield of response.yields) {
// if (!stakingYield.token || typeof stakingYield.apy !== 'number') {
// continue; // Skip invalid yields
// }
// if (stakingYield.apy > 0) {
// apyData.push({
// token_symbol: stakingYield.token,
// apy: stakingYield.apy,
// protocol_name: this.name,
// network: this.network,
// additional_info: {
// type: 'staking',
// yield_type: stakingYield.type,
// tvl: stakingYield.tvl,
// },
// });
// }
// }
// return apyData;
// } catch (error) {
// if (error instanceof ValidationError || error instanceof NetworkError) {
// throw error;
// }
// throw new NetworkError(`Failed to fetch Kamino staking APYs: ${error}`, this.name, error as Error);
// }
// }
// }