Call Parameters
When interacting with contracts, you can configure specific parameters for contract calls using the callParams
method. The available call parameters are:
forward
gasLimit
Note: Setting transaction parameters is also available when calling contracts. More information on this can be found at Transaction Parameters.
The contract in use in this section has the following implementation:
impl ReturnContext for Contract {
#[payable]
fn return_context_amount() -> u64 {
msg_amount()
}
}
Forward Parameter
The forward
parameter allows the sending of a specific amount of coins to a contract when a function is called. This is useful when a contract function requires coins for its execution, such as paying fees or transferring funds. The forward parameter helps you control the resources allocated to the contract call and offers protection against potentially costly operations.
const amountToForward = 10;
const { waitForResult } = await contract.functions
.return_context_amount()
.callParams({
forward: [amountToForward, provider.getBaseAssetId()],
})
.call();
const { value } = await waitForResult();
console.log('forwarded amount:', value.toNumber());
// forwarded amount: 10
Gas Limit Parameter
The gasLimit
refers to the maximum amount of gas that can be consumed specifically by the contract call itself, separate from the rest of the transaction.
try {
await contract.functions
.return_context_amount()
.callParams({
forward: [10, provider.getBaseAssetId()],
gasLimit: 1,
})
.call();
} catch (e) {
console.log('error', e);
// error _FuelError: The transaction reverted with reason: "OutOfGas"
}
Call Parameter gasLimit
vs Transaction Parameter gasLimit
The call parameter gasLimit
sets the maximum gas allowed for the actual contract call, whereas the transaction parameter gasLimit
(see Transaction Parameters) sets the maximum gas allowed for the entire transaction and constrains the gasLimit
call parameter. If the call parameter gasLimit
is set to a value greater than the available transaction gas, then the entire available transaction gas will be allocated for the contract call execution.
If you don't set the gasLimit
for the call, the transaction gasLimit
will be applied.
Setting Both Parameters
You can set both call parameters and transaction parameters within the same contract function call.
const contractCallGasLimit = 4_000;
const transactionGasLimit = 100_000;
const { waitForResult } = await contract.functions
.return_context_amount()
.callParams({
forward: [10, provider.getBaseAssetId()],
gasLimit: contractCallGasLimit,
})
.txParams({
gasLimit: transactionGasLimit,
})
.call();