Skip to content

Contract 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.

The 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.

ts
getBalance(assetId: BytesLike) {
  return this.provider.getContractBalance(this.id, assetId);
}
See code in context

Checking Contract Balance

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

rust
contract;

use std::asset::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);
    }
}
See code in context

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.

ts
import type { AssetId } from 'fuels';
import { Wallet, BN, BaseAssetId } from 'fuels';

const amountToForward = 40;
const amountToTransfer = 10;

const recipient = Wallet.generate({
  provider,
});

const asset: AssetId = {
  value: BaseAssetId,
};

await contract.functions
  .transfer(amountToTransfer, asset, 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);
See code in context

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.