Fetching Balances

There are a couple ways to fetch badge balances, depending on the balance type of the collections you are trying to fetch. Note this is different from $BADGE (see here for fetching $BADGE balance.

Fetching by User or by Collection

If you want to fetch badges owned by a specific user or specific owners for a collection, we refer you to the prior tutorials. Notably, you can fetch a paginated view using the badgesCollected view key for accounts or the owners view key for a collection. These are paginated, so you will need to pass in the prior bookmark received to the next request to fetch the next page (if it has more).

However, note that these only apply to standard on-chain or off-chain indexed balanced. Off-chain non-indexed balances must be fetched directly one by one (see below). You can also fetch off-chain indexed balances directly from source.

pageFetching CollectionspageFetching Accounts
const mintBalances: Balance<bigint>[] = collection.owners.find(
    (x) => x.cosmosAddress == 'Mint'
);

Fetching Directly

You can also fetch balances directly using the getBadgeBalanceByAddress API route. This will work for all balance types (standard, off-chain indexed, and off-chain non-indexed).

const res = await BitBadgesApi.getBadgeBalanceByAddress(
    collectionId,
    cosmosAddress
);
console.log(res);

Verifying Requirements

If you do not need the balances themselves and you just want to verify if a user meets certain ownership requirements, consider using the following. This also supports BitBadges Lists and Ethereum / polygon NFTs.

const res = await BitBadgesApi.verifyOwnershipRequirements({
    cosmosAddress,
    assetOwnershipRequirements: {
        $and: [
            {
                assets: [
                    {
                        chain: 'BitBadges',
                        collectionId: 1n,
                        assetIds: [{ start: 1n, end: 1n }],
                        ownershipTimes: UintRangeArray.FullRanges(),
                        mustOwnAmounts: { start: 0n, end: 0n },
                    },
                ],
            },
            {
                assets: [
                    {
                        chain: 'BitBadges',
                        collectionId: 2n,
                        assetIds: [{ start: 1n, end: 1n }],
                        ownershipTimes: UintRangeArray.FullRanges(),
                        mustOwnAmounts: { start: 0n, end: 0n },
                    },
                ],
            },
        ],
    },
});
console.log(res);

Last updated