Output variables

Sometimes, the contract you call might transfer funds to a specific address, depending on its execution. The underlying transaction for such a contract call has to have the appropriate number of variable outputs to succeed.

Let's say you deployed a contract with the following method:

    fn transfer_coins_to_output(coins: u64, asset_id: ContractId, recipient: Address) {
        transfer_to_address(coins, asset_id, recipient);
    }

When calling transfer_coins_to_output with the SDK, you can specify the number of variable outputs by chaining append_variable_outputs(amount) to your call. Like this:

        let address = wallet.address();

        // withdraw some tokens to wallet
        let response = contract_methods
            .transfer_coins_to_output(1_000_000, contract_id.into(), address.into())
            .append_variable_outputs(1)
            .call()
            .await?;

append_variable_outputs effectively appends a given amount of Output::Variables to the transaction's list of outputs. This output type indicates that the amount and the owner may vary based on transaction execution.

Note: that the Sway lib-std function mint_to_address calls transfer_to_address under the hood, so you need to call append_variable_outputs in the Rust SDK tests like you would for transfer_to_address.

Output messages

Similarly, when your contract transfers messages, the underlying transaction has to have the appropriate number of output messages.

Output messages can be added to a contract call by chaining append_output_messages(amount):

        let base_layer_address = Bits256([1u8; 32]);
        let amount = 1000;

        let response = contract_methods
            .send_message(base_layer_address, amount)
            .append_message_outputs(1)
            .call()
            .await?;