Skip to content
Open
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
@@ -1,6 +1,5 @@
import { ethers, getAddress } from 'ethers'
import { AdaptersController } from '../../../../core/adaptersController'
import { ZERO_ADDRESS } from '../../../../core/constants/ZERO_ADDRESS'
import { Chain } from '../../../../core/constants/chains'
import { CacheToDb } from '../../../../core/decorators/cacheToDb'
import { NotImplementedError } from '../../../../core/errors/errors'
Expand Down Expand Up @@ -118,7 +117,7 @@ export class AaveV3RewardsAdapter implements IProtocolAdapter {
}

@CacheToDb
async getProtocolTokens(): Promise<ProtocolToken[]> {
async getProtocolTokens(): Promise<[ProtocolToken]> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Handy trick.

It is compatible with the interface signature of ProtocolToken[] whilst, at the same time, and when it is used internally, it clearly specifies that it's an array with only one item.

With this, there's no need to do assertions to check if the item is undefined.

const rewardTokenAddresses = await this.incentivesContract.getRewardsList()

const rewardTokens = await Promise.all(
Expand All @@ -132,93 +131,43 @@ export class AaveV3RewardsAdapter implements IProtocolAdapter {
]
}

private detectPositionEventSignature(
eventSignature = 'Transfer(address,address,uint256)',
): string {
return ethers.id(eventSignature)
}

/**
* Checks if the user has ever opened a position in AaveV3
* Return AToken addresses if found
*/
private async openPositions(userAddress: string): Promise<string[]> {
const topic0 = this.detectPositionEventSignature()
const topic1 = ethers.zeroPadValue(ZERO_ADDRESS, 32)
const topic2 = ethers.zeroPadValue(userAddress, 32)

const logs = await this.provider.getLogs({
topics: [topic0, topic1, topic2],
fromBlock: '0x',
toBlock: 'latest',
})
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If we have to use getLogs here it defeats the purpose. No other adapter is doing this internally.


const aTokenAddresses = (
await this.helpers.metadataProvider.getMetadata({
protocolId: Protocol.AaveV3,
productId: 'a-token',
})
).map((token) => token.address)

const filteredLogs = logs
.filter((log) => aTokenAddresses.includes(log.address))
.map((log) => log.address)

return [...new Set(filteredLogs)]
}

async getPositions({
userAddress,
blockNumber,
protocolTokenAddresses,
}: GetPositionsInput): Promise<ProtocolPosition[]> {
const protocolTokens = await this.getProtocolTokens()
const protocolToken = protocolTokens[0]

if (!protocolToken) {
throw new Error('No protocol token found')
}
const [protocolToken] = await this.getProtocolTokens()

if (
protocolTokenAddresses &&
protocolTokenAddresses.length > 0 &&
!protocolTokenAddresses.includes(protocolToken.address)
) {
return []
}

const addressFilter = await this.openPositions(userAddress)

if (!addressFilter.length) {
return []
}

const userRewards = await this.incentivesContract.getAllUserRewards(
addressFilter,
userAddress,
{ blockTag: blockNumber },
)

const underlyingTokens = await filterMapAsync(
userRewards.unclaimedAmounts,
async (unclaimedAmount, i) => {
if (!unclaimedAmount) {
const rewards = await filterMapAsync(
protocolToken.underlyingTokens,
async (rewardToken) => {
const accruedReward =
await this.incentivesContract.getUserAccruedRewards(
userAddress,
rewardToken.address,
{ blockTag: blockNumber },
)

if (!accruedReward) {
return undefined
}

const underlying = protocolToken.underlyingTokens.find(
(underlying) => underlying.address === userRewards.rewardsList[i],
)!

return {
...underlying,
...rewardToken,
type: TokenType.UnderlyingClaimable,
balanceRaw: unclaimedAmount,
balanceRaw: accruedReward,
}
},
)

if (underlyingTokens.length === 0) {
if (rewards.length === 0) {
return []
}

Expand All @@ -227,7 +176,7 @@ export class AaveV3RewardsAdapter implements IProtocolAdapter {
type: TokenType.Protocol,
balanceRaw: 1n, // choose 1 here as a zero value may cause the position to be ignored on UIs our adapters currently expecting a protocol token but on contract positions there is no token
...this.INCENTIVES_CONTRACT_DETAILS,
tokens: underlyingTokens,
tokens: rewards,
},
]
}
Expand Down
Loading