-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* aave staking adapter * corrections * test update
- Loading branch information
Showing
13 changed files
with
7,509 additions
and
2,170 deletions.
There are no files selected for viewing
Binary file not shown.
1,901 changes: 1,901 additions & 0 deletions
1,901
packages/adapters-library/src/adapters/aave-v3/contracts/StakeToken.ts
Large diffs are not rendered by default.
Oops, something went wrong.
1,771 changes: 1,771 additions & 0 deletions
1,771
packages/adapters-library/src/adapters/aave-v3/contracts/abis/stake-token.json
Large diffs are not rendered by default.
Oops, something went wrong.
1,788 changes: 1,788 additions & 0 deletions
1,788
packages/adapters-library/src/adapters/aave-v3/contracts/factories/StakeToken__factory.ts
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
217 changes: 217 additions & 0 deletions
217
packages/adapters-library/src/adapters/aave-v3/products/staking/aaveV3StakingAdapter.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,217 @@ | ||
import { AdaptersController } from '../../../../core/adaptersController' | ||
import { Chain } from '../../../../core/constants/chains' | ||
import { CacheToDb } from '../../../../core/decorators/cacheToDb' | ||
import { CustomJsonRpcProvider } from '../../../../core/provider/CustomJsonRpcProvider' | ||
import { filterMapAsync } from '../../../../core/utils/filters' | ||
import { Helpers } from '../../../../scripts/helpers' | ||
import { | ||
IProtocolAdapter, | ||
ProtocolToken, | ||
} from '../../../../types/IProtocolAdapter' | ||
import { | ||
GetEventsInput, | ||
GetPositionsInput, | ||
GetRewardPositionsInput, | ||
GetTotalValueLockedInput, | ||
MovementsByBlock, | ||
PositionType, | ||
ProtocolAdapterParams, | ||
ProtocolDetails, | ||
ProtocolPosition, | ||
ProtocolTokenTvl, | ||
TokenType, | ||
UnderlyingReward, | ||
UnwrapExchangeRate, | ||
UnwrapInput, | ||
} from '../../../../types/adapter' | ||
import { Erc20Metadata } from '../../../../types/erc20Metadata' | ||
import { Protocol } from '../../../protocols' | ||
import { StakeToken__factory } from '../../contracts' | ||
import { AAVE_ICON_URL } from '../rewards/aaveV3RewardsAdapter' | ||
|
||
type AdditionalMetadata = { | ||
rewardTokens: Erc20Metadata[] | ||
} | ||
|
||
export class AaveV3StakingAdapter implements IProtocolAdapter { | ||
productId = 'staking' | ||
protocolId: Protocol | ||
chainId: Chain | ||
helpers: Helpers | ||
|
||
adapterSettings = { | ||
enablePositionDetectionByProtocolTokenTransfer: true, | ||
includeInUnwrap: true, | ||
} | ||
|
||
private provider: CustomJsonRpcProvider | ||
|
||
adaptersController: AdaptersController | ||
|
||
constructor({ | ||
provider, | ||
chainId, | ||
protocolId, | ||
adaptersController, | ||
helpers, | ||
}: ProtocolAdapterParams) { | ||
this.provider = provider | ||
this.chainId = chainId | ||
this.protocolId = protocolId | ||
this.adaptersController = adaptersController | ||
this.helpers = helpers | ||
} | ||
|
||
getProtocolDetails(): ProtocolDetails { | ||
return { | ||
protocolId: this.protocolId, | ||
name: 'Aave v3 Staking', | ||
description: 'AaveV3 defi adapter for Safety Module staking', | ||
siteUrl: 'https://app.aave.com/staking/', | ||
iconUrl: AAVE_ICON_URL, | ||
positionType: PositionType.Staked, | ||
chainId: this.chainId, | ||
productId: this.productId, | ||
} | ||
} | ||
|
||
@CacheToDb | ||
async getProtocolTokens(): Promise<ProtocolToken<AdditionalMetadata>[]> { | ||
const protocolTokens = await Promise.all( | ||
[ | ||
'0x4da27a545c0c5B758a6BA100e3a049001de870f5', // stkAAVE | ||
'0x1a88Df1cFe15Af22B3c4c783D4e6F7F9e0C1885d', // stkGHO | ||
'0x9eDA81C21C273a82BE9Bbc19B6A6182212068101', // stkAAVEwstETHBPTv2 | ||
].map(async (address) => this.helpers.getTokenMetadata(address)), | ||
) | ||
|
||
return await Promise.all( | ||
protocolTokens.map(async (protocolToken) => { | ||
const stakeToken = StakeToken__factory.connect( | ||
protocolToken.address, | ||
this.provider, | ||
) | ||
|
||
const [underlyingTokenAddress, rewardTokenAddress] = await Promise.all([ | ||
stakeToken.STAKED_TOKEN(), | ||
stakeToken.REWARD_TOKEN(), | ||
]) | ||
|
||
const [underlyingToken, rewardToken] = await Promise.all([ | ||
this.helpers.getTokenMetadata(underlyingTokenAddress), | ||
this.helpers.getTokenMetadata(rewardTokenAddress), | ||
]) | ||
|
||
return { | ||
...protocolToken, | ||
underlyingTokens: [underlyingToken], | ||
rewardTokens: [rewardToken], | ||
} | ||
}), | ||
) | ||
} | ||
|
||
private async getProtocolTokenByAddress(protocolTokenAddress: string) { | ||
return this.helpers.getProtocolTokenByAddress({ | ||
protocolTokens: await this.getProtocolTokens(), | ||
protocolTokenAddress, | ||
}) | ||
} | ||
|
||
async getPositions(input: GetPositionsInput): Promise<ProtocolPosition[]> { | ||
return this.helpers.getBalanceOfTokens({ | ||
...input, | ||
protocolTokens: await this.getProtocolTokens(), | ||
}) | ||
} | ||
|
||
async getRewardPositions({ | ||
userAddress, | ||
protocolTokenAddress, | ||
blockNumber, | ||
}: GetRewardPositionsInput): Promise<UnderlyingReward[]> { | ||
const protocolToken = this.helpers.getProtocolTokenByAddress({ | ||
protocolTokenAddress, | ||
protocolTokens: await this.getProtocolTokens(), | ||
}) | ||
|
||
if (!protocolToken.rewardTokens) { | ||
return [] | ||
} | ||
|
||
return await filterMapAsync( | ||
protocolToken.rewardTokens, | ||
async (rewardTokenMetadata) => { | ||
const stakeToken = StakeToken__factory.connect( | ||
protocolToken.address, | ||
this.provider, | ||
) | ||
|
||
const rewardBalance = await stakeToken.getTotalRewardsBalance( | ||
userAddress, | ||
{ | ||
blockTag: blockNumber, | ||
}, | ||
) | ||
|
||
if (rewardBalance === 0n) { | ||
return undefined | ||
} | ||
|
||
return { | ||
...rewardTokenMetadata, | ||
type: TokenType.UnderlyingClaimable, | ||
balanceRaw: rewardBalance, | ||
} | ||
}, | ||
) | ||
} | ||
|
||
async getWithdrawals({ | ||
protocolTokenAddress, | ||
fromBlock, | ||
toBlock, | ||
userAddress, | ||
}: GetEventsInput): Promise<MovementsByBlock[]> { | ||
return this.helpers.withdrawals({ | ||
protocolToken: await this.getProtocolTokenByAddress(protocolTokenAddress), | ||
filter: { fromBlock, toBlock, userAddress }, | ||
}) | ||
} | ||
|
||
async getDeposits({ | ||
protocolTokenAddress, | ||
fromBlock, | ||
toBlock, | ||
userAddress, | ||
}: GetEventsInput): Promise<MovementsByBlock[]> { | ||
return this.helpers.deposits({ | ||
protocolToken: await this.getProtocolTokenByAddress(protocolTokenAddress), | ||
filter: { fromBlock, toBlock, userAddress }, | ||
}) | ||
} | ||
|
||
async getTotalValueLocked({ | ||
protocolTokenAddresses, | ||
blockNumber, | ||
}: GetTotalValueLockedInput): Promise<ProtocolTokenTvl[]> { | ||
const protocolTokens = await this.getProtocolTokens() | ||
|
||
return await this.helpers.tvl({ | ||
protocolTokens, | ||
filterProtocolTokenAddresses: protocolTokenAddresses, | ||
blockNumber, | ||
}) | ||
} | ||
|
||
async unwrap({ | ||
protocolTokenAddress, | ||
}: UnwrapInput): Promise<UnwrapExchangeRate> { | ||
return this.helpers.unwrapOneToOne({ | ||
protocolToken: await this.getProtocolTokenByAddress(protocolTokenAddress), | ||
underlyingTokens: ( | ||
await this.getProtocolTokenByAddress(protocolTokenAddress) | ||
).underlyingTokens, | ||
}) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
...ary/src/adapters/aave-v3/products/staking/tests/snapshots/ethereum.positions.stk-gho.json
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"blockNumber": 21136131, | ||
"latency": "Latency: 1.787 seconds", | ||
"aggregatedValues": ["USD389,150.47"], | ||
"snapshot": [ | ||
{ | ||
"protocolId": "aave-v3", | ||
"name": "Aave v3 Staking", | ||
"description": "AaveV3 defi adapter for Safety Module staking", | ||
"siteUrl": "https://app.aave.com/staking/", | ||
"iconUrl": "https://cryptologos.cc/logos/aave-aave-logo.png", | ||
"positionType": "stake", | ||
"chainId": 1, | ||
"productId": "staking", | ||
"chainName": "ethereum", | ||
"success": true, | ||
"tokens": [ | ||
{ | ||
"address": "0x1a88Df1cFe15Af22B3c4c783D4e6F7F9e0C1885d", | ||
"name": "stk GHO", | ||
"symbol": "stkGHO", | ||
"decimals": 18, | ||
"balanceRaw": "386984197341887736805015n", | ||
"type": "protocol", | ||
"tokens": [ | ||
{ | ||
"address": "0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9", | ||
"name": "Aave Token", | ||
"symbol": "AAVE", | ||
"decimals": 18, | ||
"type": "underlying-claimable", | ||
"balanceRaw": "11644419193456656219n", | ||
"balance": 11.644419193456656, | ||
"price": 182.98103348656943, | ||
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9/logo.png" | ||
}, | ||
{ | ||
"address": "0x40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f", | ||
"name": "Gho Token", | ||
"symbol": "GHO", | ||
"decimals": 18, | ||
"type": "underlying", | ||
"balanceRaw": "386984197341887736805015n", | ||
"balance": 386984.19734188775, | ||
"price": 1.0000918951901496, | ||
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x40D16FC0246aD3160Ccc09B8D0D3A2cD28aE6C2f/logo.png" | ||
} | ||
], | ||
"balance": 386984.19734188775 | ||
} | ||
] | ||
} | ||
], | ||
"rpcResponses": { | ||
"2d9e20727fe7076c64cdb0e9eefb83d5": { | ||
"result": "0x0000000000000000000000000000000000000000000051f27493604bd37b7697" | ||
}, | ||
"386fb807616c3892bc7920d682f3f37a": { | ||
"result": "0x000000000000000000000000000000000000000000000000a199498fc9b97f5b" | ||
}, | ||
"731ce42c7bf681cde66ce61e5f845885": { | ||
"result": "0x00000000000000000000000000000000000000000000000700000000000006e90000000000000000000000000000000000000000000000000000004190af6cd000000000000000000000000000000000000000000000000000000000672cb65400000000000000000000000000000000000000000000000000000000672cb66300000000000000000000000000000000000000000000000700000000000006e9" | ||
}, | ||
"a527f1dc81f463bf406299c2a6112af0": { | ||
"result": "0x00000000000000000000000000000000000000000000000000014300d9716556" | ||
}, | ||
"540e67fcfa7c02400c79a292fc5da6ca": { | ||
"result": "0x00000000000000000000000000000000000000000000000000e6da0cd3b408b2" | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/adapters-library/src/adapters/aave-v3/products/staking/tests/testCases.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Chain } from '../../../../../core/constants/chains' | ||
import type { TestCase } from '../../../../../types/testCase' | ||
|
||
export const testCases: TestCase[] = [ | ||
{ | ||
key: 'stkGHO', | ||
chainId: Chain.Ethereum, | ||
method: 'positions', | ||
|
||
input: { | ||
userAddress: '0x10fd41ec6FDFE7f9C7Cc7c12DC4f0B4e77659BfA', | ||
filterProtocolTokens: ['0x1a88Df1cFe15Af22B3c4c783D4e6F7F9e0C1885d'], | ||
}, | ||
|
||
blockNumber: 21136131, | ||
}, | ||
] |
This file contains 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
This file contains 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
Oops, something went wrong.