The client to use. AvalancheCoreClient
The addresses to get the balance of GetBalanceParameters
The balance information. GetBalanceReturnType
import { createAvalancheCoreClient } from '@avalanche-sdk/client'
import { avalanche } from '@avalanche-sdk/client/chains'
import { getBalance } from '@avalanche-sdk/client/methods/pChain'
const client = createAvalancheCoreClient({
chain: avalanche,
transport: {
type: "http",
url: "<url>",
},
})
const balance = await getBalance(client, {
addresses: ["P-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p"]
})
Get the balance of AVAX controlled by a given address.
This function does not call
platform.getBalance, because AvalancheGo deprecated that method and will remove it. It reads all UTXOs of the addresses withplatform.getUTXOs(one request for each 1024 UTXOs and each 1024 addresses) and adds them up with the rules ofplatform.getBalance. It gets the AVAX asset ID withplatform.getStakingAssetID. It uses the local clock to find which outputs are locked.When it needs more than 1
platform.getUTXOsrequest, the requests do not read 1 state of the chain. A load balancer can send them to different nodes, and the chain can accept a transaction between 2 requests. So it reads all UTXOs again until 2 reads return the same UTXOs (at most 3 reads). If all 3 reads are different, it uses the last read.