In UTXO (Unspent Transaction Output) based systems, each coin is unique, similar to how physical currency bills have different denominations.
A UTXO represents a coin with a specific amount, similar to having a $10 or $5 bill. It's crucial to understand this unique feature of UTXOs, as it differs significantly from Ethereum's account-based system.
In Ethereum, balances are tracked as cumulative totals, similar to a bank account, rather than as distinct 'coins' or 'bills'.
Each UTXO corresponds to a unique coin and has an associated amount. This model allows for greater transparency and control in cryptocurrency transactions. Understanding UTXOs is key for effectively managing and tracking your digital assets.
To check the balance of a specific asset, you can use getBalance
method. This function aggregates the amounts of all unspent coins of the given asset in your wallet.
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
// The returned amount is a BigNumber
const balance: BN = await myWallet.getBalance(assetId);
To retrieve the balances of all assets in your wallet, use the getBalances
method, it returns an array of CoinQuantity
. This is useful for getting a comprehensive view of your holdings.
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
const balances: CoinQuantity[] = await myWallet.getBalances();