Skip to content

Commit

Permalink
feat: beefy fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsains committed Dec 9, 2024
1 parent e42832a commit 733a1a7
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 232 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"build:watch": "npm run build:watch -w packages/adapters-library",
"adapters-cli": "node --env-file=.env packages/adapters-library/dist/scripts/index.js",
"build-metadata-db": "npm run adapters-cli build-metadata-db --",
"delete-adapter-metadata": "npm run adapters-cli delete-adapter-metadata --",
"check-metadata-type": "npm run adapters-cli check-metadata-type --",
"check-db-totals": "npm run adapters-cli check-db-totals --",
"check-bad-snapshots": "npm run adapters-cli check-bad-snapshots --",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
"e74c02fad89642c9c831daf768b09296": {
"result": "0x000000000000000000000000000000000000000000000000156d11ae1d6fad9e00000000000000000000000000000000000000000000000000000000c68b9cf3"
},
"c08559f21e6dae77b5e2c77c82f1e0a4": {
"result": "0x00000000000000000000000000000000000000000000000000000001fcee5979"
},
"a0d7c4aaf7651d65734fcd5dd2ccce94": {
"result": "0x00000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e5831"
},
"c08559f21e6dae77b5e2c77c82f1e0a4": {
"result": "0x00000000000000000000000000000000000000000000000000000001fcee5979"
},
"384c7c494bba6adf0a332dc46f3590e1": {
"result": "0x0000000000000000000000000000000000000000000000010000000000096bec0000000000000000000000000000000000000000000000000000004cbdc10ca0000000000000000000000000000000000000000000000000000000006685417000000000000000000000000000000000000000000000000000000000668541700000000000000000000000000000000000000000000000010000000000096bec"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,19 @@ import {
TokenType,
UnwrapExchangeRate,
UnwrapInput,
UnwrappedTokenExchangeRate,
} from '../../../../types/adapter'
import { Erc20Metadata } from '../../../../types/erc20Metadata'
import { Protocol } from '../../../protocols'
import { breakdownFetcherMap, chainIdMap, protocolMap } from '../../sdk/config'
import { BeefyVaultV7__factory } from '../../contracts'
import { chainIdMap } from '../../sdk/config'
import {
ApiClmManager,
ApiVault,
BeefyProductType,
ProtocolUnwrapType,
} from '../../sdk/types'

type AdditionalMetadata = {
unwrapType: ProtocolUnwrapType
underlyingLPToken: Erc20Metadata
}

export class BeefyMooTokenAdapter implements IProtocolAdapter {
productId = BeefyProductType.MOO_TOKEN
protocolId: Protocol
Expand Down Expand Up @@ -82,7 +79,7 @@ export class BeefyMooTokenAdapter implements IProtocolAdapter {
}

@CacheToDb
async getProtocolTokens(): Promise<ProtocolToken<AdditionalMetadata>[]> {
async getProtocolTokens(): Promise<ProtocolToken[]> {
const chain = chainIdMap[this.chainId]

const cowTokenAddresses = await fetch(
Expand Down Expand Up @@ -111,87 +108,85 @@ export class BeefyMooTokenAdapter implements IProtocolAdapter {

// for each vault, get the latest breakdown to get the token list
return await filterMapAsync(vaults, async (vault) => {
const platformConfig = protocolMap[vault.platformId]
const protocolType =
vault.tokenAddress &&
cowTokenAddresses.has(vault.tokenAddress.toLocaleLowerCase())
? 'beefy_clm'
: typeof platformConfig === 'string' || !platformConfig
? platformConfig
: platformConfig[vault.strategyTypeId || 'default']

if (!protocolType) {
logger.debug(
{
productId: this.productId,
vaultId: vault.id,
platformId: vault.platformId,
vaultAddress: vault.earnedTokenAddress,
poolAddress: vault.tokenAddress,
strategyTypeId: vault.strategyTypeId,
chain,
},
'Protocol type not found',
)
return undefined
}

// test that we can indeed fetch the breakdown, otherwise we don't include the vault
// in the list
const [protocolToken, underlyingToken, breakdown] = await Promise.all([
this.helpers.getTokenMetadata(vault.earnedTokenAddress),
this.helpers.getTokenMetadata(vault.tokenAddress),
breakdownFetcherMap[protocolType](
{
protocolTokenAddress: vault.earnedTokenAddress,
underlyingLPTokenAddress: vault.tokenAddress,
blockSpec: { blockTag: undefined },
},
this.provider,
),
])

const breakdownTokenMetadata = await Promise.all(
breakdown.balances.map((balance) =>
this.helpers.getTokenMetadata(balance.tokenAddress),
),
if (
vault.earnedTokenAddress !==
'0x4D75a9342113c106F48117d81e2952A5828d1B5F'
)

return {
...protocolToken,
underlyingTokens: breakdownTokenMetadata,
underlyingLPToken: underlyingToken,
unwrapType: protocolType,
return
try {
const [protocolToken, underlyingToken] = await Promise.all([
this.helpers.getTokenMetadata(vault.earnedTokenAddress),
this.helpers.getTokenMetadata(vault.tokenAddress),
])

return {
...protocolToken,
underlyingTokens: [underlyingToken],
}
} catch (error) {
return
}
})
}

async unwrap({
protocolTokenAddress,
blockNumber,
protocolTokenAddress,
}: UnwrapInput): Promise<UnwrapExchangeRate> {
const {
underlyingTokens,
unwrapType,
underlyingLPToken,
...protocolToken
} = await this.getProtocolTokenByAddress(protocolTokenAddress)
const protocolTokenMetadata =
await this.getProtocolTokenByAddress(protocolTokenAddress)

const underlyingTokenConversionRate = await this.unwrapProtocolToken(
protocolTokenMetadata,
blockNumber,
)

return {
...protocolToken,
...protocolTokenMetadata,
baseRate: 1,
type: TokenType['Protocol'],
tokens: [
{
...underlyingLPToken,
underlyingRateRaw: BigInt(10 ** underlyingLPToken.decimals),
type: TokenType['Underlying'],
},
],
type: TokenType.Protocol,
tokens: underlyingTokenConversionRate,
}
}

protected async unwrapProtocolToken(
protocolTokenMetadata: Erc20Metadata,
blockNumber?: number | undefined,
): Promise<UnwrappedTokenExchangeRate[]> {
const [underlyingToken] = await this.getUnderlyingTokens(
protocolTokenMetadata.address,
)

const wstEthContract = BeefyVaultV7__factory.connect(
protocolTokenMetadata.address,
this.provider,
)

const pricePerShareRaw = await wstEthContract.getPricePerFullShare({
blockTag: blockNumber,
})

return [
{
...underlyingToken!,
type: TokenType.Underlying,
underlyingRateRaw: pricePerShareRaw,
},
]
}

protected async getUnderlyingTokens(
protocolTokenAddress: string,
): Promise<Erc20Metadata[]> {
const { underlyingTokens } =
await this.getProtocolTokenByAddress(protocolTokenAddress)

return underlyingTokens
}

async getPositions(input: GetPositionsInput): Promise<ProtocolPosition[]> {
console.log('getPositions', await this.getProtocolTokens(), this.chainId)

return this.helpers.getBalanceOfTokens({
...input,
protocolTokens: await this.getProtocolTokens(),
Expand Down Expand Up @@ -237,7 +232,7 @@ export class BeefyMooTokenAdapter implements IProtocolAdapter {

private async getProtocolTokenByAddress(
protocolTokenAddress: string,
): Promise<ProtocolToken<AdditionalMetadata>> {
): Promise<ProtocolToken> {
return this.helpers.getProtocolTokenByAddress({
protocolTokens: await this.getProtocolTokens(),
protocolTokenAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,155 +1,7 @@
{
"blockNumber": 21270814,
"latency": "Latency: 2.191 seconds",
"aggregatedValues": ["USD6.84"],
"snapshot": [
{
"protocolId": "beefy",
"name": "Beefy",
"description": "Beefy defi adapter",
"siteUrl": "https://beefy.com",
"iconUrl": "https://beefy.com/icons/icon-96x96.png",
"positionType": "supply",
"chainId": 1,
"productId": "moo-token",
"chainName": "ethereum",
"success": true,
"tokens": [
{
"address": "0x5dA90BA82bED0AB701E6762D2bF44E08634d9776",
"name": "Moo Aura weETH/ezETH/rsETH",
"symbol": "mooAuraweETH/ezETH/rsETH",
"decimals": 18,
"balanceRaw": "1992424932580756n",
"type": "protocol",
"tokens": [
{
"address": "0x848a5564158d84b8A8fb68ab5D004Fae11619A54",
"name": "Balancer weETH/ezETH/rswETH",
"symbol": "weETH/ezETH/rswETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "1992424932580756n",
"tokens": [
{
"address": "0xbf5495Efe5DB9ce00f80364C8B423567e58d2110",
"name": "Renzo Restaked ETH",
"symbol": "ezETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "549060233465202n",
"tokens": [
{
"address": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
"name": "Ethereum",
"symbol": "ETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "564446160749280n",
"balance": 0.00056444616074928,
"price": 3362.61634367,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png"
}
],
"balance": 0.000549060233465202,
"price": 3456.844567450548,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xbf5495Efe5DB9ce00f80364C8B423567e58d2110/logo.png"
},
{
"address": "0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee",
"name": "Wrapped eETH",
"symbol": "weETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "473921834355169n",
"tokens": [
{
"address": "0x35fA164735182de50811E8e2E824cFb9B6118ac2",
"name": "ether.fi ETH",
"symbol": "eETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "499390496766600n",
"tokens": [
{
"address": "0x0000000000000000000000000000000000000000",
"name": "Ethereum",
"symbol": "ETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "499390496766600n",
"balance": 0.0004993904967666,
"price": 3362.61634367,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/info/logo.png"
}
],
"balance": 0.0004993904967666,
"price": 3362.61634367,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x35fA164735182de50811E8e2E824cFb9B6118ac2/logo.png"
}
],
"balance": 0.000473921834355169,
"price": 3543.324077029906,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xCd5fE23C85820F7B72D0926FC9b05b43E359b7ee/logo.png"
},
{
"address": "0xFAe103DC9cf190eD75350761e95403b7b8aFa6c0",
"name": "rswETH",
"symbol": "rswETH",
"decimals": 18,
"type": "underlying",
"balanceRaw": "949762392785408n",
"balance": 0.000949762392785408,
"price": 3434.3820248325205,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xFAe103DC9cf190eD75350761e95403b7b8aFa6c0/logo.png"
}
],
"balance": 0.001992424932580756,
"price": 3432.5616535744143,
"iconUrl": "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x848a5564158d84b8A8fb68ab5D004Fae11619A54/logo.png"
}
],
"balance": 0.001992424932580756
}
]
}
],
"rpcResponses": {
"cff862862aa17152ed93923842066c9a": {
"result": "0x0000000000000000000000000000000000000000000000000007141994591d94"
},
"d683ef0e0360dc16bc66e3449549ccff": {
"result": "0x00000000000000000000000000000000000000000000000a405aa28b7d225aaa"
},
"d67f1c128f4ebf28506b9605e245fa37": {
"result": "0x000000000000000000000000ba12222222228d8ba445958a75a0704d566bf2c8"
},
"b85d862233ca8e06dfbce73d8bd1dbac": {
"result": "0x848a5564158d84b8a8fb68ab5d004fae11619a5400000000000000000000066a"
},
"d1dd2dc6cadfbcb3c8ca1fde95274f46": {
"result": "0x0000000000000000000000000000000000000000000000597873ba136410cca9"
},
"e5f133f84849e01438d54c6c286b945f": {
"result": "0x000000000000000000000000000000000000000000000009f9e5316a4763988f"
},
"44415a67c28ea019afb5a191cbe04c95": {
"result": "0x000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000001448fea0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000848a5564158d84b8a8fb68ab5d004fae11619a54000000000000000000000000bf5495efe5db9ce00f80364c8b423567e58d2110000000000000000000000000cd5fe23c85820f7b72d0926fc9b05b43e359b7ee000000000000000000000000fae103dc9cf190ed75350761e95403b7b8afa6c0000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000080000000002326c24027875f0820000000000000000000000000000000000000000000000018a7de49a4617c299a00000000000000000000000000000000000000000000001548184575a643d6a200000000000000000000000000000000000000000000002aa63f2fda826c9bba"
},
"e96fe24c9e4e37c32c90388bb1962f9c": {
"result": "0x00000000000000000000000000000000000080000000007c9f35fa3aeb6fd4c9"
},
"63653cab6f336e0ca092ec86f77a3480": {
"result": "0x0000000000000000000000000000000000000000000000000e9fa3250ffe7b19"
},
"d6e653334621e4bd98aad2df3e77352d": {
"result": "0x0000000000000000000000000000000000000000000000070000000000000b530000000000000000000000000000000000000000000000000000004e4ac1593f00000000000000000000000000000000000000000000000000000000674583e500000000000000000000000000000000000000000000000000000000674584070000000000000000000000000000000000000000000000070000000000000b53"
},
"8be7bf2e9ede0ef46c41ef9d3261ff82": {
"result": "0x0000000000000000000000000000000000000000000000000e4444d466db902d"
},
"92e6d0792deed61aa4d23b727c0ee847": {
"result": "0x0000000000000000000000000000000000000000000000000e2c8955529d22b4"
}
}
"blockNumber": 21342510,
"latency": "Latency: 1.601 seconds",
"aggregatedValues": [],
"snapshot": [],
"rpcResponses": {}
}
Loading

0 comments on commit 733a1a7

Please sign in to comment.