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.
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);
}
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_id: AssetId, recipient: b256);
}
impl TransferToAddress for Contract {
#[payable]
fn transfer(amount_to_transfer: u64, asset_id: AssetId, recipient: b256) {
let recipient_address = Address::from(recipient);
transfer_to_address(recipient_address, asset_id, amount_to_transfer);
}
}
The transfer
function has three parameters:
amount_to_transfer
: The amount that is being transferred.
asset
: The address of the deployed contract Token.
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, BaseAssetId } from 'fuels';
const amountToForward = 40;
const amountToTransfer = 10;
const provider = await Provider.create(FUEL_NETWORK_URL);
const recipient = Wallet.generate({
provider,
});
await contract.functions
.transfer(amountToTransfer, BaseAssetId, recipient.address.toB256())
.callParams({
forward: [amountToForward, BaseAssetId],
})
.call();
const contractBalance = await contract.getBalance(BaseAssetId);
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.