Icon LinkCalling contracts

Once you've deployed your contract, as seen in the previous sections, you'll likely want to:

  1. Call contract methods;
  2. Configure call and transaction parameters such as gas price, byte price, and gas limit;
  3. Forward coins and gas in your contract calls;
  4. Read and interpret returned values and logs.

Here's an example. Suppose your Sway contract has two ABI methods called initialize_counter(u64) and increment_counter(u64). Once you've deployed it the contract, you can call these methods like this:

// This will generate your contract's methods onto `MyContract`.
// This means an instance of `MyContract` will have access to all
// your contract's methods that are running on-chain!
abigen!(Contract(
    name = "MyContract",
    abi = "packages/fuels/tests/contracts/contract_test/out/debug/contract_test-abi.json"
));
 
// This is an instance of your contract which you can use to make calls to your functions
let contract_instance = MyContract::new(contract_id_2, wallet);
 
let response = contract_instance
    .methods()
    .initialize_counter(42) // Build the ABI call
    .call() // Perform the network call
    .await?;
 
assert_eq!(42, response.value);
 
let response = contract_instance
    .methods()
    .increment_counter(10)
    .call()
    .await?;
 
assert_eq!(52, response.value);

The example above uses all the default configurations and performs a simple contract call.

Next, we'll see how we can further configure the many different parameters in a contract call

Was this page helpful?