The parameters for a contract call are:
You can use these to forward coins to a contract. You can configure these parameters by creating an instance of CallParameters
and passing it to a chain method called call_params
.
For instance, suppose the following contract that uses Sway's msg_amount()
to return the amount sent in that transaction.
Then, in Rust, after setting up and deploying the above contract, you can configure the amount being sent in the transaction like this:
let contract_methods = MyContract::new(contract_id, wallet.clone()).methods();
let tx_params = TxParameters::default();
// Forward 1_000_000 coin amount of base asset_id
// this is a big number for checking that amount can be a u64
let call_params = CallParameters::default().with_amount(1_000_000);
let response = contract_methods
.get_msg_amount() // Our contract method.
.tx_params(tx_params) // Chain the tx params setting method.
.call_params(call_params)? // Chain the call params setting method.
.call() // Perform the contract call.
.await?;
call_params
returns a result to ensure you don't forward assets to a contract method that isn't payable.
In the following example, we try to forward an amount of 100 of the base asset to non_payable
. As its name suggests, non_payable
isn't annotated with #[payable]
in the contract code. Passing CallParameters
with an amount other than 0 leads to an InvalidCallParameters
error:
Note: forwarding gas to a contract call is always possible, regardless of the contract method being non-payable.
You can also use CallParameters::default()
to use the default values:
This way:
let response = contract_methods
.initialize_counter(42)
.call_params(CallParameters::default())?
.call()
.await?;
The gas_forwarded
parameter defines the limit for the actual contract call as opposed to the gas limit for the whole transaction. This means that it is constrained by the transaction limit. If it is set to an amount greater than the available gas, all available gas will be forwarded.
// Set the transaction `gas_limit` to 1_000_000 and `gas_forwarded` to 4300 to specify that
// the contract call transaction may consume up to 1_000_000 gas, while the actual call may
// only use 4300 gas
let tx_params = TxParameters::default().with_gas_limit(1_000_000);
let call_params = CallParameters::default().with_gas_forwarded(4300);
let response = contract_methods
.get_msg_amount() // Our contract method.
.tx_params(tx_params) // Chain the tx params setting method.
.call_params(call_params)? // Chain the call params setting method.
.call() // Perform the contract call.
.await?;
If you don't set the call parameters or use CallParameters::default()
, the transaction gas limit will be forwarded instead.
Was this page helpful?