Skip to content

Native Parameter Types

Below you can find examples of how to convert between common native Sway program input and output types:

Address

AddressInput

To pass an Address as an input parameter to a Sway program, you can define the input as shown below:

ts
import { Address } from 'fuels';
const address = Address.fromRandom();
const addressInput = { value: address.toB256() };
See code in context

AddressOutput

For a Sway program that returns an Address type, you can convert the returned value to an Address type in fuels as shown below:

ts
import { Address } from 'fuels';
const addressOutput = callResponse.value;
const addressFromOutput: Address = Address.fromB256(addressOutput.value);
See code in context

ContractId

ContractIdInput

To pass a ContractId as an input parameter to a Sway program, you can define the input as shown below:

ts
const contractId = '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec';
const contractIdInput = { value: contractId.toString() };
See code in context

ContractIdOutput

For a Sway program that returns a ContractId type, you can convert the returned value to a string as shown below:

ts
const contractIdOutput = callResponse.value;
const contractIdFromOutput: string = contractIdOutput.value;
See code in context

Identity

IdentityInput

To pass an Identity as an input parameter to a Sway program, you can define the input as shown below:

For an address:

ts
import { Address } from 'fuels';
const address = Address.fromRandom();
const addressInput = { value: address.toB256() };
const addressIdentityInput = { Address: addressInput };
See code in context

For a contract:

ts
const contractId = '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec';
const contractIdInput = { value: contractId.toString() };
const contractIdentityInput = { ContractId: contractIdInput };
See code in context

IdentityOutput

For a Sway program that returns an Identity type, you can convert the returned value to an Address or string as shown below:

For an address:

ts
import { Address } from 'fuels';
const identityFromOutput1 = callResponse1.value;
const addressStringFromOutput = identityFromOutput1.Address.value;
const addressFromOutput: Address = Address.fromB256(addressStringFromOutput);
See code in context

For a contract:

ts
const identityFromOutput2 = callResponse2.value;
const contractIdFromOutput: string = identityFromOutput2.ContractId.value;
See code in context

AssetId

AssetIdInput

To pass an AssetId as an input parameter to a Sway program, you can define the input as shown below:

ts
const assetId = '0x0cfabde7bbe58d253cf3103d8f55d26987b3dc4691205b9299ac6826c613a2e2';
const assetIdInput = { value: assetId };
See code in context

AssetIdOutput

For a Sway program that returns an AssetId type, you can convert the returned value to a string as shown below:

ts
const assetIdOutput = callResponse.value;
const assetIdFromOutput: string = assetIdOutput.value;
See code in context