Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add native param types page #1870

Merged
merged 11 commits into from
Mar 13, 2024
2 changes: 2 additions & 0 deletions .changeset/thick-points-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
91 changes: 91 additions & 0 deletions apps/docs-snippets/src/guide/types/contract-types.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { Address, type Contract } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject } from '../../utils';

/**
* @group node
*/

describe(__filename, () => {
let contract: Contract;

beforeAll(async () => {
contract = await createAndDeployContractFromProject(DocSnippetProjectsEnum.INPUT_OUTPUT_TYPES);
});

it('should successfully call a function with an Address type input and output parameters', async () => {
// #region address-input
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved
// #import { Address };
const address = Address.fromRandom();
const addressInput = { value: address.toB256() };
// #endregion address-input
const callResponse = await contract.functions.address(addressInput).simulate();

// #region address-output
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved
const addressOutput = callResponse.value;
const addressFromOutput: Address = Address.fromB256(addressOutput.value);
// #endregion address-output
expect(addressFromOutput).toEqual(address);
});

it('should successfully call a function with a ContractId type input and output parameters', async () => {
// #region contract-id-input
const contractId = '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec';
const contractIdInput = { value: contractId.toString() };
// #endregion contract-id-input
const callResponse = await contract.functions.contract_id(contractIdInput).simulate();

// #region contract-id-output
const contractIdOutput = callResponse.value;
const contractIdFromOutput: string = contractIdOutput.value;
// #endregion contract-id-output

expect(contractIdFromOutput).toEqual(contractId);
});

it('should successfully call a function with a Identity type input and output parameters', async () => {
// #region identity-address-input
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved
const address = Address.fromRandom();
const addressInput = { value: address.toB256() };
const addressIdentityInput = { Address: addressInput };
// #endregion identity-address-input
const callResponse1 = await contract.functions.identity(addressIdentityInput).simulate();

// #region identity-address-output
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved
const identityFromOutput1 = callResponse1.value;
const addressStringFromOutput = identityFromOutput1.Address.value;
const addressFromOutput: Address = Address.fromB256(addressStringFromOutput);
// #endregion identity-address-output

// #region identity-contract-input
const contractId = '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec';
const contractIdInput = { value: contractId.toString() };
const contractIdentityInput = { ContractId: contractIdInput };
// #endregion identity-contract-input
const callResponse2 = await contract.functions.identity(contractIdentityInput).simulate();

// #region identity-contract-output
const identityFromOutput2 = callResponse2.value;
const contractIdFromOutput: string = identityFromOutput2.ContractId.value;
// #endregion identity-contract-output

expect(addressFromOutput).toEqual(address);
expect(contractIdFromOutput).toEqual(contractId);
});

it('should successfully call a function with an AssetId type input and output parameters', async () => {
// #region asset-id-input
const assetId = '0x0cfabde7bbe58d253cf3103d8f55d26987b3dc4691205b9299ac6826c613a2e2';
const assetIdInput = { value: assetId };
// #endregion asset-id-input
const callResponse = await contract.functions.asset_id(assetIdInput).simulate();

// #region asset-id-output
const assetIdOutput = callResponse.value;
const assetIdFromOutput: string = assetIdOutput.value;
// #endregion asset-id-output

expect(assetIdFromOutput).toEqual(assetId);
});
});
1 change: 1 addition & 0 deletions apps/docs-snippets/test/fixtures/forc-projects/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ members = [
"token",
"echo-raw-slice",
"echo-std-string",
"input-output-types",
]
1 change: 1 addition & 0 deletions apps/docs-snippets/test/fixtures/forc-projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export enum DocSnippetProjectsEnum {
ECHO_STD_STRING = 'echo-std-string',
ECHO_ASSET_ID = 'echo-asset-id',
SCRIPT_TRANSFER_TO_CONTRACT = 'script-transfer-to-contract',
INPUT_OUTPUT_TYPES = 'input-output-types',
}

export const getDocsSnippetsForcProject = (project: DocSnippetProjectsEnum) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
name = "input-output-types"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
contract;

use std::bytes::Bytes;

abi InputOutputTypes {
fn address(address: Address) -> Address;

fn contract_id(contract_id: ContractId) -> ContractId;

fn identity(identity: Identity) -> Identity;

fn asset_id(asset_id: AssetId) -> AssetId;
}

impl InputOutputTypes for Contract {
fn address(address: Address) -> Address {
address
}

fn contract_id(contract_id: ContractId) -> ContractId {
contract_id
}

fn identity(identity: Identity) -> Identity {
identity
}

fn asset_id(asset_id: AssetId) -> AssetId {
asset_id
}
}
4 changes: 4 additions & 0 deletions apps/docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ export default defineConfig({
text: 'Date Time',
link: '/guide/types/date-time',
},
{
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved
text: 'Native Parameters',
link: '/guide/types/native-parameters',
},
],
},
{
Expand Down
76 changes: 76 additions & 0 deletions apps/docs/src/guide/types/native-parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Native Parameter Types

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

- [`Address`](#address)
- [`ContractId`](#contractid)
- [`Identity`](#identity)
- [`AssetId`](#assetid)

## `Address`

### `AddressInput`

To pass an `Address` as an input parameter to a contract, you can define the input as shown below:
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#address-input

### `AddressOutput`

For a contract call that returns an `Address` type in Sway, you can convert to an `Address` type in `fuels` as shown below:
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#address-output

## `ContractId`

### `ContractIdInput`

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

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#contract-id-input

### `ContractIdOutput`

For a contract call that returns a `ContractId` type in Sway, you can convert to an `string` as shown below:
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#contract-id-output

## `Identity`

### `IdentityInput`

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

For an address:

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-address-input

For a contract:

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-contract-input

### `IdentityOutput`

For a contract call that returns an `Identity` type in Sway, you can convert to an `Address` or `string` as shown below:
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved

For an address:

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-address-output

For a contract:

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-contract-output

## `AssetId`

### `AssetIdInput`

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

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#asset-id-input

### `AssetIdOutput`

For a contract call that returns an `AssetId` type in Sway, you can convert to a `string` as shown below:
sarahschwartz marked this conversation as resolved.
Show resolved Hide resolved

<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#asset-id-output
Loading