From cee24ab625a744b3ea0b8e3d57e68587b28928b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Pr=C3=A9vost?= <998369+prevostc@users.noreply.github.com> Date: Mon, 2 Dec 2024 23:33:42 +0100 Subject: [PATCH] Add by-address endpoint --- README.md | 9 ++++- src/routes/v1/vault.ts | 84 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 5952517..55c070c 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,18 @@ http://localhost:4000/api/v1/config/arbitrum/vaults?include_eol=true https://balance-api.beefy.finance/api/v1/vault/base/baseswap-cow-weth-cbbtc/20449610/bundle-holder-share http://localhost:4000/api/v1/vault/base/baseswap-cow-weth-cbbtc/20449610/bundle-holder-share - https://balance-api.beefy.finance/api/v1/vault/arbitrum/camelot-order-weth/279181618/bundle-holder-share http://localhost:4000/api/v1/vault/arbitrum/camelot-order-weth/279181618/bundle-holder-share https://balance-api.beefy.finance/api/v1/vault/arbitrum/uniswap-cow-arb-usdc-dai-vault/279181618/bundle-holder-share http://localhost:4000/api/v1/vault/arbitrum/uniswap-cow-arb-usdc-dai-vault/279181618/bundle-holder-share +https://balance-api.beefy.finance/api/v1/vault/base/0xb37b4fac09af8d900e15ac942a4ee1e498fa0989/20449610/bundle-holder-share-by-vault-address +http://localhost:4000/api/v1/vault/base/0xb37b4fac09af8d900e15ac942a4ee1e498fa0989/20449610/bundle-holder-share-by-vault-address +https://balance-api.beefy.finance/api/v1/vault/arbitrum/0x42cf53622b413b40cb24f78a79e0e76e587b7f33/279181618/bundle-holder-share-by-vault-address +http://localhost:4000/api/v1/vault/arbitrum/0x42cf53622b413b40cb24f78a79e0e76e587b7f33/279181618/bundle-holder-share-by-vault-address +https://balance-api.beefy.finance/api/v1/vault/arbitrum/0xf4ea976b260a498f26417b89f6dbdd555104a734/279181618/bundle-holder-share-by-vault-address +http://localhost:4000/api/v1/vault/arbitrum/0xf4ea976b260a498f26417b89f6dbdd555104a734/279181618/bundle-holder-share-by-vault-address + + ``` \ No newline at end of file diff --git a/src/routes/v1/vault.ts b/src/routes/v1/vault.ts index 47f27c5..cf52e90 100644 --- a/src/routes/v1/vault.ts +++ b/src/routes/v1/vault.ts @@ -7,7 +7,10 @@ import { addressSchema } from '../../schema/address'; import { bigintSchema } from '../../schema/bigint'; import { getAsyncCache } from '../../utils/async-lock'; import { getSdksForChain, paginate } from '../../utils/sdk'; -import { getBeefyBreakdownableVaultConfig } from '../../vault-breakdown/vault/getBeefyVaultConfig'; +import { + type BeefyVault, + getBeefyBreakdownableVaultConfig, +} from '../../vault-breakdown/vault/getBeefyVaultConfig'; export default async function ( instance: FastifyInstance, @@ -94,7 +97,50 @@ export default async function ( `vault:${chain}:${base_vault_id}:${block_number}:holders`, 5 * 60 * 1000, async () => - getVaultHoldersAsBaseVaultEquivalent(chain, base_vault_id, BigInt(block_number)) + getVaultHoldersAsBaseVaultEquivalentForVaultId( + chain, + base_vault_id, + BigInt(block_number) + ) + ); + + reply.send(result); + } + ); + } + + // all holder count list for all chains + { + const urlParamsSchema = Type.Object({ + chain: chainIdSchema, + vault_address: addressSchema, + block_number: bigintSchema, + }); + type UrlParams = Static; + + const schema: FastifySchema = { + tags: ['vault'], + params: urlParamsSchema, + response: { + 200: vaultHoldersSchema, + }, + }; + + instance.get<{ Params: UrlParams }>( + '/:chain/:vault_address/:block_number/bundle-holder-share-by-vault-address', + { schema }, + async (request, reply) => { + const { chain, vault_address, block_number } = request.params; + + const result = await asyncCache.wrap( + `vault:${chain}:${vault_address}:${block_number}:holders`, + 5 * 60 * 1000, + async () => + getVaultHoldersAsBaseVaultEquivalentForVaultAddress( + chain, + vault_address as Hex, + BigInt(block_number) + ) ); reply.send(result); @@ -209,7 +255,27 @@ const getVaultHolders = async ( }); }; -const getVaultHoldersAsBaseVaultEquivalent = async ( +const getVaultHoldersAsBaseVaultEquivalentForVaultAddress = async ( + chainId: ChainId, + vault_address: Hex, + block: bigint +) => { + // first get the addresses linked to that vault id + const configs = await getBeefyBreakdownableVaultConfig( + chainId, + vault => vault.vault_address === vault_address + ); + if (!configs.length) { + throw new Error(`Vault with "vault_address" ${vault_address} not found`); + } + if (configs.length > 1) { + throw new Error(`Vault with "vault_address" ${vault_address} is not unique`); + } + + return _getVaultHoldersAsBaseVaultEquivalent(chainId, configs[0], block); +}; + +const getVaultHoldersAsBaseVaultEquivalentForVaultId = async ( chainId: ChainId, vault_id: string, block: bigint @@ -217,14 +283,20 @@ const getVaultHoldersAsBaseVaultEquivalent = async ( // first get the addresses linked to that vault id const configs = await getBeefyBreakdownableVaultConfig(chainId, vault => vault.id === vault_id); if (!configs.length) { - throw new Error(`Vault ${vault_id} not found`); + throw new Error(`Vault with "id" ${vault_id} not found`); } if (configs.length > 1) { - throw new Error(`Vault ${vault_id} is not unique`); + throw new Error(`Vault with "id" ${vault_id} is not unique`); } - const config = configs[0]; + return _getVaultHoldersAsBaseVaultEquivalent(chainId, configs[0], block); +}; +const _getVaultHoldersAsBaseVaultEquivalent = async ( + chainId: ChainId, + config: BeefyVault, + block: bigint +) => { const tokens = uniq( (config.protocol_type === 'beefy_clm_vault' ? [