Icon LinkContract Balance

When working with contracts, it's crucial to be aware of the available contract balance of an asset while paying for costly operations. This guide will explain the getBalance method in the Contract class, which allows you to check a contract's available balance.

Icon LinkThe getBalance Method

The Contract class includes a method called getBalance that retrieves the available balance of a specific asset for a contract. This method is particularly useful for determining the remaining balance after sending assets to a contract and executing contract calls.

	getBalance(assetId: BytesLike) {
return this.provider.getContractBalance(this.id, assetId);
	}

Icon LinkChecking Contract Balance

Consider a simple contract that transfers a specified amount of a given asset to an address:

contract;
 
use std::token::transfer_to_address;
 
abi TransferToAddress {
#[payable]
fn transfer(amount_to_transfer: u64, asset: b256, recipient: b256);
}
 
impl TransferToAddress for Contract {
#[payable]
fn transfer(amount_to_transfer: u64, asset: b256, recipient: b256) {
	let recipient_address = Address::from(recipient);
 
	let asset_id = ContractId::from(asset);
 
	transfer_to_address(amount_to_transfer, asset_id, recipient_address);
}
}

The transfer function has three parameters:

  1. amount_to_transfer: The amount that is being transferred.

  2. asset: The address of the deployed contract Token.

  3. recipient: The address from the receiver's wallet.

The transfer function calls the built-in Sway function transfer_to_address, which does precisely what the name suggests.

Let's execute this contract and use the getBalance method to validate the remaining asset amount the contract has left to spend.

import { Wallet, BN, NativeAssetId } from 'fuels';
 
const amountToForward = 40;
const amountToTransfer = 10;
 
const recipient = Wallet.generate();
 
await contract.functions
	.transfer(amountToTransfer, NativeAssetId, recipient.address.toB256())
	.callParams({
	forward: [amountToForward, NativeAssetId],
	})
	.call();
 
const contractBalance = await contract.getBalance(NativeAssetId);
 
const expectedBalance = amountToForward - amountToTransfer;
 
expect(new BN(contractBalance).toNumber()).toBe(expectedBalance);

In this example, we first forward an asset amount greater than the amount required for the transfer and then execute the contract call.

Finally, we use the getBalance method to confirm that the contract balance is precisely the total forwarded amount minus the transferred amount.

It is important to note that this method returns the total available contract balance, regardless of how often assets have been sent or spent on costly operations.

Icon ListDetailsOn this page