Skip to content

Commit

Permalink
Use raw amount for contract balance
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Dec 21, 2024
1 parent 102c00c commit ea6731c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
36 changes: 36 additions & 0 deletions src/queries/ContractBalance.graphql
Original file line number Diff line number Diff line change
@@ -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
}
}
}
File renamed without changes.
16 changes: 10 additions & 6 deletions src/routes/v1/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
}),
Expand Down Expand Up @@ -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,
})),
};
Expand Down

0 comments on commit ea6731c

Please sign in to comment.