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
const address = Address.fromRandom();
const addressInput = { bits: 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
const addressOutput = response1.value;
const addressFromOutput: Address = Address.fromB256(addressOutput.bits);
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 = { bits: contractId };
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 = response.value;
const contractIdFromOutput: string = contractIdOutput.bits;
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
const address = Address.fromRandom();
const addressInput = { bits: address.toB256() };
const addressIdentityInput = { Address: addressInput };
See code in context

For a contract:

ts
const contractId =
  '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec';
const contractIdInput = { bits: 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
const response = await contract.functions.identity(addressIdentityInput).get();

const identityFromOutput: IdentityOutput = response.value;
const addressStringFromOutput: AddressOutput =
  identityFromOutput.Address as AddressOutput;
const addressFromOutput = Address.fromB256(addressStringFromOutput.bits);
See code in context

For a contract:

ts
const response = await contract.functions.identity(contractIdentityInput).get();

const identityFromOutput2: IdentityOutput = response.value;
const contractIdOutput = identityFromOutput2.ContractId as ContractIdOutput;
const contractIdFromOutput = contractIdOutput.bits;
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 = { bits: 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 = response5.value;
const assetIdFromOutput: string = assetIdOutput.bits;
See code in context