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

aavev2 apr and apy #73

Merged
merged 5 commits into from
Oct 10, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ export class AaveV2ATokenPoolAdapter extends AaveV2BasePoolAdapter {
): string {
return reserveTokenAddresses.aTokenAddress
}

protected getReserveTokenRate(
reserveData: Awaited<ReturnType<ProtocolDataProvider['getReserveData']>>,
): bigint {
return reserveData.liquidityRate
}
}
91 changes: 80 additions & 11 deletions src/adapters/aave-v2/products/pool/aaveV2BasePoolAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SimplePoolAdapter } from '../../../../core/adapters/SimplePoolAdapter'
import { Chain } from '../../../../core/constants/chains'
import { SECONDS_PER_YEAR } from '../../../../core/constants/SECONDS_PER_YEAR'
import { IMetadataBuilder } from '../../../../core/decorators/cacheToFile'
import { aprToApy } from '../../../../core/utils/aprToApy'
import { getTokenMetadata } from '../../../../core/utils/getTokenMetadata'
import { logger } from '../../../../core/utils/logger'
import {
Expand Down Expand Up @@ -31,6 +33,15 @@ type AaveV2PoolMetadata = Record<
}
>

const protocolDataProviderContractAddresses: Partial<Record<Chain, string>> = {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same contracts used for metadata, so I've just moved them as a constant for the whole file.

[Chain.Ethereum]: '0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d',
[Chain.Polygon]: '0x7551b5D2763519d4e37e8B81929D336De671d46d',
[Chain.Avalanche]: '0x65285E9dfab318f57051ab2b139ccCf232945451',
}

// A RAY unit represents 27 decimal places
const RAY = 10 ** 27
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to their glossary, A ray is just a unit with 27 decimals of precision.

https://docs.aave.com/developers/v/2.0/glossary


// Aave tokens always pegged one to one to underlying
const PRICE_PEGGED_TO_ONE = 1
export abstract class AaveV2BasePoolAdapter
Expand All @@ -55,13 +66,38 @@ export abstract class AaveV2BasePoolAdapter
throw new Error('Not Implemented')
}

async getApy(_input: GetApyInput): Promise<ProtocolTokenApy> {
throw new Error('Not Implemented')
async getApy({
protocolTokenAddress,
blockNumber,
}: GetApyInput): Promise<ProtocolTokenApy> {
const apr = await this.getProtocolTokenApr({
protocolTokenAddress,
blockNumber,
})

const apy = aprToApy(apr, SECONDS_PER_YEAR)

return {
...(await this.fetchProtocolTokenMetadata(protocolTokenAddress)),
apyDecimal: apy * 100,
}
}

async getApr(_input: GetAprInput): Promise<ProtocolTokenApr> {
throw new Error('Not Implemented')
async getApr({
protocolTokenAddress,
blockNumber,
}: GetAprInput): Promise<ProtocolTokenApr> {
const apr = await this.getProtocolTokenApr({
protocolTokenAddress,
blockNumber,
})

return {
...(await this.fetchProtocolTokenMetadata(protocolTokenAddress)),
aprDecimal: apr * 100,
}
}

async getRewardApy(_input: GetApyInput): Promise<ProtocolTokenApy> {
throw new Error('Not Implemented')
}
Expand All @@ -71,14 +107,8 @@ export abstract class AaveV2BasePoolAdapter
}

async buildMetadata() {
const contractAddresses: Partial<Record<Chain, string>> = {
[Chain.Ethereum]: '0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d',
[Chain.Polygon]: '0x7551b5D2763519d4e37e8B81929D336De671d46d',
[Chain.Avalanche]: '0x65285E9dfab318f57051ab2b139ccCf232945451',
}

const protocolDataProviderContract = ProtocolDataProvider__factory.connect(
contractAddresses[this.chainId]!,
protocolDataProviderContractAddresses[this.chainId]!,
this.provider,
)

Expand All @@ -87,6 +117,18 @@ export abstract class AaveV2BasePoolAdapter

const metadataObject: AaveV2PoolMetadata = {}
for (const { tokenAddress } of reserveTokens) {
const reserveConfigurationData =
await protocolDataProviderContract.getReserveConfigurationData(
tokenAddress,
)

if (
!reserveConfigurationData.isActive ||
reserveConfigurationData.isFrozen
Copy link
Collaborator Author

@bergarces bergarces Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks that the protocol token is active and not frozen.

Otherwise, they are not included in the metadata file.

) {
continue
}

const reserveTokenAddresses =
await protocolDataProviderContract.getReserveTokensAddresses(
tokenAddress,
Expand Down Expand Up @@ -170,6 +212,10 @@ export abstract class AaveV2BasePoolAdapter
>,
): string

protected abstract getReserveTokenRate(
reserveData: Awaited<ReturnType<ProtocolDataProvider['getReserveData']>>,
): bigint

private async fetchPoolMetadata(protocolTokenAddress: string) {
const poolMetadata = (await this.buildMetadata())[protocolTokenAddress]

Expand All @@ -180,4 +226,27 @@ export abstract class AaveV2BasePoolAdapter

return poolMetadata
}

private async getProtocolTokenApr({
protocolTokenAddress,
blockNumber,
}: GetAprInput): Promise<number> {
const protocolDataProviderContract = ProtocolDataProvider__factory.connect(
protocolDataProviderContractAddresses[this.chainId]!,
this.provider,
)

const underlyingTokenMetadata = (
await this.fetchPoolMetadata(protocolTokenAddress)
).underlyingToken

const reserveData = await protocolDataProviderContract.getReserveData(
underlyingTokenMetadata.address,
{ blockTag: blockNumber },
)

const aprRaw = this.getReserveTokenRate(reserveData)

return Number(aprRaw) / RAY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ export class AaveV2StableDebtTokenPoolAdapter extends AaveV2BasePoolAdapter {
): string {
return reserveTokenAddresses.stableDebtTokenAddress
}

protected getReserveTokenRate(
reserveData: Awaited<ReturnType<ProtocolDataProvider['getReserveData']>>,
): bigint {
return reserveData.stableBorrowRate
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ export class AaveV2VariableDebtTokenPoolAdapter extends AaveV2BasePoolAdapter {
): string {
return reserveTokenAddresses.variableDebtTokenAddress
}

protected getReserveTokenRate(
reserveData: Awaited<ReturnType<ProtocolDataProvider['getReserveData']>>,
): bigint {
return reserveData.variableBorrowRate
}
}
Loading