-
Notifications
You must be signed in to change notification settings - Fork 48
Aave v3 rewards remove getLogs #420
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
Open
bergarces
wants to merge
6
commits into
main
Choose a base branch
from
gpg-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' | ||
|
|
@@ -118,7 +117,7 @@ export class AaveV3RewardsAdapter implements IProtocolAdapter { | |
| } | ||
|
|
||
| @CacheToDb | ||
| async getProtocolTokens(): Promise<ProtocolToken[]> { | ||
| async getProtocolTokens(): Promise<[ProtocolToken]> { | ||
| const rewardTokenAddresses = await this.incentivesContract.getRewardsList() | ||
|
|
||
| const rewardTokens = await Promise.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', | ||
| }) | ||
|
Collaborator
Author
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. 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 [] | ||
| } | ||
|
|
||
|
|
@@ -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, | ||
| }, | ||
| ] | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.