⌘K

Icon SunFilledIcon MoonStars
Variable Outputs

Icon LinkVariable Outputs

You may need to send funds to the transaction output in certain scenarios. Sway provides a method called transfer_to_address(coins, asset_id, recipient) that we can use for this purpose, which allows you to transfer a specific number of coins for a given asset to a recipient address.

Icon LinkExample: Using transfer_to_address in a Contract

Here's an example of a contract function that utilizes the transfer_to_address method:

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

Icon LinkUsing the SDK to Call the transfer_coins_to_output Function

With the SDK, you can call transfer_coins_to_output by chaining the txParams and adding the property variableOutputs: amount to your contract call. Like this:

const { transactionResult } = await contract.functions
	.increment_count(15)
	.txParams({
	gasPrice,
	gasLimit,
	variableOutputs: 1,
	})
	.call();

In the TypeScript SDK, the output variables are automatically added to the transaction's list of outputs. The output's amount and owner may vary based on the transaction execution.