Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added defi/apy endpoint #37

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/agents/nova/src/core/resources/defi/defi.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Controller, Get, HttpCode, HttpStatus, Query } from '@nestjs/common';
import { ApiResponse } from '@nestjs/swagger';
import { DefiApyQueryDto } from './dto/defi-apy-query.dto';
import { DefiApyResponse } from './dto/defi-apy-response.dto';
import { DefiService } from './defi.service';

@Controller('defi')
export class DefiController {
constructor(private readonly defiService: DefiService) {}

/**
* Retrieves the APY of the asset address provided
* @param {DefiApyQueryDto} query - query params containing asset address
* @returns DefiApyResponse
*/
@HttpCode(HttpStatus.OK)
@ApiResponse({
status: HttpStatus.OK,
description: 'APY data fetched successfully.',
type: DefiApyResponse,
})
@Get('apy')
getApy(@Query() query: DefiApyQueryDto): Promise<DefiApyResponse> {
return this.defiService.getDefiApy(query);
}
}
59 changes: 59 additions & 0 deletions packages/agents/nova/src/core/resources/defi/defi.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Injectable } from '@nestjs/common';
import { DefiApyQueryDto } from './dto/defi-apy-query.dto';
import { DefiApyResponse } from './dto/defi-apy-response.dto';
import { ethers } from 'ethers';

@Injectable()
export class DefiService {
/**
* This service is called by /defi/apy endpoint
* It retrieves the apy of the asset address provided
* This only fetches apy from aave
* @returns apy string
*/
async getDefiApy(query: DefiApyQueryDto): Promise<DefiApyResponse> {
// TODO: move to env
const rpcUrl = 'https://1rpc.io/sepolia';
const aavePoolContractAddress =
'0x6Ae43d3271ff6888e7Fc43Fd7321a503ff738951';

const provider = new ethers.JsonRpcProvider(rpcUrl);

const contractABI = [
'function getReserveData(address asset) external view returns (tuple((uint256) configuration, uint128 liquidityIndex, uint128 variableBorrowIndex, uint128 currentLiquidityRate, uint128 currentVariableBorrowRate, uint128 currentStableBorrowRate, uint40 lastUpdateTimestamp, address aTokenAddress, address stableDebtTokenAddress, address variableDebtTokenAddress, address interestRateStrategyAddress, uint8 id))',
];

const contract = new ethers.Contract(
aavePoolContractAddress,
contractABI,
provider,
);

try {
const reserveData = await contract.getReserveData(query.assetAddress);

const liquidityRate = BigInt(reserveData.currentLiquidityRate.toString());
const RAY = BigInt('10') ** BigInt('27');
const SECONDS_PER_YEAR = BigInt(31536000);

const depositAPR = liquidityRate / RAY;
const depositAPY =
(1 + Number(depositAPR) / Number(SECONDS_PER_YEAR)) **
Number(SECONDS_PER_YEAR) -
1;

const response: DefiApyResponse = {
message: 'APY data fetched successfully.',
statusCode: 201,
data: {
apy: depositAPY.toString(),
},
};

return response;
} catch (error) {
console.error('Error calling getReserveData:', error);
throw new Error('Unable to retrieve data');
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString, Length } from 'class-validator';

export enum Sort {
ASCENDING = 'asc',
DESCENDING = 'desc',
}

export class DefiApyQueryDto {
@IsString()
@IsNotEmpty()
@Length(42, 42)
@ApiProperty({
description: 'The asset address',
example: '0x0617b72940f105811F251967EE4fdD5E38f159d5',
})
assetAddress: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { IsNotEmpty, IsString } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';
import { NovaResponse } from 'src/common/interfaces/nova-response.interface';

export class DefiApyResponse implements NovaResponse {
@ApiProperty({
description: 'Response message',
example: 'Apy data fetched successfully.',
})
message: string;
@ApiProperty({
description: 'Response status code',
example: 201,
})
statusCode: number;
@ApiProperty({
description: 'Response data',
})
data: ApyResponse;
}

export class ApyResponse {
@IsString()
@IsNotEmpty()
apy: string;
}