lending-apy-fetcher-ts
Version:
TypeScript library for fetching APYs from DeFi lending protocols
74 lines (64 loc) • 1.67 kB
text/typescript
// Core data types for APY fetching
export interface ApyData {
token_symbol: string;
apy: number;
protocol_name: string;
network: string;
additional_info?: Record<string, any>;
}
// Protocol interface that all implementations must follow
export interface Protocol {
name: string;
network: string;
fetchApys(): Promise<ApyData[]>;
}
// Custom error types for different failure modes
export class ApyFetcherError extends Error {
constructor(
message: string,
public readonly protocol?: string,
public readonly cause?: Error
) {
super(message);
this.name = 'ApyFetcherError';
}
}
export class NetworkError extends ApyFetcherError {
constructor(message: string, protocol?: string, cause?: Error) {
super(message, protocol, cause);
this.name = 'NetworkError';
}
}
export class ValidationError extends ApyFetcherError {
constructor(message: string, protocol?: string, cause?: Error) {
super(message, protocol, cause);
this.name = 'ValidationError';
}
}
export class AuthenticationError extends ApyFetcherError {
constructor(message: string, protocol?: string, cause?: Error) {
super(message, protocol, cause);
this.name = 'AuthenticationError';
}
}
// Utility types
export interface TokenMapping {
[address: string]: string;
}
export interface NetworkTokenMappings {
[network: string]: TokenMapping;
}
export interface ApyResult {
success: boolean;
data?: ApyData[];
error?: ApyFetcherError;
protocol: string;
}
export interface GroupedApys {
[tokenSymbol: string]: ApyData[];
}
export interface BestApyResult {
token_symbol: string;
apy: number;
network: string;
}