Skip to content

Commit

Permalink
Add by-address endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Dec 2, 2024
1 parent 9ae9100 commit cee24ab
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
84 changes: 78 additions & 6 deletions src/routes/v1/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<typeof urlParamsSchema>;

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);
Expand Down Expand Up @@ -209,22 +255,48 @@ 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
) => {
// 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'
? [
Expand Down

0 comments on commit cee24ab

Please sign in to comment.