I’m getting this result while using this code.
contract_token_all_balance {
balances: [
{
assetId: ‘0xd331425f990cafd224fec452061b26257fed4fe0d56f91b3fd0db1623ae61146’,
amount: <BN: 0x1e8480>
},
{
assetId: ‘0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07’,
amount: <BN: 0x30d40>
}
]
}
I’m getting zero when I try to get the balance from contract. this is the contract code.
#[storage(read)]
fn get_asset_balance(asset_id: AssetId) → u64 {
this_balance(asset_id)
}
can anyone help me to resolve this issue? I try to get the native coin and src20 token balance from contract.
this_balance() function is used to get the balance of the current contract. If that’s what you are trying to do then the correct syntax is this_balance(AssetId::base()). You can have a look at the example here
i’m doing the same which you mentioned in the reply. still, I get the zero balance in contract code. when I search the contract address in fuel explorer. i can able to see the balance in the account page but not on the contract page. Do I need to follow any other steps to send the coin to the contract? I’m a little bit confused here. can anyone please me to resolve this issue? i have attached my git repo link please look into it
Hey @aravindhk, the discrepancy between the account balance and the contract balance can be due to the fundamental difference between how balances are managed for accounts and contracts in the Fuel network.
Account balances sum up the UTXOs, contract balances fetch the balance owned by the contract state. If there is a regular balance associated with a contract id that’s likely due to user error, as those utxos will essentially be stuck since no one will be able to sign over those.
I don’t have specific documentation that directly compares account and contract balances. However, I can provide you with relevant documentation links that cover how to check balances for both accounts and contracts, which should help you understand the differences.
Relevant Documentation
Checking Account Balances:
This documentation explains how to check the balance of a specific asset in your wallet.