-
Notifications
You must be signed in to change notification settings - Fork 46
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
@@ -31,6 +33,15 @@ type AaveV2PoolMetadata = Record< | |
} | ||
> | ||
|
||
const protocolDataProviderContractAddresses: Partial<Record<Chain, string>> = { | ||
[Chain.Ethereum]: '0x057835Ad21a177dbdd3090bB1CAE03EaCF78Fc6d', | ||
[Chain.Polygon]: '0x7551b5D2763519d4e37e8B81929D336De671d46d', | ||
[Chain.Avalanche]: '0x65285E9dfab318f57051ab2b139ccCf232945451', | ||
} | ||
|
||
// A RAY unit represents 27 decimal places | ||
const RAY = 10 ** 27 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
// Aave tokens always pegged one to one to underlying | ||
const PRICE_PEGGED_TO_ONE = 1 | ||
export abstract class AaveV2BasePoolAdapter | ||
|
@@ -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') | ||
} | ||
|
@@ -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, | ||
) | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
@@ -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] | ||
|
||
|
@@ -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 | ||
} | ||
} |
There was a problem hiding this comment.
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.