Skip to content

Commit

Permalink
Mmi 5818 aave v 2 missing positions (#379)
Browse files Browse the repository at this point in the history
* correct rewards

* fix ui filter

* refetch fix

* update snapshot

* lint
  • Loading branch information
bergarces authored Nov 25, 2024
1 parent 6e9feb4 commit 823733e
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 26 deletions.
Binary file modified avax.db
Binary file not shown.
Binary file modified ethereum.db
Binary file not shown.
Binary file modified matic.db
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
ProtocolDataProvider__factory,
} from '../contracts'

const protocolDataProviderContractAddresses: Partial<
export const protocolDataProviderContractAddresses: Partial<
Record<Protocol, Partial<Record<Chain, string>>>
> = {
[Protocol.AaveV2]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Chain } from '../../../../core/constants/chains'
import { CacheToDb } from '../../../../core/decorators/cacheToDb'
import { NotImplementedError } from '../../../../core/errors/errors'
import { CustomJsonRpcProvider } from '../../../../core/provider/CustomJsonRpcProvider'
import { filterMapAsync } from '../../../../core/utils/filters'
import { Helpers } from '../../../../scripts/helpers'
import {
IProtocolAdapter,
Expand All @@ -25,7 +26,15 @@ import {
} from '../../../../types/adapter'
import { AAVE_ICON_URL } from '../../../aave-v3/products/rewards/aaveV3RewardsAdapter'
import { Protocol } from '../../../protocols'
import { StakedTokenIncentivesController__factory } from '../../contracts'
import { protocolDataProviderContractAddresses } from '../../common/aaveBasePoolAdapter'
import {
ProtocolDataProvider__factory,
StakedTokenIncentivesController__factory,
} from '../../contracts'

type AdditionalMetadata = {
reserveTokensWithEmissions: string[]
}

export class AaveV2RewardsAdapter implements IProtocolAdapter {
productId = 'rewards'
Expand Down Expand Up @@ -70,29 +79,72 @@ export class AaveV2RewardsAdapter implements IProtocolAdapter {
}

@CacheToDb
async getProtocolTokens(): Promise<ProtocolToken[]> {
const stakedTokensIncentiveControllerAddress = getAddress(
'0xd784927ff2f95ba542bfc824c8a8a98f3495f6b5',
)
async getProtocolTokens(): Promise<ProtocolToken<AdditionalMetadata>[]> {
const stakedTokensIncentiveControllerAddresses: Partial<
Record<Chain, string>
> = {
[Chain.Ethereum]: getAddress(
'0xd784927ff2f95ba542bfc824c8a8a98f3495f6b5',
),
[Chain.Polygon]: getAddress('0x357D51124f59836DeD84c8a1730D72B749d8BC23'),
[Chain.Avalanche]: getAddress(
'0x01D83Fe6A10D2f2B7AF17034343746188272cAc9',
),
}
const stakedTokensIncentiveControllerAddress =
stakedTokensIncentiveControllerAddresses[this.chainId]!

const stakedTokensIncentiveController =
StakedTokenIncentivesController__factory.connect(
stakedTokensIncentiveControllerAddress,
this.provider,
)

const protocolDataProvider = ProtocolDataProvider__factory.connect(
protocolDataProviderContractAddresses[this.protocolId]![this.chainId]!,
this.provider,
)

const reserveTokens = await protocolDataProvider.getAllReservesTokens()

const reserveTokensWithEmissions = (
await Promise.all(
reserveTokens.map(async ({ tokenAddress }) => {
const reserveTokenAddresses =
await protocolDataProvider.getReserveTokensAddresses(tokenAddress)

const reserveTokensWithEmissions = await filterMapAsync(
reserveTokenAddresses,
async (reserveTokenAddress) => {
const assetDetails =
await stakedTokensIncentiveController.assets(
reserveTokenAddress,
)

return assetDetails.emissionPerSecond
? reserveTokenAddress
: undefined
},
)

return reserveTokensWithEmissions
}),
)
).flat()

const rewardTokenAddress =
await stakedTokensIncentiveController.REWARD_TOKEN()

const rewardToken = await this.helpers.getTokenMetadata(rewardTokenAddress)

return [
{
...rewardToken,
address: stakedTokensIncentiveControllerAddress,
name: 'Aave v2 Rewards',
symbol: 'Rewards',
address: stakedTokensIncentiveControllerAddress,
decimals: rewardToken.decimals,
underlyingTokens: [rewardToken],
reserveTokensWithEmissions,
},
]
}
Expand All @@ -102,7 +154,7 @@ export class AaveV2RewardsAdapter implements IProtocolAdapter {
blockNumber,
protocolTokenAddresses,
}: GetPositionsInput): Promise<ProtocolPosition[]> {
const { underlyingTokens, ...protocolToken } = (
const { underlyingTokens, reserveTokensWithEmissions, ...protocolToken } = (
await this.getProtocolTokens()
)[0]!

Expand All @@ -120,11 +172,16 @@ export class AaveV2RewardsAdapter implements IProtocolAdapter {
)

const rewardAmount =
await stakedTokensIncentiveController.getUserUnclaimedRewards(
await stakedTokensIncentiveController.getRewardsBalance(
reserveTokensWithEmissions,
userAddress,
{ blockTag: blockNumber },
)

if (!rewardAmount) {
return []
}

return [
{
...protocolToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"blockNumber": 21143202,
"latency": "Latency: 1.813 seconds",
"latency": "Latency: 2.107 seconds",
"aggregatedValues": ["USD88.37"],
"snapshot": [
{
Expand All @@ -21,41 +21,41 @@
"symbol": "Rewards",
"decimals": 18,
"type": "protocol",
"balanceRaw": "484007380433762915n",
"balanceRaw": "484007420894897694n",
"tokens": [
{
"address": "0x4da27a545c0c5B758a6BA100e3a049001de870f5",
"name": "Staked Aave",
"symbol": "stkAAVE",
"decimals": 18,
"type": "underlying-claimable",
"balanceRaw": "484007380433762915n",
"balanceRaw": "484007420894897694n",
"tokens": [
{
"address": "0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9",
"name": "Aave Token",
"symbol": "AAVE",
"decimals": 18,
"type": "underlying-claimable",
"balanceRaw": "484007380433762915n",
"balance": 0.4840073804337629,
"balanceRaw": "484007420894897694n",
"balance": 0.4840074208948977,
"price": 182.58831074981887,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9/logo.png"
}
],
"balance": 0.4840073804337629,
"balance": 0.4840074208948977,
"price": 182.58831074981887,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x4da27a545c0c5B758a6BA100e3a049001de870f5/logo.png"
}
],
"balance": 0.4840073804337629
"balance": 0.4840074208948977
}
]
}
],
"rpcResponses": {
"e2542edd3bb17e3f089d5e2fe65827d3": {
"result": "0x00000000000000000000000000000000000000000000000006b78a25eb6bfa63"
"b4639da0df30cbddde659dfe2134cc27": {
"result": "0x00000000000000000000000000000000000000000000000006b78a2f5717e61e"
},
"036f5df494d83a21caf9e17028f5a1fc": {
"result": "0x0000000000000000000000000000000000000000000000070000000000000715000000000000000000000000000000000000000000000000000000445230f70000000000000000000000000000000000000000000000000000000000672e077f00000000000000000000000000000000000000000000000000000000672e078f0000000000000000000000000000000000000000000000070000000000000715"
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function App() {
.sort()
.map((value) => ({
value,
label: ChainIdToChainNameMap[+value as unknown as Chain],
label: value,
}))

const chainOptions = Array.from(
Expand Down
9 changes: 6 additions & 3 deletions packages/dev-ui/src/Positions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ export function Positions() {
queryKey: [
'positions',
data.userAddress,
data.protocolIds?.length ? data.protocolIds.join(',') : null,
data.chainIds?.length ? data.chainIds.join(',') : null,
data.protocolIds?.length
? data.protocolIds.map((v) => v.value).join(',')
: null,
data.chainIds?.length
? data.chainIds.map((v) => v.value).join(',')
: null,
],
})
}
Expand Down Expand Up @@ -160,7 +164,6 @@ function PositionsDisplay({
chainIds: number[] | undefined
}) {
const { isPending, error, data, isFetching, isRefetching } = useQuery({
staleTime: 60000,
queryKey: [
'positions',
userAddress,
Expand Down
8 changes: 6 additions & 2 deletions packages/dev-ui/src/Profits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ export function Profits() {
'profits',
data.userAddress,
data.timePeriod,
data.protocolIds?.length ? data.protocolIds.join(',') : null,
data.chainIds?.length ? data.chainIds.join(',') : null,
data.protocolIds?.length
? data.protocolIds.map((v) => v.value).join(',')
: null,
data.chainIds?.length
? data.chainIds.map((v) => v.value).join(',')
: null,
],
})
}
Expand Down
11 changes: 10 additions & 1 deletion packages/dev-ui/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import './index.css'

const queryClient = new QueryClient()
const queryClient = new QueryClient({
defaultOptions: {
queries: {
refetchOnWindowFocus: false,
refetchOnReconnect: false,
refetchOnMount: false,
staleTime: Number.POSITIVE_INFINITY, // Data is considered stale immediately
},
},
})

ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
Expand Down

0 comments on commit 823733e

Please sign in to comment.