diff --git a/src/queries/ContractBalance.graphql b/src/queries/ContractBalance.graphql new file mode 100644 index 0000000..18866c2 --- /dev/null +++ b/src/queries/ContractBalance.graphql @@ -0,0 +1,36 @@ +query ContractBalance( + $token_in_1: [Bytes!]! + $token_in_2: [String!]! + $account_not_in: [String!]! + $block: Int + $tokenFirst: Int = 1000 + $tokenSkip: Int = 0 + $first: Int = 1000 + $skip: Int = 0 + $orderBy: TokenBalance_orderBy = id + $orderDirection: OrderDirection = desc +) { + tokens(first: $tokenFirst, skip: $tokenSkip, where: { id_in: $token_in_1 }) { + id + name + decimals + symbol + + balances( + orderBy: $orderBy + orderDirection: $orderDirection + first: $first + skip: $skip + where: { + rawAmount_gt: 0 + token_in: $token_in_2 + account_not_in: $account_not_in + } + ) { + account { + id + } + rawAmount + } + } +} \ No newline at end of file diff --git a/src/queries/VaultSharesBalances.graphql b/src/queries/TokenBalance.graphql similarity index 100% rename from src/queries/VaultSharesBalances.graphql rename to src/queries/TokenBalance.graphql diff --git a/src/routes/v1/contract.ts b/src/routes/v1/contract.ts index 3a30fa1..ce7cbb6 100644 --- a/src/routes/v1/contract.ts +++ b/src/routes/v1/contract.ts @@ -106,7 +106,7 @@ export default async function ( const result = await asyncCache.wrap( `vault:${chain}:${contract_addresses.join(',')}:top-holders:${limit}`, 5 * 60 * 1000, - async () => await getTopHolders(chain, contract_addresses as Hex[], limit) + async () => await getTopContractHolders(chain, contract_addresses as Hex[], limit) ); reply.send(result); } @@ -185,20 +185,24 @@ const getContractHolders = async ( ); }; -const getTopHolders = async (chainId: ChainId, vault_addresses: Hex[], limit: number) => { +const getTopContractHolders = async ( + chainId: ChainId, + contract_addresses: Hex[], + limit: number +) => { const res = ( await Promise.all( getSdksForChain(chainId).map(sdk => paginate({ fetchPage: ({ skip: tokenSkip, first: tokenFirst }) => - sdk.TokenBalance({ + sdk.ContractBalance({ tokenSkip, tokenFirst, skip: 0, first: limit, account_not_in: ['0x0000000000000000000000000000000000000000'], // providing an empty account_not_in will return 0 holders - token_in_1: vault_addresses, - token_in_2: vault_addresses, + token_in_1: contract_addresses, + token_in_2: contract_addresses, orderBy: TokenBalance_OrderBy.Amount, orderDirection: OrderDirection.Desc, }), @@ -226,7 +230,7 @@ const getTopHolders = async (chainId: ChainId, vault_addresses: Hex[], limit: nu symbol: token.symbol, decimals: Number.parseInt(token.decimals, 10), balances: token.balances.map(balance => ({ - balance: balance.amount, + rawAmount: balance.rawAmount, holder: balance.account.id, })), };