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 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 { value } = await contract.functions
.return_context_amount()
.callParams({
forward: [amountToForward, baseAssetId],
})
.call();
expect(new BN(value).toNumber()).toBe(amountToForward);
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.
await expect(
contract.functions
.return_context_amount()
.callParams({
forward: [10, baseAssetId],
gasLimit: 1,
})
.call()
).rejects.toThrow('The transaction reverted with reason: "OutOfGas"');
gasLimit
vs Transaction Parameters gasLimit
The Call gasLimit
parameter sets the maximum gas allowed for the actual contract call, whereas the Transaction gasLimit
(see Transaction Parameters ) sets the maximum gas allowed for the entire transaction and constrains the gasLimit
for the Call. If the Call 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.
You can set both Call Parameters and Transaction Parameters within the same contract function call.
const amountToForward = 10;
const contractCallGasLimit = 4000;
const transactionGasLimit = 100_000;
const result = await contract.functions
.return_context_amount()
.callParams({
forward: [amountToForward, baseAssetId],
gasLimit: contractCallGasLimit,
})
.txParams({
gasLimit: transactionGasLimit,
})
.call();
const { value } = result;
const expectedValue = 10;
expect(new BN(value).toNumber()).toBe(expectedValue);