const contract_wallet = Wallet.fromAddress(toBech32(market_contract_id), provider);
const contract_token_all_balance = await contract_wallet.getBalances()
console.log("contract_token_all_balance", contract_token_all_balance);
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
source code -
js script - fuel_mint/marketplace.js at main · aravindhkm/fuel_mint · GitHub
contract code - fuel_mint/native-asset/native-asset-contract/src/main.sw at main · aravindhkm/fuel_mint · GitHub
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
At the first glance, your code seems correct. though let me try debugging and get back †o you
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.
@Nazeeh21 Can you please share any reference links for that?
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.
- Checking Balances
- Checking Contract Balances:
- This documentation explains how to check the available balance of a specific asset for a contract.
- Contract Balance
Key Points
- Account Balance:
- Managed by the Fuel network.
- Reflects assets directly held by the account.
- Example method:
getBalance
on a wallet instance.
- Contract Balance:
- Depends on the contract’s logic to manage and track assets.
- Reflects assets explicitly transferred to and managed by the contract.
- Example method:
getBalance
on a contract instance.
Example: Checking Account Balance
const myWallet = Wallet.fromPrivateKey(privateKey, provider);
// The returned amount is a BigNumber
const balance: BN = await myWallet.getBalance(provider.getBaseAssetId());
console.log("Account Balance:", balance.toString());
Example: Checking Contract Balance
const contractBalance = await contract.getBalance(baseAssetId);
console.log("Contract Balance:", contractBalance.toString());
LMK if you need more explanation