Skip to content

Commit

Permalink
Get all pages of balances
Browse files Browse the repository at this point in the history
  • Loading branch information
tombeynon committed Jan 5, 2024
1 parent 9d40ae5 commit 7413032
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/utils/QueryClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,23 @@ const QueryClient = async (chainId, restUrls, opts) => {
}

function getBalance(address, denom, opts) {
return axios
.get(apiUrl('bank', `balances/${address}`), opts)
.then((res) => res.data)
.then((result) => {
if (!denom)
return result.balances;
return getAllPages((nextKey) => {
const searchParams = new URLSearchParams();
if (nextKey)
searchParams.append("pagination.key", nextKey);
return axios
.get(apiUrl('bank', `balances/${address}?${searchParams.toString()}`), opts)
.then((res) => res.data)
}).then((pages) => {
const result = pages.map((el) => el.balances).flat()
if (!denom)
return result

const balance = result.balances?.find(
(element) => element.denom === denom
) || { denom: denom, amount: 0 };
return balance;
});
const balance = result.find(
(element) => element?.denom === denom
) || { denom: denom, amount: 0 };
return balance;
});
}

function getDelegations(address) {
Expand Down

0 comments on commit 7413032

Please sign in to comment.