Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Variable Outputs

Icon LinkVariable Outputs

Sway includes robust functions for transferring assets to wallets and contracts.

When using these transfer functions within your Sway projects, it is important to be aware that each call will require an Output Variable Icon Link within the Outputs Icon Link of the transaction.

For instance, if a contract function calls a Sway transfer function 3 times, it will require 3 Output Variables present within the list of outputs in your transaction.

Icon LinkExample: Sway's built-in functions that requires Output Variable

fn transfer_to_address(recipient: Address, asset_id: AssetId, amount: u64) {
    transfer_to_address(recipient, asset_id, amount);
}
 
fn transfer_to_contract(target: ContractId, asset_id: AssetId, amount: u64) {
    force_transfer_to_contract(target, asset_id, amount);
}
 
fn mint_coins(sub_id: b256, mint_amount: u64) {
    mint(sub_id, mint_amount);
}
 
fn burn_coins(sub_id: b256, burn_amount: u64) {
    burn(sub_id, burn_amount);
}
Icon InfoCircle

Note: Functions like mint and burn also requires an Output Variable for each call, as they internally execute the transfer function.

Icon LinkAdding Variable Outputs to the contract call

When your contract invokes any of these functions, or if it calls a function that leads to another contract invoking these functions, you need to add the appropriate number of Output Variables.

This can be done as shown in the following example:

const { minGasPrice } = provider.getGasConfig();
 
const { transactionResult } = await contract.functions
  .increment_count(15)
  .txParams({
    gasLimit: 10_000,
    variableOutputs: 1,
  })
  .call();

In the TypeScript SDK, the Output Variables are automatically added to the transaction's list of outputs.

This process is done by a brute-force strategy, performing sequential dry runs until no errors are returned. This method identifies the number of Output Variables required to process the transaction.

However, this can significantly delay the transaction processing. Therefore, it is highly recommended to manually add the correct number of Output Variables before submitting the transaction.