Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Contract Balance

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::asset::transfer;
 
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(
            Identity::Address(recipient_address),
            asset_id,
            amount_to_transfer,
        );
    }
}

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 { AssetId, Wallet, BN };
 
const amountToForward = 40;
const amountToTransfer = 10;
const baseAssetId = provider.getBaseAssetId();
 
const recipient = Wallet.generate({
  provider,
});
 
const asset: AssetId = {
  bits: baseAssetId,
};
 
const { waitForResult } = await contract.functions
  .transfer(amountToTransfer, asset, recipient.address.toB256())
  .callParams({
    forward: [amountToForward, baseAssetId],
  })
  .call();
 
await waitForResult();
 
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.