Get Token Balance
Overview
This section details how to retrieve token balance using user wallet address and token IDs.
Properties
addresses
addresses
A list of addresses used to query tokens.
e.g.
[
"0x2D2438C6281B5115733287fC279f854c868D3ee2",
"0xFFFFFfFF00000000000000000000000000000417"
]
genericTokenIds
genericTokenIds
A list of token IDs to filter tokens on the addresses provided. (Optional)
If this property is not provided, all tokens owned by the above addresses will be retrieved.
e.g.
[
"7672:root:1"
]
Usage
1. Retrieve balances of specified tokens
import { useGenericTokenBalances } from '@futureverse/asset-register-react/v2'
import { ChainAddress, GenericTokenId } from '@futureverse/asset-register/types'
export const GenericTokenBalances = () => {
const { genericTokenBalances } = useGenericTokenBalances({
addresses: ['0x8bb9a0fff4ff087a5de85ba0e89a5d376ce2a595' as ChainAddress],
genericTokenIds: ['7672:root:1' as GenericTokenId],
})
return (
<div>
{genericTokenBalances?.map((genericTokenBalance) => {
return (
<div>
<div>{genericTokenBalance.genericToken.chainId}</div>
<div>{genericTokenBalance.genericToken.location}</div>
<div>{genericTokenBalance.owner.address}</div>
<div>{genericTokenBalance.amount}</div>
</div>
)
})}
</div>
)
}
import { AssetRegister } from '@futureverse/asset-register/v2'
import { ChainAddress, GenericTokenId } from '@futureverse/asset-register/types'
import { GenericTokenBalanceModel } from '@futureverse/asset-register/models'
const ar = new AssetRegister({
...
})
const tokenBalances = await ar.genericTokenBalances({
addresses: ['0x8bb9a0fff4ff087a5de85ba0e89a5d376ce2a595' as ChainAddress],
genericTokenIds: ['7672:root:1' as GenericTokenId],
}).execute()[0] as Array<GenericTokenBalanceModel>
query GenericTokenBalances($addresses: [ChainAddress!]!, $genericTokenIds: [GenericTokenId!]) {
genericTokenBalances(addresses: $addresses, genericTokenIds: $genericTokenIds) {
amount
genericToken {
chainId
chainType
location
}
owner {
address
}
}
}
{
"addresses": ["0x8bb9a0fff4ff087a5de85ba0e89a5d376ce2a595"],
"genericTokenIds": ["7672:root:1"]
}
2. Retrieve tokens owned by the specified addresses
import { useGenericTokenBalances } from '@futureverse/asset-register-react/v2'
import { ChainAddress, GenericTokenId } from '@futureverse/asset-register/types'
export const GenericTokenBalances = () => {
const { genericTokenBalances } = useGenericTokenBalances({
addresses: ['0x8bb9a0fff4ff087a5de85ba0e89a5d376ce2a595' as ChainAddress],
})
return (
<div>
{genericTokenBalances?.map((genericTokenBalance) => {
return (
<div>
<div>{genericTokenBalance.genericToken.chainId}</div>
<div>{genericTokenBalance.genericToken.location}</div>
<div>{genericTokenBalance.owner.address}</div>
<div>{genericTokenBalance.amount}</div>
</div>
)
})}
</div>
)
}
import { AssetRegister } from '@futureverse/asset-register/v2'
import { ChainAddress, GenericTokenId } from '@futureverse/asset-register/types'
import { GenericTokenBalanceModel } from '@futureverse/asset-register/models'
const ar = new AssetRegister({
...
})
const tokenBalances = await ar.genericTokenBalances({
addresses: ['0x8bb9a0fff4ff087a5de85ba0e89a5d376ce2a595' as ChainAddress],
}).execute()[0] as Array<GenericTokenBalanceModel>
query GenericTokenBalances($addresses: [ChainAddress!]!, $genericTokenIds: [GenericTokenId!]) {
genericTokenBalances(addresses: $addresses, genericTokenIds: $genericTokenIds) {
amount
genericToken {
chainId
chainType
location
}
owner {
address
}
}
}
{
"addresses": ["0x8bb9a0fff4ff087a5de85ba0e89a5d376ce2a595"],
}
Last updated