Skip to content
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

Mmi 5817 implement support for beefy on linea #387

Merged
merged 8 commits into from
Dec 13, 2024
Merged
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
Binary file modified arb.db
Binary file not shown.
Binary file modified avax.db
Binary file not shown.
Binary file modified base.db
Binary file not shown.
Binary file modified ethereum.db
Binary file not shown.
Binary file modified linea.db
Binary file not shown.
Binary file modified matic.db
Binary file not shown.
Binary file modified op.db
Binary file not shown.
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 @@ -55,15 +55,15 @@
"a3e4474df9052353b8b8dbfa8e0e3a90": {
"result": "0x000000000000000000000000000000000000000000000000000000000096fb63"
},
"e74c02fad89642c9c831daf768b09296": {
"result": "0x000000000000000000000000000000000000000000000000156d11ae1d6fad9e00000000000000000000000000000000000000000000000000000000c68b9cf3"
},
"c08559f21e6dae77b5e2c77c82f1e0a4": {
"result": "0x00000000000000000000000000000000000000000000000000000001fcee5979"
},
"a0d7c4aaf7651d65734fcd5dd2ccce94": {
"result": "0x00000000000000000000000082af49447d8a07e3bd95bd0d56f35241523fbab1000000000000000000000000af88d065e77c8cc2239327c5edb3a432268e5831"
},
"e74c02fad89642c9c831daf768b09296": {
"result": "0x000000000000000000000000000000000000000000000000156d11ae1d6fad9e00000000000000000000000000000000000000000000000000000000c68b9cf3"
},
"384c7c494bba6adf0a332dc46f3590e1": {
"result": "0x0000000000000000000000000000000000000000000000010000000000096bec0000000000000000000000000000000000000000000000000000004cbdc10ca0000000000000000000000000000000000000000000000000000000006685417000000000000000000000000000000000000000000000000000000000668541700000000000000000000000000000000000000000000000010000000000096bec"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,13 @@ 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 {
ApiClmManager,
ApiVault,
BeefyProductType,
ProtocolUnwrapType,
} from '../../sdk/types'

type AdditionalMetadata = {
unwrapType: ProtocolUnwrapType
underlyingLPToken: Erc20Metadata
}
import { BeefyVaultV7__factory } from '../../contracts'
import { chainIdMap } from '../../sdk/config'
import { ApiClmManager, ApiVault, BeefyProductType } from '../../sdk/types'

export class BeefyMooTokenAdapter implements IProtocolAdapter {
productId = BeefyProductType.MOO_TOKEN
Expand Down Expand Up @@ -82,20 +74,9 @@ export class BeefyMooTokenAdapter implements IProtocolAdapter {
}

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

const cowTokenAddresses = await fetch(
`https://api.beefy.finance/cow-vaults/${chain}`,
)
.then((res) => res.json())
.then((res) =>
(res as ApiClmManager[]).map((r) =>
r.earnedTokenAddress.toLocaleLowerCase(),
),
)
.then((res) => new Set(res))

const vaults = await fetch(`https://api.beefy.finance/vaults/${chain}`)
.then((res) => res.json())
.then((res) =>
Expand All @@ -111,86 +92,77 @@ 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),
),
)

return {
...protocolToken,
underlyingTokens: breakdownTokenMetadata,
underlyingLPToken: underlyingToken,
unwrapType: protocolType,
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(
Copy link
Collaborator

Choose a reason for hiding this comment

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

If they are not being inherited and used by a subclass, they should be private.

protocolTokenMetadata: Erc20Metadata,
blockNumber?: number | undefined,
): Promise<UnwrappedTokenExchangeRate[]> {
const {
underlyingTokens: [underlyingToken],
} = await this.getProtocolTokenByAddress(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[]> {
return this.helpers.getBalanceOfTokens({
...input,
Expand Down Expand Up @@ -237,7 +209,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
Loading
Loading