diff --git a/.changeset/poor-years-hang.md b/.changeset/poor-years-hang.md new file mode 100644 index 00000000000..8eca3b2a1a9 --- /dev/null +++ b/.changeset/poor-years-hang.md @@ -0,0 +1,4 @@ +--- +--- + +docs: migrate `guide/types` snippets diff --git a/.knip.json b/.knip.json index ecf7925895b..6e629a38f71 100644 --- a/.knip.json +++ b/.knip.json @@ -29,6 +29,7 @@ "eslint-plugin-react", "eslint-plugin-react-hooks", "dotenv", + "ethers", "kill", "lsof", "memfs", diff --git a/apps/docs-snippets/package.json b/apps/docs-snippets/package.json index f6178574888..58bb894667f 100644 --- a/apps/docs-snippets/package.json +++ b/apps/docs-snippets/package.json @@ -10,7 +10,6 @@ "@fuel-ts/account": "workspace:*", "@fuel-ts/errors": "workspace:*", "@fuel-ts/utils": "workspace:*", - "ethers": "^6.13.4", "fuels": "workspace:*", "vitest": "~2.0.5" }, diff --git a/apps/docs-snippets/src/guide/types/address.test.ts b/apps/docs-snippets/src/guide/types/address.test.ts deleted file mode 100644 index 0bbcea93034..00000000000 --- a/apps/docs-snippets/src/guide/types/address.test.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { Address, Wallet } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('Address Types', () => { - it('should successfully create new address from bech32 string', () => { - // #region address-2 - const ADDRESS_BECH32 = 'fuel1elnmzsav56dqnp95sx4e2pckq36cvae9ser44m5zlvgtwxw49fmqd7e42e'; - const ADDRESS_CHECKSUM = '0xcfe7b143Aca69a0984b481ab950716047586772586475AEE82fb10B719d52A76'; - - const address = new Address(ADDRESS_BECH32); - - expect(address.toString()).toEqual(ADDRESS_CHECKSUM); - expect(address.bech32Address).toEqual(ADDRESS_BECH32); - // #endregion address-2 - }); - - it('should successfully generate new address instance from public key', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - // #region address-3 - const wallet = Wallet.generate({ - provider, - }); - - const address = Address.fromPublicKey(wallet.publicKey); - - expect(address).toEqual(wallet.address); - // #endregion address-3 - }); - - it('should successfully generate new address instance from 256 bit string', () => { - // #region address-4 - const b256 = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'; - - const address = Address.fromB256(b256); - - expect(address.toB256()).toEqual(b256); - // #endregion address-4 - }); - - it('should successfully create address from Bech32 or B256 address', () => { - // #region address-5 - const address = Address.fromRandom(); - - const addressCloneFromBech = Address.fromString(address.toString()); - const addressCloneFromB256 = Address.fromString(address.toB256()); - - expect(addressCloneFromBech.equals(addressCloneFromB256)); - // #endregion address-5 - }); - - it('should successfully create address from an unknown input format', () => { - // #region address-6 - const dataFromInput: string = - '0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e'; - - // if the input string can't be resolved this will throw an error - const someAddress = Address.fromDynamicInput(dataFromInput); - - expect(someAddress).toBeTruthy(); - // #endregion address-6 - }); - - it('should successfully match address using equals method', () => { - // #region address-7 - const address = Address.fromRandom(); - - const addressCloneFromBech = Address.fromString(address.toString()); - const addressCloneFromB256 = Address.fromString(address.toB256()); - - expect(address.equals(addressCloneFromBech)).toBeTruthy(); - expect(address.equals(addressCloneFromB256)).toBeTruthy(); - // #endregion address-7 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/arrays.test.ts b/apps/docs-snippets/src/guide/types/arrays.test.ts deleted file mode 100644 index 99fbc78f175..00000000000 --- a/apps/docs-snippets/src/guide/types/arrays.test.ts +++ /dev/null @@ -1,97 +0,0 @@ -import type { BigNumberish } from 'fuels'; -import { BN } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoU64ArrayFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Arrays Types', () => { - it('should successfully demonstrate typed arrays examples', () => { - // #region arrays-1 - const numberArray: number[] = [1, 2, 3, 4, 5]; // in Sway: [u8; 5] - - const boolArray: boolean[] = [true, false, true]; // in Sway: [bool; 3] - // #endregion arrays-1 - - expect(numberArray).toHaveLength(5); - expect(boolArray).toHaveLength(3); - }); - - it('should successfully execute echo u64 array contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoU64ArrayFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - // #region arrays-2 - const u64Array: [BigNumberish, BigNumberish] = [10000000, 20000000]; - - // This expects two arguments - const { value } = await contract.functions.echo_u64_array(u64Array).simulate(); - - expect(new BN(value[0]).toNumber()).toEqual(u64Array[0]); - - expect(new BN(value[1]).toNumber()).toEqual(u64Array[1]); - // #endregion arrays-2 - }); - - it('should throw an error for array length mismatch', async () => { - let error: unknown; - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoU64ArrayFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - try { - // @ts-expect-error Will throw error because the array length is not 2 - // #region arrays-3 - // will throw error because the array length is not 2 - await contract.functions.echo_u64_array([10000000]).simulate(); - // #endregion arrays-3 - } catch (e) { - error = e; - } - - expect(error).toBeDefined(); - }); - - it('should throw an error for array type mismatch', async () => { - let error: unknown; - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoU64ArrayFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - try { - // #region arrays-4 - // will throw error because the second element is not of type u64 - await contract.functions.echo_u64_array([10000000, 'a']).simulate(); - // #endregion arrays-4 - } catch (e) { - error = e; - } - - expect(error).toBeDefined(); - }); -}); diff --git a/apps/docs-snippets/src/guide/types/asset-id.test.ts b/apps/docs-snippets/src/guide/types/asset-id.test.ts deleted file mode 100644 index c87bb4818f1..00000000000 --- a/apps/docs-snippets/src/guide/types/asset-id.test.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { Address } from 'fuels'; -import type { AssetId, B256Address } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoAssetIdFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('AssetId', () => { - const Bits256: B256Address = '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c'; - - it('should demonstrate typed asset id example', () => { - // #region asset-id-1 - // #import { AssetId }; - - const assetId: AssetId = { - bits: Bits256, - }; - // #endregion asset-id-1 - - expect(assetId.bits).toBe(Bits256); - }); - - it('should create an AssetId from a B256Address', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoAssetIdFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region asset-id-2 - // #import { AssetId }; - - const b256Address = '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c'; - - const address = Address.fromB256(b256Address); - - const assetId: AssetId = address.toAssetId(); - // #endregion asset-id-2 - - const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate(); - - expect(value).toBeTruthy(); - }); - - it('should pass an asset id to a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoAssetIdFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region asset-id-3 - // #import { AssetId }; - - const assetId: AssetId = { - bits: Bits256, - }; - - const { value } = await contract.functions.echo_asset_id_comparison(assetId).simulate(); - - expect(value).toBeTruthy(); - // #endregion asset-id-3 - }); - - it('should retrieve an asset id from a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoAssetIdFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region asset-id-4 - // #import { AssetId }; - - const assetId: AssetId = { - bits: Bits256, - }; - - const { value } = await contract.functions.echo_asset_id().simulate(); - - expect(value).toEqual(assetId); - // #endregion asset-id-4 - - expect(value.bits).toEqual(Bits256); - }); -}); diff --git a/apps/docs-snippets/src/guide/types/bech32.test.ts b/apps/docs-snippets/src/guide/types/bech32.test.ts deleted file mode 100644 index 355e845338a..00000000000 --- a/apps/docs-snippets/src/guide/types/bech32.test.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { Address } from 'fuels'; - -/** - * @group node - * @group browser - */ -describe('Bech32 Types', () => { - it('should successfully generate a bech32 address', () => { - // #region bech32-2 - const address = Address.fromRandom(); - - // #context console.log(address.bech32Address); - - // fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs - // #endregion bech32-2 - - // #region addresses-1 - const bech32 = 'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs'; - // #endregion addresses-1 - expect(address.toAddress()).toMatch(/^fuel1/); - expect(bech32).toEqual('fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs'); - }); -}); diff --git a/apps/docs-snippets/src/guide/types/bits256.test.ts b/apps/docs-snippets/src/guide/types/bits256.test.ts deleted file mode 100644 index 3ef2f88b996..00000000000 --- a/apps/docs-snippets/src/guide/types/bits256.test.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { Address, arrayify, getRandomB256, hexlify } from 'fuels'; - -/** - * @group node - * @group browser - */ -describe('Bits256 Types', () => { - it('should successfully generate and validate bit256 hexed string', () => { - // #region bits256-1 - // #import { getRandomB256 }; - - // randomB256 is a hexlified string representing a 256-bit value - const randomB256: string = getRandomB256(); - // 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 - // #endregion bits256-1 - - // #region addresses-2 - const bits256 = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'; - // #endregion addresses-2 - - expect(typeof randomB256 === 'string').toBeTruthy(); - expect(bits256).toBe('0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'); - }); - - it('should successfully convert between hexed b256 and Uint8Array', () => { - // #region bits256-2 - // #import { arrayify, hexlify, getRandomB256 }; - - const randomB256: string = getRandomB256(); - - // convert to Uint8Array - const uint8Arr = arrayify(randomB256); - - // convert back to hexlified string - const hexedB256 = hexlify(uint8Arr); - // #endregion bits256-2 - - expect(uint8Arr.length).toBe(32); - expect(hexedB256).toEqual(randomB256); - }); - - it('should successfully generate an address from a b256 string', () => { - // #region bits256-3 - // #import { Address, getRandomB256 }; - - const randomB256: string = getRandomB256(); - - const address = Address.fromB256(randomB256); - - expect(address.toB256()).toEqual(randomB256); - // #endregion bits256-3 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/bits512.test.ts b/apps/docs-snippets/src/guide/types/bits512.test.ts deleted file mode 100644 index b8ef6810356..00000000000 --- a/apps/docs-snippets/src/guide/types/bits512.test.ts +++ /dev/null @@ -1,51 +0,0 @@ -import { Wallet } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Bits512 Types', () => { - it('should successfully call contract function and validate b512', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region bits512-1 - // #context pub struct B512 { - // #context bytes: [b256; 2], - // #context } - // #endregion bits512-1 - - const provider = contract.provider; - - // #region bits512-2 - // #import { Wallet }; - - const wallet = Wallet.generate({ - provider, - }); - - // #context console.log(walllet.publicKey); - - // 0x97e3a666e4cd34b6b3cf778ef5ec617de4439b68f7a629245442a1fece7713094a1cb0aa7ad0ac253ca1ea47d4618f9090b2a881e829e091fb2c426763e94cca - // #endregion bits512-2 - // #region bits512-4 - const b512 = wallet.publicKey; - - const { value } = await contract.functions.echo_b512(b512).simulate(); - - expect(value).toEqual(b512); - // #endregion bits512-4 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/bytes.test.ts b/apps/docs-snippets/src/guide/types/bytes.test.ts deleted file mode 100644 index 74dd37b2653..00000000000 --- a/apps/docs-snippets/src/guide/types/bytes.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { Bytes } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoBytesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Bytes', () => { - it('should pass bytes to a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoBytesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region bytes-1 - // #import { Bytes }; - - const bytes: Bytes = [40, 41, 42]; - - const { value } = await contract.functions.bytes_comparison(bytes).simulate(); - - expect(value).toBeTruthy(); - // #endregion bytes-1 - }); - - it('should retrieve bytes from a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoBytesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region bytes-2 - // #import { Bytes }; - - const bytes: Bytes = [8, 42, 77]; - - const { value } = await contract.functions.echo_bytes(bytes).simulate(); - - expect(value).toStrictEqual(new Uint8Array(bytes)); - // #endregion bytes-2 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/bytes32.test.ts b/apps/docs-snippets/src/guide/types/bytes32.test.ts deleted file mode 100644 index a0be7313d38..00000000000 --- a/apps/docs-snippets/src/guide/types/bytes32.test.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { Bytes } from 'fuels'; -import { arrayify, hexlify, randomBytes } from 'fuels'; - -/** - * @group node - * @group browser - */ -describe('Bytes32 Types', () => { - it('should successfully generate and convert byte32 to hexlified string', () => { - // #region bytes32-1 - // #region bytes32-2 - // #import { randomBytes }; - - const bytes32: Bytes = randomBytes(32); - // #endregion bytes32-1 - - const bytes32String: string = hexlify(bytes32); - - // safely pass a 32-byte array into arrayify - expect(arrayify(bytes32)).toEqual(arrayify(bytes32String)); - - // a byte32 can be safely passed into hexlify more than once - expect(bytes32String).toEqual(hexlify(bytes32String)); - // #endregion bytes32-2 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/contract-types.test.ts b/apps/docs-snippets/src/guide/types/contract-types.test.ts deleted file mode 100644 index 0d17a7f0d9e..00000000000 --- a/apps/docs-snippets/src/guide/types/contract-types.test.ts +++ /dev/null @@ -1,143 +0,0 @@ -import { Address } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { InputOutputTypesFactory } from '../../../test/typegen'; -import type { - IdentityOutput, - AddressOutput, - ContractIdOutput, -} from '../../../test/typegen/contracts/InputOutputTypes'; - -/** - * @group node - * @group browser - */ - -describe('Contract Types', () => { - it('should successfully call a function with an Address type input and output parameters', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: InputOutputTypesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region address-input - // #import { Address }; - const address = Address.fromRandom(); - const addressInput = { bits: address.toB256() }; - // #endregion address-input - const callResponse = await contract.functions.address(addressInput).simulate(); - - // #region address-output - // #import { Address }; - const addressOutput = callResponse.value; - const addressFromOutput: Address = Address.fromB256(addressOutput.bits); - // #endregion address-output - expect(addressFromOutput).toEqual(address); - }); - - it('should successfully call a function with a ContractId type input and output parameters', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: InputOutputTypesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region contract-id-input - const contractId = '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec'; - const contractIdInput = { bits: 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.bits; - // #endregion contract-id-output - - expect(contractIdFromOutput).toEqual(contractId); - }); - - it('should successfully call a function with a Identity type input and output parameters', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: InputOutputTypesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region identity-address-input - // #import { Address }; - const address = Address.fromRandom(); - const addressInput = { bits: address.toB256() }; - const addressIdentityInput = { Address: addressInput }; - // #endregion identity-address-input - const callResponse1 = await contract.functions.identity(addressIdentityInput).simulate(); - - // #region identity-address-output - // #import { Address }; - const identityFromOutput1: IdentityOutput = callResponse1.value; - const addressStringFromOutput: AddressOutput = identityFromOutput1.Address as AddressOutput; - const addressFromOutput: Address = Address.fromB256(addressStringFromOutput.bits); - // #endregion identity-address-output - - // #region identity-contract-input - const contractId = '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec'; - const contractIdInput = { bits: contractId.toString() }; - const contractIdentityInput = { ContractId: contractIdInput }; - // #endregion identity-contract-input - const callResponse2 = await contract.functions.identity(contractIdentityInput).simulate(); - - // #region identity-contract-output - const identityFromOutput2: IdentityOutput = callResponse2.value; - const contractIdOutput: ContractIdOutput = identityFromOutput2.ContractId as ContractIdOutput; - const contractIdFromOutput: string = contractIdOutput.bits; - // #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 () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: InputOutputTypesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region asset-id-input - const assetId = '0x0cfabde7bbe58d253cf3103d8f55d26987b3dc4691205b9299ac6826c613a2e2'; - const assetIdInput = { bits: 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.bits; - // #endregion asset-id-output - - expect(assetIdFromOutput).toEqual(assetId); - }); -}); diff --git a/apps/docs-snippets/src/guide/types/enums.test.ts b/apps/docs-snippets/src/guide/types/enums.test.ts deleted file mode 100644 index 2d56bf0cfb4..00000000000 --- a/apps/docs-snippets/src/guide/types/enums.test.ts +++ /dev/null @@ -1,160 +0,0 @@ -import { FuelError } from 'fuels'; -import { expectToThrowFuelError, launchTestNode } from 'fuels/test-utils'; - -import { EchoEnumFactory } from '../../../test/typegen'; -import { StateErrorInput, UserErrorInput } from '../../../test/typegen/contracts/EchoEnum'; - -/** - * @group node - * @group browser - */ -describe('Enums Types', () => { - it('should successfully echo a simple enum in a contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEnumFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region simple-enum-3 - // #context import { StateErrorInput } from '../path/to/typegen/contracts/EchoEnum'; - - const enumVariant = StateErrorInput.Completed; - - const { value } = await contract.functions.echo_state_error_enum(enumVariant).simulate(); - - expect(value).toEqual(enumVariant); - // #endregion simple-enum-3 - }); - - it('should successfully echo a enum in a contract call (UserError Enum)', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEnumFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region enum-of-enums-3 - // #context import { UserErrorInput } from '../path/to/typegen/contracts/EchoEnum'; - const enumParam = { UserError: UserErrorInput.InsufficientPermissions }; - - const { value } = await contract.functions.echo_error_enum(enumParam).simulate(); - - expect(value).toEqual(enumParam); - // #endregion enum-of-enums-3 - }); - - it('should successfully echo a enum in a contract call (StateError Enum)', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEnumFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region enum-of-enums-4 - // #context import { StateErrorInput } from '../path/to/typegen/contracts/EchoEnum'; - - const enumParam = { StateError: StateErrorInput.Completed }; - - const { value } = await contract.functions.echo_error_enum(enumParam).simulate(); - - expect(value).toEqual(enumParam); - // #endregion enum-of-enums-4 - }); - - it('should throw when enum value is not the correct type', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEnumFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region enum-error-mismatch-type - // Valid types: string - const emumValue: number = 1; - - await expectToThrowFuelError( - // @ts-expect-error number is not a valid type - () => contract.functions.echo_state_error_enum(emumValue).simulate(), - new FuelError(FuelError.CODES.INVALID_DECODE_VALUE, 'A field for the case must be provided.') - ); - // #endregion enum-error-mismatch-type - }); - - it('should throw when enum value is not present in Sway enum values', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEnumFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region enum-error-value-mismatch - // Valid values: 'Void', 'Pending', 'Completed' - const emumValue = 'NotStateEnumValue'; - - await expectToThrowFuelError( - // @ts-expect-error NotStateEnumValue is not a valid value - () => contract.functions.echo_state_error_enum(emumValue).simulate(), - new FuelError(FuelError.CODES.INVALID_DECODE_VALUE, 'Only one field must be provided.') - ); - // #endregion enum-error-value-mismatch - }); - - it('should throw when using incorrect key for enum of enums', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEnumFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region enum-error-case-key-mismatch - // Valid case keys: 'StateError', 'UserError' - const enumParam = { UnknownKey: 'Completed' }; - - await expectToThrowFuelError( - // @ts-expect-error UnknownKey is not a valid key - () => contract.functions.echo_error_enum(enumParam).simulate(), - new FuelError( - FuelError.CODES.INVALID_DECODE_VALUE, - `Invalid case 'UnknownKey'. Valid cases: 'StateError', 'UserError'.` - ) - ); - // #endregion enum-error-case-key-mismatch - }); -}); diff --git a/apps/docs-snippets/src/guide/types/evm-address.test.ts b/apps/docs-snippets/src/guide/types/evm-address.test.ts deleted file mode 100644 index 2ec443f056c..00000000000 --- a/apps/docs-snippets/src/guide/types/evm-address.test.ts +++ /dev/null @@ -1,117 +0,0 @@ -import type { EvmAddress, B256AddressEvm } from 'fuels'; -import { Address } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoEvmAddressFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('EvMAddress', () => { - const Bits256: B256AddressEvm = - '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6'; - - it('should demonstrate typed evm address example', () => { - // #region evm-address-1 - // #import { EvmAddress }; - - const evmAddress: EvmAddress = { - bits: Bits256, - }; - // #endregion evm-address-1 - - // #region addresses-3 - // #import { EvmAddress }; - - const address: EvmAddress = { - bits: '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6', - }; - // #endregion addresses-3 - - expect(evmAddress.bits).toBe(Bits256); - expect(address.bits).toBe('0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6'); - }); - - it('should create an Evm Address from a B256Address', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEvmAddressFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region evm-address-2 - // #import { EvmAddress, Address }; - - const b256Address = '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'; - - const address = Address.fromB256(b256Address); - - const evmAddress: EvmAddress = address.toEvmAddress(); - // #endregion evm-address-2 - - const { value } = await contract.functions.echo_address_comparison(evmAddress).simulate(); - - expect(value).toBeTruthy(); - }); - - it('should pass an evm address to a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEvmAddressFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region evm-address-3 - // #import { EvmAddress }; - - const evmAddress: EvmAddress = { - bits: Bits256, - }; - - const { value } = await contract.functions.echo_address_comparison(evmAddress).simulate(); - - expect(value).toBeTruthy(); - // #endregion evm-address-3 - }); - - it('should retrieve an evm address from a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEvmAddressFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region evm-address-4 - // #import { EvmAddress }; - - const evmAddress: EvmAddress = { - bits: Bits256, - }; - - const { value } = await contract.functions.echo_address().simulate(); - - expect(value).toEqual(evmAddress); - // #endregion evm-address-4 - - expect(value.bits).toEqual(Bits256); - }); -}); diff --git a/apps/docs-snippets/src/guide/types/numbers.test.ts b/apps/docs-snippets/src/guide/types/numbers.test.ts deleted file mode 100644 index 2e3dd0fcfd6..00000000000 --- a/apps/docs-snippets/src/guide/types/numbers.test.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { toBigInt } from 'ethers'; -import { bn } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Numbers Types', () => { - test('should successfully create new Sway-compatible BigNumber from a JavaScript number', () => { - // #region numbers-docs-1 - // #context import { bn } from 'fuels'; - - const originalNumber = 20; - - const bigNumber = bn(originalNumber); - - expect(bigNumber.toNumber()).toEqual(originalNumber); - // #endregion numbers-docs-1 - }); - - test('should successfully create new Sway-compatible BigNumber from a string', () => { - // #region numbers-docs-2 - // #context import { bn } from 'fuels'; - - const originalNumber = '9007199254740992'; - - const bigNumber = bn(originalNumber); - - expect(bigNumber.toString()).toEqual(originalNumber); - // #endregion numbers-docs-2 - }); - - test('should successfully pass in and read a number to/from a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region numbers-docs-3 - const originalNumber = 20; - - const { waitForResult } = await contract.functions.echo_u64(bn(originalNumber)).call(); - const { value } = await waitForResult(); - - expect(value.toNumber()).toEqual(originalNumber); - // #endregion numbers-docs-3 - }); - - test('should successfully pass in and read a number to/from a contract - small numbers', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region numbers-docs-4 - const originalNumber = 20; - - const { waitForResult } = await contract.functions.echo_u8(originalNumber).call(); - const { value } = await waitForResult(); - - expect(value).toEqual(originalNumber); - // #endregion numbers-docs-4 - }); - - test('ethers -> fuels BigNum conversion', () => { - // #region numbers-docs-5 - // #context import { toBigInt } from 'ethers'; - // #context import { bn } from 'fuels'; - - const originalNumber = 20; - - const ethersBigNum = toBigInt(originalNumber); - - const fuelsBigNum = bn(ethersBigNum.toString()); - - expect(fuelsBigNum.toNumber()).toEqual(originalNumber); - // #endregion numbers-docs-5 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/options.test.ts b/apps/docs-snippets/src/guide/types/options.test.ts deleted file mode 100644 index 73c53b15a4e..00000000000 --- a/apps/docs-snippets/src/guide/types/options.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { launchTestNode } from 'fuels/test-utils'; - -import { SumOptionU8Factory } from '../../../test/typegen'; - -function setupContract() { - return launchTestNode({ - contractsConfigs: [{ factory: SumOptionU8Factory }], - }); -} - -/** - * @group node - * @group browser - */ -describe('options', () => { - it('should successfully execute contract call to sum 2 option inputs (2 INPUTS)', async () => { - using launched = await setupContract(); - const { - contracts: [contract], - } = launched; - - // #region options-1 - // Sway Option - // #region options-3 - const input1: number | undefined = 10; - // #endregion options-1 - - const input2: number | undefined = 5; - - const { value } = await contract.functions.sum_optional_u8(input1, input2).simulate(); - - expect(value).toEqual(input1 + input2); - // #endregion options-3 - }); - - it('should successfully execute contract call to sum 2 option inputs (1 INPUT)', async () => { - using launched = await setupContract(); - const { - contracts: [contract], - } = launched; - - // #region options-4 - const input: number | undefined = 5; - - const { value } = await contract.functions.sum_optional_u8(input).simulate(); - - expect(value).toEqual(input); - // #endregion options-4 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/raw-slice.test.ts b/apps/docs-snippets/src/guide/types/raw-slice.test.ts deleted file mode 100644 index 658780119f7..00000000000 --- a/apps/docs-snippets/src/guide/types/raw-slice.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { RawSlice } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoRawSliceFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('RawSlice', () => { - it('should pass a raw slice to a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoRawSliceFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region raw-slice-1 - // #import { RawSlice }; - - const rawSlice: RawSlice = [40, 41, 42]; - - const { value } = await contract.functions.raw_slice_comparison(rawSlice).simulate(); - - expect(value).toBeTruthy(); - // #endregion raw-slice-1 - }); - - it('should retrieve a raw slice from a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoRawSliceFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region raw-slice-2 - // #import { RawSlice }; - - const rawSlice: RawSlice = [8, 42, 77]; - - const { value } = await contract.functions.echo_raw_slice(rawSlice).simulate(); - - expect(value).toStrictEqual(rawSlice); - // #endregion raw-slice-2 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/std-string.test.ts b/apps/docs-snippets/src/guide/types/std-string.test.ts deleted file mode 100644 index cf33a4b24e9..00000000000 --- a/apps/docs-snippets/src/guide/types/std-string.test.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { StdString } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoStdStringFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('StdString', () => { - it('should pass a std string to a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoStdStringFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region std-string-1 - // #import { StdString }; - - const stdString: StdString = 'Hello World'; - - const { value } = await contract.functions.string_comparison(stdString).simulate(); - - expect(value).toBeTruthy(); - // #endregion std-string-1 - }); - - it('should retrieve a std string from a contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoStdStringFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region std-string-2 - // #import { StdString }; - - const stdString: StdString = 'Hello Fuel'; - - const { value } = await contract.functions.echo_string(stdString).simulate(); - - expect(value).toEqual(stdString); - // #endregion std-string-2 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/string.test.ts b/apps/docs-snippets/src/guide/types/string.test.ts deleted file mode 100644 index 3db1cc29bd5..00000000000 --- a/apps/docs-snippets/src/guide/types/string.test.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('String Types', () => { - it('should validate string', () => { - // #region string-1 - // Sway str[2] - const stringSize2 = 'st'; - - // Sway str[8] - const stringSize8 = 'fuel-sdk'; - // #endregion string-1 - - expect(stringSize2.length).toBe(2); - expect(stringSize8.length).toBe(8); - }); - - it('should successfully execute and validate echoed 8 contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region string-2 - const { value } = await contract.functions.echo_str_8('fuel-sdk').simulate(); - - expect(value).toEqual('fuel-sdk'); - // #endregion string-2 - }); - - it('will throw given an input string that is too long or too short', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region string-3 - const longString = 'fuel-sdk-WILL-THROW-ERROR'; - - await expect(async () => contract.functions.echo_str_8(longString).call()).rejects.toThrowError( - 'Value length mismatch during encode' - ); - - const shortString = 'THROWS'; - - await expect(async () => - contract.functions.echo_str_8(shortString).call() - ).rejects.toThrowError('Value length mismatch during encode'); - // #endregion string-3 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/struct.test.ts b/apps/docs-snippets/src/guide/types/struct.test.ts deleted file mode 100644 index 46a5cae7787..00000000000 --- a/apps/docs-snippets/src/guide/types/struct.test.ts +++ /dev/null @@ -1,29 +0,0 @@ -/** - * @group node - * @group browser - */ -describe('Struct Types', () => { - it('should successfully validate struct representation', () => { - // #region struct-2 - type EmployeeDataStruct = { - name: string; - age: number; - salary: number; - idHash: string; - ratings: number[]; - isActive: boolean; - }; - - const data: EmployeeDataStruct = { - name: 'John Doe', - age: 30, - salary: 100_000, - idHash: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', - ratings: [4, 5, 5], - isActive: true, - }; - // #endregion struct-2 - - expect(data).toBeDefined(); - }); -}); diff --git a/apps/docs-snippets/src/guide/types/tuples.test.ts b/apps/docs-snippets/src/guide/types/tuples.test.ts deleted file mode 100644 index 4dc16d49236..00000000000 --- a/apps/docs-snippets/src/guide/types/tuples.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { BN } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Tuples Types', () => { - it('should successfully echo tuple in a contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region tuples-1 - // Sway let tuple2: (u8, bool, u64) = (100, false, 10000); - // #region tuples-3 - const tuple: [number, boolean, number] = [100, false, 10000]; - // #endregion tuples-1 - - const { value } = await contract.functions.echo_tuple(tuple).simulate(); - - expect(tuple).toEqual([value[0], value[1], new BN(value[2]).toNumber()]); - // #endregion tuples-3 - }); -}); diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts deleted file mode 100644 index c8e3b2b5f4f..00000000000 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ /dev/null @@ -1,95 +0,0 @@ -import { BN, arrayify, getRandomB256 } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { BytecodeInputFactory, EchoEmployeeDataVectorFactory } from '../../../test/typegen'; -import type { EmployeeDataInput } from '../../../test/typegen/contracts/EchoU64Array'; - -/** - * @group node - * @group browser - */ -describe('Vector Types', () => { - it('should successfully execute and validate contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoEmployeeDataVectorFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region vector-1 - // Sway Vec - // #context const basicU8Vector = [1, 2, 3]; - // #endregion vector-1 - - // #region vector-4 - // #import { getRandomB256 }; - // #context import { EmployeeDataInput } from '../path/to/typegen/contracts/EchoU64Array'; - - const employees: EmployeeDataInput[] = [ - { - name: 'John Doe', - age: 30, - salary: 8000, - idHash: getRandomB256(), - ratings: [1, 2, 3], - isActive: true, - }, - { - name: 'Everyman', - age: 31, - salary: 9000, - idHash: getRandomB256(), - ratings: [5, 6, 7], - isActive: true, - }, - ]; - const { value } = await contract.functions.echo_last_employee_data(employees).simulate(); - // #endregion vector-4 - expect(value.name).toEqual(employees[1].name); - expect(value.age).toEqual(employees[1].age); - expect(new BN(value.salary).toNumber()).toEqual(employees[1].salary); - expect(value.idHash).toEqual(employees[1].idHash); - expect(value.ratings).toEqual(employees[1].ratings); - expect(value.isActive).toEqual(employees[1].isActive); - }); - - it( - 'should successfully execute a contract call with a bytecode input', - async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: BytecodeInputFactory, - }, - ], - }); - - const { - contracts: [bytecodeContract], - } = launched; - - // #region vector-bytecode-input-ts - // #import { arrayify }; - // #context import { BytecodeInputFactory } from '../path/to/typegen'; - - const bytecodeAsVecU8 = Array.from(arrayify(BytecodeInputFactory.bytecode)); - - const { waitForResult } = await bytecodeContract.functions - .compute_bytecode_root(bytecodeAsVecU8) - .call(); - - const { value: bytecodeRoot } = await waitForResult(); - // #endregion vector-bytecode-input-ts - - expect(bytecodeRoot).toBeDefined(); - expect(bytecodeRoot.length).toBe(66); - }, - { timeout: 10000 } - ); -}); diff --git a/apps/docs-snippets2/package.json b/apps/docs-snippets2/package.json index 1cf4a23cad3..e5c056a8fd0 100644 --- a/apps/docs-snippets2/package.json +++ b/apps/docs-snippets2/package.json @@ -11,7 +11,8 @@ "test": "cd ../.. && pnpm run test:filter apps/docs-snippets2" }, "dependencies": { - "fuels": "workspace:*" + "fuels": "workspace:*", + "ethers": "^6.13.2" }, "devDependencies": { "glob": "^10.4.5" diff --git a/apps/docs-snippets2/scripts/test-template.ts b/apps/docs-snippets2/scripts/test-template.ts index 95513d3ac3c..b4d542f747f 100644 --- a/apps/docs-snippets2/scripts/test-template.ts +++ b/apps/docs-snippets2/scripts/test-template.ts @@ -1,7 +1,9 @@ /* eslint-disable no-global-assign */ +/* eslint-disable @typescript-eslint/ban-ts-comment */ // %IMPORTS% +// @ts-ignore global assignment const consoleBkp = { ...console }; afterAll(() => { diff --git a/apps/docs-snippets2/scripts/wrap-snippets.ts b/apps/docs-snippets2/scripts/wrap-snippets.ts index 4f2db820032..b01c4f7b6d9 100644 --- a/apps/docs-snippets2/scripts/wrap-snippets.ts +++ b/apps/docs-snippets2/scripts/wrap-snippets.ts @@ -27,7 +27,7 @@ export const wrapSnippet = (filepath: string) => { /* Removes .env file import */ - const envImportReg = /\nimport.+\{.+([\s\S]+).+\}.+from.+'\.\.\/env';/gm; + const envImportReg = /\nimport\s*\{([^}]+)\}\s*from\s*['"](\.\.\/)+env['"];/gm; if (envImportReg.test(imports)) { const allImports = imports.match(envImportReg)?.[0]; const envImport = `import ${allImports?.split('import ').pop()}`; diff --git a/apps/docs-snippets2/src/types/address/creating-an-address.ts b/apps/docs-snippets2/src/types/address/creating-an-address.ts new file mode 100644 index 00000000000..bdc0642e38d --- /dev/null +++ b/apps/docs-snippets2/src/types/address/creating-an-address.ts @@ -0,0 +1,10 @@ +// #region full +import { Address } from 'fuels'; + +const ADDRESS_BECH32 = + 'fuel1elnmzsav56dqnp95sx4e2pckq36cvae9ser44m5zlvgtwxw49fmqd7e42e'; + +const address = new Address(ADDRESS_BECH32); +// #endregion full + +console.log('address', address); diff --git a/apps/docs-snippets2/src/types/address/from-a-b256.ts b/apps/docs-snippets2/src/types/address/from-a-b256.ts new file mode 100644 index 00000000000..01c1050b358 --- /dev/null +++ b/apps/docs-snippets2/src/types/address/from-a-b256.ts @@ -0,0 +1,12 @@ +// #region full +import { Address } from 'fuels'; +// #region b256-1 +const b256 = + '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'; +// #endregion b256-1 + +const address = Address.fromB256(b256); + +console.log('b256', address.toB256()); +// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 +// #endregion full diff --git a/apps/docs-snippets2/src/types/address/from-a-public-key.ts b/apps/docs-snippets2/src/types/address/from-a-public-key.ts new file mode 100644 index 00000000000..a0f6a85a492 --- /dev/null +++ b/apps/docs-snippets2/src/types/address/from-a-public-key.ts @@ -0,0 +1,13 @@ +// #region full +import { Address, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const wallet = Wallet.generate({ provider }); + +const address = Address.fromPublicKey(wallet.publicKey); +// #endregion full + +console.log('address', address); diff --git a/apps/docs-snippets2/src/types/address/utilities-function-1.ts b/apps/docs-snippets2/src/types/address/utilities-function-1.ts new file mode 100644 index 00000000000..b1acbcd343a --- /dev/null +++ b/apps/docs-snippets2/src/types/address/utilities-function-1.ts @@ -0,0 +1,11 @@ +// #region full +import { Address } from 'fuels'; + +const address = Address.fromRandom(); + +const addressCloneFromBech = Address.fromString(address.toString()); +const addressCloneFromB256 = Address.fromString(address.toB256()); +// #endregion full + +console.log('addressCloneFromBech', addressCloneFromBech); +console.log('addressCloneFromB256', addressCloneFromB256); diff --git a/apps/docs-snippets2/src/types/address/utilities-function-2.ts b/apps/docs-snippets2/src/types/address/utilities-function-2.ts new file mode 100644 index 00000000000..3e6d4ba15fc --- /dev/null +++ b/apps/docs-snippets2/src/types/address/utilities-function-2.ts @@ -0,0 +1,10 @@ +// #region full +import { Address } from 'fuels'; + +const dataFromInput: string = + '0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e'; + +// If the input string can't be resolved this will throw an error +const address = Address.fromDynamicInput(dataFromInput); +// #endregion full +console.log('address', address); diff --git a/apps/docs-snippets2/src/types/address/utilities-function-3.ts b/apps/docs-snippets2/src/types/address/utilities-function-3.ts new file mode 100644 index 00000000000..bd6609f8675 --- /dev/null +++ b/apps/docs-snippets2/src/types/address/utilities-function-3.ts @@ -0,0 +1,11 @@ +// #region full +import { Address } from 'fuels'; + +const address = Address.fromRandom(); + +const address1 = Address.fromString(address.toString()); +const address2 = Address.fromString(address.toB256()); + +console.log('equals', address1.equals(address2)); +// true +// #endregion full diff --git a/apps/docs-snippets2/src/types/arrays.ts b/apps/docs-snippets2/src/types/arrays.ts new file mode 100644 index 00000000000..31163d44ba1 --- /dev/null +++ b/apps/docs-snippets2/src/types/arrays.ts @@ -0,0 +1,52 @@ +import type { BigNumberish } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoValuesFactory } from '../typegend'; + +// #region arrays-1 +// in Sway: [u8; 5] +const numberArray: number[] = [1, 2, 3, 4, 5]; + +// in Sway: [bool; 3] +const boolArray: boolean[] = [true, false, true]; +// #endregion arrays-1 + +console.log('numberArray', numberArray); +console.log('boolArray', boolArray); + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region arrays-3 +const u64Array: [BigNumberish, BigNumberish] = [10000000, 20000000]; + +const { value } = await contract.functions.echo_u64_array(u64Array).get(); + +console.log('value', value); +// [, ] +// #endregion arrays-3 + +console.log('value 1', value[0]); +console.log('value 2', value[1]); + +// #region arrays-4 +try { + // @ts-expect-error forced error + await contract.functions.echo_u64_array([10000000]).get(); +} catch (e) { + console.log('error', e); + // Types/values length mismatch. +} +// #endregion arrays-4 + +// #region arrays-5 +try { + await contract.functions.echo_u64_array([10000000, 'a']).get(); +} catch (e) { + console.log('error', e); + // Invalid u64. +} +// #endregion arrays-5 diff --git a/apps/docs-snippets2/src/types/asset-id/intro.ts b/apps/docs-snippets2/src/types/asset-id/intro.ts new file mode 100644 index 00000000000..38822f65c97 --- /dev/null +++ b/apps/docs-snippets2/src/types/asset-id/intro.ts @@ -0,0 +1,11 @@ +// #region full +import type { AssetId } from 'fuels'; +import { getRandomB256 } from 'fuels'; + +const bits256 = getRandomB256(); + +const assetId: AssetId = { + bits: bits256, +}; +// #endregion full +console.log(assetId, 'assetId'); diff --git a/apps/docs-snippets2/src/types/asset-id/using-an-asset-id-1.ts b/apps/docs-snippets2/src/types/asset-id/using-an-asset-id-1.ts new file mode 100644 index 00000000000..739f268f9a1 --- /dev/null +++ b/apps/docs-snippets2/src/types/asset-id/using-an-asset-id-1.ts @@ -0,0 +1,21 @@ +import type { AssetId } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoAssetIdFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoAssetIdFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const assetId: AssetId = { + bits: '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c', +}; + +const { value } = await contract.functions + .echo_asset_id_comparison(assetId) + .get(); +// #endregion snippet-1 +console.log('value', value); diff --git a/apps/docs-snippets2/src/types/asset-id/using-an-asset-id-2.ts b/apps/docs-snippets2/src/types/asset-id/using-an-asset-id-2.ts new file mode 100644 index 00000000000..bab24db09c9 --- /dev/null +++ b/apps/docs-snippets2/src/types/asset-id/using-an-asset-id-2.ts @@ -0,0 +1,18 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoAssetIdFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoAssetIdFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const { value } = await contract.functions.echo_asset_id().get(); + +console.log('value', value); +// const value: AssetId = { +// bits: '0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c', +// }; +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/bech32.ts b/apps/docs-snippets2/src/types/bech32.ts new file mode 100644 index 00000000000..5e3c399ec44 --- /dev/null +++ b/apps/docs-snippets2/src/types/bech32.ts @@ -0,0 +1,16 @@ +// #region bech32-2 +import { Address } from 'fuels'; + +const address = Address.fromRandom(); + +console.log('Bech32', address.bech32Address); + +// fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs +// #endregion bech32-2 + +// #region addresses-1 +const bech32 = + 'fuel1d5cfwekq78r0zq73g7eg0747etkaxxltrqx5tncm7lvg89awe3hswhqjhs'; +// #endregion addresses-1 + +console.log('bech32', bech32); diff --git a/apps/docs-snippets2/src/types/bits256/converting-between-b256-uint8.ts b/apps/docs-snippets2/src/types/bits256/converting-between-b256-uint8.ts new file mode 100644 index 00000000000..c67aa31b375 --- /dev/null +++ b/apps/docs-snippets2/src/types/bits256/converting-between-b256-uint8.ts @@ -0,0 +1,13 @@ +// #region full +import { arrayify, getRandomB256, hexlify } from 'fuels'; + +const randomB256: string = getRandomB256(); + +// Convert to Uint8Array +const uint8Arr: Uint8Array = arrayify(randomB256); + +// Convert back to hexlified string +const hexedB256: string = hexlify(uint8Arr); +// #endregion full + +console.log('hexedB256', hexedB256); diff --git a/apps/docs-snippets2/src/types/bits256/generating-random-b256.ts b/apps/docs-snippets2/src/types/bits256/generating-random-b256.ts new file mode 100644 index 00000000000..201c7916b11 --- /dev/null +++ b/apps/docs-snippets2/src/types/bits256/generating-random-b256.ts @@ -0,0 +1,9 @@ +// #region full +import { getRandomB256 } from 'fuels'; + +// b256 is a hexlified string representing a 256-bit value +const b256: string = getRandomB256(); + +console.log('b256', b256); +// 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6 +// #endregion full diff --git a/apps/docs-snippets2/src/types/bits256/support-from-address-class.ts b/apps/docs-snippets2/src/types/bits256/support-from-address-class.ts new file mode 100644 index 00000000000..7adbc198e63 --- /dev/null +++ b/apps/docs-snippets2/src/types/bits256/support-from-address-class.ts @@ -0,0 +1,9 @@ +// #region full +import { getRandomB256, Address } from 'fuels'; + +const randomB256: string = getRandomB256(); + +const address = Address.fromB256(randomB256); +// #endregion full + +console.log('address', address); diff --git a/apps/docs-snippets2/src/types/bits512/b512-in-the-sdk.ts b/apps/docs-snippets2/src/types/bits512/b512-in-the-sdk.ts new file mode 100644 index 00000000000..4df56defe63 --- /dev/null +++ b/apps/docs-snippets2/src/types/bits512/b512-in-the-sdk.ts @@ -0,0 +1,12 @@ +// #region snippet-1 +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.generate({ provider }); + +console.log('public key', wallet.publicKey); + +// 0x97e3a666e4cd34b6b3cf778ef5ec617de4439b68f7a629245442a1fece7713094a1cb0aa7ad0ac253ca1ea47d4618f9090b2a881e829e091fb2c426763e94cca +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/bits512/echoing-a-b512.ts b/apps/docs-snippets2/src/types/bits512/echoing-a-b512.ts new file mode 100644 index 00000000000..0043dba8938 --- /dev/null +++ b/apps/docs-snippets2/src/types/bits512/echoing-a-b512.ts @@ -0,0 +1,19 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoValuesFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const b512 = wallet.publicKey; + +const { value } = await contract.functions.echo_b512(b512).get(); +// #endregion snippet-1 + +console.log('value', value); diff --git a/apps/docs-snippets2/src/types/bytes.ts b/apps/docs-snippets2/src/types/bytes.ts new file mode 100644 index 00000000000..e2d43372a3f --- /dev/null +++ b/apps/docs-snippets2/src/types/bytes.ts @@ -0,0 +1,19 @@ +import type { Bytes } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoBytesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoBytesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const bytes: Bytes = [40, 41, 42]; + +const { value } = await contract.functions.echo_bytes(bytes).get(); + +console.log('value', value); +// Uint8Array(3)[40, 41, 42] +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/bytes32/converting-between-byte.ts b/apps/docs-snippets2/src/types/bytes32/converting-between-byte.ts new file mode 100644 index 00000000000..c9c3e68d8b8 --- /dev/null +++ b/apps/docs-snippets2/src/types/bytes32/converting-between-byte.ts @@ -0,0 +1,11 @@ +// #region snippet-1 +import type { Bytes } from 'fuels'; +import { arrayify, hexlify, randomBytes } from 'fuels'; + +const randomBytes32: Bytes = randomBytes(32); + +const bytes32String: string = hexlify(randomBytes32); + +const bytes32: Bytes = arrayify(bytes32String); +// #endregion snippet-1 +console.log('bytes32', bytes32); diff --git a/apps/docs-snippets2/src/types/bytes32/generating-random-bytes32.ts b/apps/docs-snippets2/src/types/bytes32/generating-random-bytes32.ts new file mode 100644 index 00000000000..53b0b8bc65c --- /dev/null +++ b/apps/docs-snippets2/src/types/bytes32/generating-random-bytes32.ts @@ -0,0 +1,7 @@ +// #region snippet-1 +import { randomBytes, type Bytes } from 'fuels'; + +const bytes32: Bytes = randomBytes(32); + +// #endregion snippet-1 +console.log('bytes32', bytes32); diff --git a/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-case.ts b/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-case.ts new file mode 100644 index 00000000000..92cd05ee20a --- /dev/null +++ b/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-case.ts @@ -0,0 +1,11 @@ +// #region snippet-1 +// Valid case keys: 'StateError', 'UserError' +const enumParam = { UnknownKey: 'Completed' }; + +try { + // @ts-expect-error UnknownKey is not a valid key + await contract.functions.echo_error_enum(enumParam).get(); +} catch (error) { + console.log('error', error); +} +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-type.ts b/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-type.ts new file mode 100644 index 00000000000..95b3f743a0d --- /dev/null +++ b/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-type.ts @@ -0,0 +1,11 @@ +// #region snippet-1 +// Valid types: string +const emumParam = 1; + +try { + // @ts-expect-error number is not a valid type + await contract.functions.echo_state_error_enum(emumParam).get(); +} catch (error) { + console.log('error', error); +} +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-value.ts b/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-value.ts new file mode 100644 index 00000000000..b183811e44a --- /dev/null +++ b/apps/docs-snippets2/src/types/enums/using-an-invalid-enum-value.ts @@ -0,0 +1,11 @@ +// #region snippet-1 +// Valid values: 'Void', 'Pending', 'Completed' +const emumParam = 'NotStateEnumValue'; + +try { + // @ts-expect-error NotStateEnumValue is not a valid value + await contract.functions.echo_state_error_enum(emumParam).get(); +} catch (error) { + console.log('error', error); +} +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/enums/using-enums-of-enums-1.ts b/apps/docs-snippets2/src/types/enums/using-enums-of-enums-1.ts new file mode 100644 index 00000000000..802edff9c0d --- /dev/null +++ b/apps/docs-snippets2/src/types/enums/using-enums-of-enums-1.ts @@ -0,0 +1,19 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoEnumFactory } from '../../typegend'; +import { UserErrorInput } from '../../typegend/contracts/EchoEnum'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoEnumFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const enumParam = { UserError: UserErrorInput.InsufficientPermissions }; + +const { value } = await contract.functions.echo_error_enum(enumParam).get(); + +console.log('value', value); +// { UserError: UserErrorInput.InsufficientPermissions } +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/enums/using-enums-of-enums-2.ts b/apps/docs-snippets2/src/types/enums/using-enums-of-enums-2.ts new file mode 100644 index 00000000000..b9db1fd95be --- /dev/null +++ b/apps/docs-snippets2/src/types/enums/using-enums-of-enums-2.ts @@ -0,0 +1,19 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoEnumFactory } from '../../typegend'; +import { StateErrorInput } from '../../typegend/contracts/EchoEnum'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoEnumFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const enumParam = { StateError: StateErrorInput.Completed }; + +const { value } = await contract.functions.echo_error_enum(enumParam).get(); + +console.log('value', value); +// { StateError: StateErrorInput.Completed } +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/enums/using-sway-enums.ts b/apps/docs-snippets2/src/types/enums/using-sway-enums.ts new file mode 100644 index 00000000000..e8778f22f7a --- /dev/null +++ b/apps/docs-snippets2/src/types/enums/using-sway-enums.ts @@ -0,0 +1,21 @@ +// #region snippet-1 +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoEnumFactory } from '../../typegend'; +import { StateErrorInput } from '../../typegend/contracts/EchoEnum'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoEnumFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +const enumParam = StateErrorInput.Completed; + +const { value } = await contract.functions + .echo_state_error_enum(enumParam) + .get(); + +console.log('value', value); +// StateErrorInput.Completed +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/evm-address/creating-an-evm.ts b/apps/docs-snippets2/src/types/evm-address/creating-an-evm.ts new file mode 100644 index 00000000000..4e655c1531d --- /dev/null +++ b/apps/docs-snippets2/src/types/evm-address/creating-an-evm.ts @@ -0,0 +1,15 @@ +// #region snippet-1 +import { Address } from 'fuels'; + +// #region snippet-2 +const b256Address = + '0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6'; +// #endregion snippet-2 + +const address = Address.fromB256(b256Address); + +const evmAddress = address.toEvmAddress(); + +console.log('evmAddress', evmAddress); +// '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6' +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/evm-address/intro.ts b/apps/docs-snippets2/src/types/evm-address/intro.ts new file mode 100644 index 00000000000..1f5c1bbe47c --- /dev/null +++ b/apps/docs-snippets2/src/types/evm-address/intro.ts @@ -0,0 +1,12 @@ +// #region snippet-1 +import type { B256AddressEvm, EvmAddress } from 'fuels'; + +const bits256: B256AddressEvm = + '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6'; + +const evmAddress: EvmAddress = { + bits: bits256, +}; +// #endregion snippet-1 + +console.log('evmAddress', evmAddress); diff --git a/apps/docs-snippets2/src/types/evm-address/using-an-evm-address-1.ts b/apps/docs-snippets2/src/types/evm-address/using-an-evm-address-1.ts new file mode 100644 index 00000000000..17144029198 --- /dev/null +++ b/apps/docs-snippets2/src/types/evm-address/using-an-evm-address-1.ts @@ -0,0 +1,23 @@ +import { Provider, Wallet, type EvmAddress } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoEvmAddressFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoEvmAddressFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +// #region snippet-2 +const evmAddress: EvmAddress = { + bits: '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6', +}; +// #endregion snippet-2 + +const { value } = await contract.functions + .echo_address_comparison(evmAddress) + .get(); +// #endregion snippet-1 + +console.log('value', value); diff --git a/apps/docs-snippets2/src/types/evm-address/using-an-evm-address-2.ts b/apps/docs-snippets2/src/types/evm-address/using-an-evm-address-2.ts new file mode 100644 index 00000000000..7f8c37ea10f --- /dev/null +++ b/apps/docs-snippets2/src/types/evm-address/using-an-evm-address-2.ts @@ -0,0 +1,16 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoEvmAddressFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoEvmAddressFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const { value } = await contract.functions.echo_address().get(); + +console.log('value', value); +// { bits: '0x000000000000000000000000210cf886ce41952316441ae4cac35f00f0e882a6' } +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/native-parameters/address.ts b/apps/docs-snippets2/src/types/native-parameters/address.ts new file mode 100644 index 00000000000..f769cf23769 --- /dev/null +++ b/apps/docs-snippets2/src/types/native-parameters/address.ts @@ -0,0 +1,23 @@ +import { Address, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { InputOutputTypesFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await InputOutputTypesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region address-input +const address = Address.fromRandom(); +const addressInput = { bits: address.toB256() }; +// #endregion address-input + +const response1 = await contract.functions.address(addressInput).get(); + +// #region address-output +const addressOutput = response1.value; +const addressFromOutput: Address = Address.fromB256(addressOutput.bits); +// #endregion address-output + +console.log('equals', addressFromOutput.equals(address)); diff --git a/apps/docs-snippets2/src/types/native-parameters/asset-id.ts b/apps/docs-snippets2/src/types/native-parameters/asset-id.ts new file mode 100644 index 00000000000..5470c011a1f --- /dev/null +++ b/apps/docs-snippets2/src/types/native-parameters/asset-id.ts @@ -0,0 +1,24 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { InputOutputTypesFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await InputOutputTypesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region asset-id-input +const assetId = + '0x0cfabde7bbe58d253cf3103d8f55d26987b3dc4691205b9299ac6826c613a2e2'; +const assetIdInput = { bits: assetId }; +// #endregion asset-id-input + +const response5 = await contract.functions.asset_id(assetIdInput).get(); + +// #region asset-id-output +const assetIdOutput = response5.value; +const assetIdFromOutput: string = assetIdOutput.bits; +// #endregion asset-id-output + +console.log('equals', assetIdFromOutput === assetId); diff --git a/apps/docs-snippets2/src/types/native-parameters/contract-id.ts b/apps/docs-snippets2/src/types/native-parameters/contract-id.ts new file mode 100644 index 00000000000..740a42de8ae --- /dev/null +++ b/apps/docs-snippets2/src/types/native-parameters/contract-id.ts @@ -0,0 +1,24 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { InputOutputTypesFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await InputOutputTypesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region contract-id-input +const contractId = + '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec'; +const contractIdInput = { bits: contractId }; +// #endregion contract-id-input + +const response = await contract.functions.contract_id(contractIdInput).get(); + +// #region contract-id-output +const contractIdOutput = response.value; +const contractIdFromOutput: string = contractIdOutput.bits; +// #endregion contract-id-output + +console.log('equals', contractIdFromOutput === contractId); diff --git a/apps/docs-snippets2/src/types/native-parameters/identity-address.ts b/apps/docs-snippets2/src/types/native-parameters/identity-address.ts new file mode 100644 index 00000000000..13b2f2e7715 --- /dev/null +++ b/apps/docs-snippets2/src/types/native-parameters/identity-address.ts @@ -0,0 +1,30 @@ +import { Address, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { InputOutputTypesFactory } from '../../typegend'; +import type { + IdentityOutput, + AddressOutput, +} from '../../typegend/contracts/InputOutputTypes'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await InputOutputTypesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region identity-address-input +const address = Address.fromRandom(); +const addressInput = { bits: address.toB256() }; +const addressIdentityInput = { Address: addressInput }; +// #endregion identity-address-input + +// #region identity-address-output +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); +// #endregion identity-address-output + +console.log('equals', addressFromOutput.equals(address)); diff --git a/apps/docs-snippets2/src/types/native-parameters/identity-contract.ts b/apps/docs-snippets2/src/types/native-parameters/identity-contract.ts new file mode 100644 index 00000000000..2408194ddaa --- /dev/null +++ b/apps/docs-snippets2/src/types/native-parameters/identity-contract.ts @@ -0,0 +1,30 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { InputOutputTypesFactory } from '../../typegend'; +import type { + ContractIdOutput, + IdentityOutput, +} from '../../typegend/contracts/InputOutputTypes'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await InputOutputTypesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region identity-contract-input +const contractId = + '0x7296ff960b5eb86b5f79aa587d7ebe1bae147c7cac046a16d062fbd7f3a753ec'; +const contractIdInput = { bits: contractId.toString() }; +const contractIdentityInput = { ContractId: contractIdInput }; +// #endregion identity-contract-input + +// #region identity-contract-output +const response = await contract.functions.identity(contractIdentityInput).get(); + +const identityFromOutput2: IdentityOutput = response.value; +const contractIdOutput = identityFromOutput2.ContractId as ContractIdOutput; +const contractIdFromOutput = contractIdOutput.bits; +// #endregion identity-contract-output + +console.log('equals', contractIdFromOutput === contractId); diff --git a/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-1.ts b/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-1.ts new file mode 100644 index 00000000000..6b6a9829549 --- /dev/null +++ b/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-1.ts @@ -0,0 +1,10 @@ +// #region snippet-1 +import { bn } from 'fuels'; + +const number: number | string = 20; + +const bigNumber = bn(number); + +console.log('equals', bigNumber.eqn(number)); +// true +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-2.ts b/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-2.ts new file mode 100644 index 00000000000..e00838191de --- /dev/null +++ b/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-2.ts @@ -0,0 +1,10 @@ +// #region snippet-1 +import { bn } from 'fuels'; + +const strNumber = '9007199254740992'; + +const bigNumber = bn(strNumber); + +console.log('equals', bigNumber.toString() === strNumber); +// true +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-3.ts b/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-3.ts new file mode 100644 index 00000000000..288b7344686 --- /dev/null +++ b/apps/docs-snippets2/src/types/numbers/for-u64-and-u256-3.ts @@ -0,0 +1,18 @@ +import { bn, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoValuesFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const bigNumber = bn('10000000000000000000'); + +const { value } = await contract.functions.echo_u64(bigNumber).get(); + +console.log('value', value.toString()); +// '10000000000000000000' +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/numbers/for-u8-u16-and-u32-1.ts b/apps/docs-snippets2/src/types/numbers/for-u8-u16-and-u32-1.ts new file mode 100644 index 00000000000..3ff56ae8eb7 --- /dev/null +++ b/apps/docs-snippets2/src/types/numbers/for-u8-u16-and-u32-1.ts @@ -0,0 +1,18 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { EchoValuesFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const number = 200; + +const { value } = await contract.functions.echo_u8(number).get(); + +console.log('value', Number(value)); +// 200 +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/numbers/for-u8-u16-and-u32-2.ts b/apps/docs-snippets2/src/types/numbers/for-u8-u16-and-u32-2.ts new file mode 100644 index 00000000000..51cf9d54fa9 --- /dev/null +++ b/apps/docs-snippets2/src/types/numbers/for-u8-u16-and-u32-2.ts @@ -0,0 +1,13 @@ +// #region snippet-1 +import { toBigInt } from 'ethers'; +import { bn } from 'fuels'; + +const number = 20; + +const ethersBigNum = toBigInt(number); + +const fuelsBigNum = bn(ethersBigNum.toString()); + +console.log('value', fuelsBigNum.toNumber()); +// 20 +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/options/example-option-u8.ts b/apps/docs-snippets2/src/types/options/example-option-u8.ts new file mode 100644 index 00000000000..6b4e68e69ae --- /dev/null +++ b/apps/docs-snippets2/src/types/options/example-option-u8.ts @@ -0,0 +1,18 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { SumOptionU8Factory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await SumOptionU8Factory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +const input: number | undefined = 10; + +const { value } = await contract.functions.sum_optional_u8(input).get(); + +console.log('value', value); +// 10 +// #endregion snippet-1 diff --git a/apps/docs-snippets2/src/types/options/overview-of-option.ts b/apps/docs-snippets2/src/types/options/overview-of-option.ts new file mode 100644 index 00000000000..95157e0a07a --- /dev/null +++ b/apps/docs-snippets2/src/types/options/overview-of-option.ts @@ -0,0 +1,22 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { SumOptionU8Factory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await SumOptionU8Factory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region snippet-1 +// Sway Option +// #region snippet-2 +const input: number | undefined = 10; +// #endregion snippet-1 +const input2: number | undefined = 5; + +const { value } = await contract.functions.sum_optional_u8(input, input2).get(); + +console.log('value', value); +// 15 +// #endregion snippet-2 diff --git a/apps/docs-snippets2/src/types/raw-slice.ts b/apps/docs-snippets2/src/types/raw-slice.ts new file mode 100644 index 00000000000..450a0baeacb --- /dev/null +++ b/apps/docs-snippets2/src/types/raw-slice.ts @@ -0,0 +1,20 @@ +// #region full +import type { RawSlice } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoRawSliceFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoRawSliceFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region raw-slice-2 +const rawSlice: RawSlice = [8, 42, 77]; + +const { value } = await contract.functions.echo_raw_slice(rawSlice).get(); +// #endregion raw-slice-2 +// #endregion full + +console.log('value', JSON.stringify(value) === JSON.stringify(rawSlice)); diff --git a/apps/docs-snippets2/src/types/std-string.ts b/apps/docs-snippets2/src/types/std-string.ts new file mode 100644 index 00000000000..5e8045f02a2 --- /dev/null +++ b/apps/docs-snippets2/src/types/std-string.ts @@ -0,0 +1,20 @@ +// #region full +import { Provider, Wallet, type StdString } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoStdStringFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoStdStringFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region std-string-2 +const stdString: StdString = 'Hello Fuel'; + +const { value } = await contract.functions.echo_string(stdString).get(); + +console.log('value', value); +// 'Hello Fuel' +// #endregion std-string-2 +// #endregion full diff --git a/apps/docs-snippets2/src/types/string.ts b/apps/docs-snippets2/src/types/string.ts new file mode 100644 index 00000000000..c80cf100f72 --- /dev/null +++ b/apps/docs-snippets2/src/types/string.ts @@ -0,0 +1,49 @@ +// #region full +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoValuesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region string-1 +// Sway str[2] +const stringSize2 = 'st'; + +// Sway str[8] +const stringSize8 = 'fuel-sdk'; +// #endregion string-1 + +// #region string-2 +const { value } = await contract.functions.echo_str_8('fuel-sdk').get(); + +console.log('value', value); +// 'fuel-sdk' +// #endregion string-2 + +// #region string-3 +const longString = 'fuel-sdk-WILL-THROW-ERROR'; + +try { + await contract.functions.echo_str_8(longString).call(); +} catch (error) { + console.log('error', error); + // Value length mismatch during encode +} + +const shortString = 'THROWS'; + +try { + await contract.functions.echo_str_8(shortString).call(); +} catch (error) { + console.log('error', error); + // Value length mismatch during encode +} +// #endregion string-3 +// #endregion full + +console.log('equals', stringSize2 === 'st'); +console.log('equals', stringSize8 === 'fuel-sdk'); diff --git a/apps/docs-snippets2/src/types/structs.ts b/apps/docs-snippets2/src/types/structs.ts new file mode 100644 index 00000000000..19ef7a9aff8 --- /dev/null +++ b/apps/docs-snippets2/src/types/structs.ts @@ -0,0 +1,21 @@ +// #region struct-2 +type EmployeeDataStruct = { + name: string; + age: number; + salary: number; + idHash: string; + ratings: number[]; + isActive: boolean; +}; + +const data: EmployeeDataStruct = { + name: 'John Doe', + age: 30, + salary: 100_000, + idHash: '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', + ratings: [4, 5, 5], + isActive: true, +}; +// #endregion struct-2 + +console.log('value', data); diff --git a/apps/docs-snippets2/src/types/tuples.ts b/apps/docs-snippets2/src/types/tuples.ts new file mode 100644 index 00000000000..986f8306013 --- /dev/null +++ b/apps/docs-snippets2/src/types/tuples.ts @@ -0,0 +1,23 @@ +// #region full +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoValuesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region tuples-1 +// Sway let tuple2: (u8, bool, u64) = (100, false, 10000); +// #region tuples-3 +const tuple: [number, boolean, number] = [100, false, 10000]; +// #endregion tuples-1 + +const { value } = await contract.functions.echo_tuple(tuple).simulate(); + +console.log('value', value); +// [100, false, ] +// #endregion tuples-3 +// #endregion full diff --git a/apps/docs-snippets2/src/types/vectors.ts b/apps/docs-snippets2/src/types/vectors.ts new file mode 100644 index 00000000000..207f9353da5 --- /dev/null +++ b/apps/docs-snippets2/src/types/vectors.ts @@ -0,0 +1,67 @@ +// #region full +import { arrayify, bn, getRandomB256, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { + BytecodeInputFactory, + EchoEmployeeDataVectorFactory, +} from '../typegend'; +import type { EmployeeDataInput } from '../typegend/contracts/EchoEmployeeDataVector'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const deploy = await EchoEmployeeDataVectorFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); +const deploy2 = await BytecodeInputFactory.deploy(wallet); +const { contract: bytecodeContract } = await deploy2.waitForResult(); + +// #region vector-1 +// Sway Vec +const basicU8Vector = [1, 2, 3]; +// #endregion vector-1 + +// #region vector-4 +const employees: EmployeeDataInput[] = [ + { + name: 'John Doe', + age: 30, + salary: bn(8000), + idHash: getRandomB256(), + ratings: [1, 2, 3], + isActive: true, + }, + { + name: 'Everyman', + age: 31, + salary: bn(9000), + idHash: getRandomB256(), + ratings: [5, 6, 7], + isActive: true, + }, +]; +const { value } = await contract.functions + .echo_last_employee_data(employees) + .simulate(); +// #endregion vector-4 + +// #region vector-bytecode-input-ts +const bytecodeAsVecU8 = Array.from(arrayify(BytecodeInputFactory.bytecode)); + +const { waitForResult } = await bytecodeContract.functions + .compute_bytecode_root(bytecodeAsVecU8) + .call(); + +const { value: bytecodeRoot } = await waitForResult(); +// #endregion vector-bytecode-input-ts +// #endregion full + +console.log( + 'equals', + JSON.stringify(basicU8Vector) === JSON.stringify([1, 2, 3]) +); +console.log('equals', JSON.stringify(value) === JSON.stringify(employees[1])); +console.log( + 'equals', + bytecodeRoot === + '0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20' +); diff --git a/apps/docs-snippets2/sway/Forc.toml b/apps/docs-snippets2/sway/Forc.toml index fd431b1e348..5165ca3bdbd 100644 --- a/apps/docs-snippets2/sway/Forc.toml +++ b/apps/docs-snippets2/sway/Forc.toml @@ -1,2 +1,17 @@ [workspace] -members = ["counter", "script-sum"] +members = [ + "counter", + "script-sum", + "echo-values", + "echo-asset-id", + "echo-bytes", + "echo-enum", + "sum-option-u8", + "employee-data", + "echo-std-string", + "bytecode-input", + "echo-employee-data-vector", + "echo-raw-slice", + "echo-evm-address", + "input-output-types", +] diff --git a/apps/docs-snippets2/sway/bytecode-input/Forc.toml b/apps/docs-snippets2/sway/bytecode-input/Forc.toml new file mode 100644 index 00000000000..f6e52ea153a --- /dev/null +++ b/apps/docs-snippets2/sway/bytecode-input/Forc.toml @@ -0,0 +1,6 @@ +[project] +authors = ["Fuel Labs "] +license = "Apache-2.0" +name = "bytecode-input" + +[dependencies] diff --git a/apps/docs-snippets2/sway/bytecode-input/src/main.sw b/apps/docs-snippets2/sway/bytecode-input/src/main.sw new file mode 100644 index 00000000000..72efbb2df17 --- /dev/null +++ b/apps/docs-snippets2/sway/bytecode-input/src/main.sw @@ -0,0 +1,21 @@ +contract; + +abi MyContract { + fn compute_bytecode_root(bytecode_input: Vec) -> b256; +} + +// #region b512-1 +pub struct B512 { + /// The two `b256`s that make up the `B512`. + bits: [b256; 2], +} +// #endregion b512-1 + +impl MyContract for Contract { + // #region vector-bytecode-input-sway + fn compute_bytecode_root(bytecode_input: Vec) -> b256 { + //simply return a fixed b256 value created from a hexadecimal string from testing purposes + return 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; + } + // #endregion vector-bytecode-input-sway +} diff --git a/apps/docs-snippets2/sway/echo-asset-id/Forc.toml b/apps/docs-snippets2/sway/echo-asset-id/Forc.toml new file mode 100644 index 00000000000..fb1a79e0ff7 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-asset-id/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-asset-id" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-asset-id/src/main.sw b/apps/docs-snippets2/sway/echo-asset-id/src/main.sw new file mode 100644 index 00000000000..a6cddfece12 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-asset-id/src/main.sw @@ -0,0 +1,25 @@ +// #region asset-id-2 +contract; + +abi EvmTest { + fn echo_asset_id() -> AssetId; + fn echo_asset_id_comparison(asset_id: AssetId) -> bool; + fn echo_asset_id_input(asset_id: AssetId) -> AssetId; +} + +const ASSET_ID: AssetId = AssetId::from(0x9ae5b658754e096e4d681c548daf46354495a437cc61492599e33fc64dcdc30c); + +impl EvmTest for Contract { + fn echo_asset_id() -> AssetId { + ASSET_ID + } + + fn echo_asset_id_comparison(asset_id: AssetId) -> bool { + asset_id == ASSET_ID + } + + fn echo_asset_id_input(asset_id: AssetId) -> AssetId { + asset_id + } +} +// #endregion asset-id-2 diff --git a/apps/docs-snippets2/sway/echo-bytes/Forc.toml b/apps/docs-snippets2/sway/echo-bytes/Forc.toml new file mode 100644 index 00000000000..78665c8162d --- /dev/null +++ b/apps/docs-snippets2/sway/echo-bytes/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-bytes" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-bytes/src/main.sw b/apps/docs-snippets2/sway/echo-bytes/src/main.sw new file mode 100644 index 00000000000..01e3066f069 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-bytes/src/main.sw @@ -0,0 +1,26 @@ +// #region bytes-1 +contract; + +use std::bytes::Bytes; + +abi BytesTest { + fn echo_bytes(value: Bytes) -> Bytes; + fn bytes_comparison(value: Bytes) -> bool; +} + +impl BytesTest for Contract { + fn echo_bytes(value: Bytes) -> Bytes { + value + } + + fn bytes_comparison(value: Bytes) -> bool { + let mut bytes = Bytes::new(); + + bytes.push(40u8); + bytes.push(41u8); + bytes.push(42u8); + + value == bytes + } +} +// #endregion bytes-1 diff --git a/apps/docs-snippets2/sway/echo-employee-data-vector/Forc.toml b/apps/docs-snippets2/sway/echo-employee-data-vector/Forc.toml new file mode 100644 index 00000000000..d24943f2c58 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-employee-data-vector/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-employee-data-vector" + +[dependencies] +employee-data = { path = "../employee-data" } diff --git a/apps/docs-snippets2/sway/echo-employee-data-vector/src/main.sw b/apps/docs-snippets2/sway/echo-employee-data-vector/src/main.sw new file mode 100644 index 00000000000..b1d1d761438 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-employee-data-vector/src/main.sw @@ -0,0 +1,16 @@ +contract; + +use std::vec::Vec; +use employee_data::EmployeeData; + +abi EchoEmployeeDataVector { + fn echo_last_employee_data(employee_data_vector: Vec) -> EmployeeData; +} + +impl EchoEmployeeDataVector for Contract { + // #region vector-3 + fn echo_last_employee_data(employee_data_vector: Vec) -> EmployeeData { + employee_data_vector.get(employee_data_vector.len() - 1).unwrap() + } + // #endregion vector-3 +} diff --git a/apps/docs-snippets2/sway/echo-enum/Forc.toml b/apps/docs-snippets2/sway/echo-enum/Forc.toml new file mode 100644 index 00000000000..513c476c847 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-enum/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-enum" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-enum/src/main.sw b/apps/docs-snippets2/sway/echo-enum/src/main.sw new file mode 100644 index 00000000000..b107b840d3d --- /dev/null +++ b/apps/docs-snippets2/sway/echo-enum/src/main.sw @@ -0,0 +1,46 @@ +contract; + +// #region enums-1 +// #region enums-4 +pub enum StateError { + Void: (), + Pending: (), + Completed: (), +} +// #endregion enums-1 + +pub enum UserError { + Unauthorized: (), + InsufficientPermissions: (), +} + +pub enum Error { + StateError: StateError, + UserError: UserError, +} +// #endregion enums-4 + +abi EchoEnum { + fn echo_state_error_enum(state_error: StateError) -> StateError; + + fn echo_user_error_enum(state_error: UserError) -> UserError; + + fn echo_error_enum(error: Error) -> Error; +} + +impl EchoEnum for Contract { + // #region enums-2 + fn echo_state_error_enum(state_error: StateError) -> StateError { + state_error + } + // #endregion enums-2 + fn echo_user_error_enum(user_error: UserError) -> UserError { + user_error + } + + // #region enums-5 + fn echo_error_enum(error: Error) -> Error { + error + } + // #endregion enums-5 +} diff --git a/apps/docs-snippets2/sway/echo-evm-address/Forc.toml b/apps/docs-snippets2/sway/echo-evm-address/Forc.toml new file mode 100644 index 00000000000..dcc57bfdbb3 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-evm-address/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-evm-address" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-evm-address/src/main.sw b/apps/docs-snippets2/sway/echo-evm-address/src/main.sw new file mode 100644 index 00000000000..9650c4d6eb6 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-evm-address/src/main.sw @@ -0,0 +1,26 @@ +// #region evm-address-1 +contract; + +use std::vm::evm::evm_address::EvmAddress; + +configurable { + B256_ADDR: b256 = 0xbebd3baab326f895289ecbd4210cf886ce41952316441ae4cac35f00f0e882a6, +} + +abi EvmTest { + fn echo_address() -> EvmAddress; + fn echo_address_comparison(evm_addr: EvmAddress) -> bool; +} + +impl EvmTest for Contract { + fn echo_address() -> EvmAddress { + return EvmAddress::from(B256_ADDR); + } + + fn echo_address_comparison(evm_addr: EvmAddress) -> bool { + let evm_addr2 = EvmAddress::from(B256_ADDR); + + evm_addr == evm_addr2 + } +} +// #endregion evm-address-1 diff --git a/apps/docs-snippets2/sway/echo-raw-slice/Forc.toml b/apps/docs-snippets2/sway/echo-raw-slice/Forc.toml new file mode 100644 index 00000000000..ba2173acecc --- /dev/null +++ b/apps/docs-snippets2/sway/echo-raw-slice/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-raw-slice" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-raw-slice/src/main.sw b/apps/docs-snippets2/sway/echo-raw-slice/src/main.sw new file mode 100644 index 00000000000..0264c48a5f9 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-raw-slice/src/main.sw @@ -0,0 +1,20 @@ +// #region raw-slice-1 +contract; + +abi RawSliceTest { + fn echo_raw_slice(value: raw_slice) -> raw_slice; + fn raw_slice_comparison(value: raw_slice) -> bool; +} + +impl RawSliceTest for Contract { + fn echo_raw_slice(value: raw_slice) -> raw_slice { + value + } + + fn raw_slice_comparison(value: raw_slice) -> bool { + let vec: Vec = Vec::from(value); + + vec.len() == 3 && vec.get(0).unwrap() == 40 && vec.get(1).unwrap() == 41 && vec.get(2).unwrap() == 42 + } +} +// #endregion raw-slice-1 diff --git a/apps/docs-snippets2/sway/echo-std-string/Forc.toml b/apps/docs-snippets2/sway/echo-std-string/Forc.toml new file mode 100644 index 00000000000..f7557aff247 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-std-string/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-std-string" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-std-string/src/main.sw b/apps/docs-snippets2/sway/echo-std-string/src/main.sw new file mode 100644 index 00000000000..cb4989eae01 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-std-string/src/main.sw @@ -0,0 +1,22 @@ +// #region std-string-1 +contract; + +use std::string::String; + +abi StdStringTest { + fn echo_string(value: String) -> String; + fn string_comparison(value: String) -> bool; +} + +impl StdStringTest for Contract { + fn echo_string(value: String) -> String { + value + } + + fn string_comparison(value: String) -> bool { + let expected = String::from_ascii_str("Hello World"); + + value.as_bytes() == expected.as_bytes() + } +} +// #endregion std-string-1 diff --git a/apps/docs-snippets2/sway/echo-values/Forc.toml b/apps/docs-snippets2/sway/echo-values/Forc.toml new file mode 100644 index 00000000000..999e1134150 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-values/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-values" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-values/src/main.sw b/apps/docs-snippets2/sway/echo-values/src/main.sw new file mode 100644 index 00000000000..f48734129c6 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-values/src/main.sw @@ -0,0 +1,56 @@ +// #region understanding-fuel-binary-file +contract; + +use std::b512::B512; + +abi EchoValues { + fn echo_u8(value: u8) -> u8; + + fn echo_str_8(value: str[8]) -> str[8]; + + fn echo_str(value: str) -> str; + + fn echo_tuple(tuple: (u8, bool, u64)) -> (u8, bool, u64); + + fn echo_b512(input: B512) -> B512; + + fn echo_u64(value: u64) -> u64; + + fn echo_u64_array(u64_array: [u64; 2]) -> [u64; 2]; +} + +impl EchoValues for Contract { + fn echo_u8(value: u8) -> u8 { + value + } + + fn echo_str(value: str) -> str { + value + } + + fn echo_str_8(value: str[8]) -> str[8] { + value + } + + // #region tuples-2 + fn echo_tuple(tuple: (u8, bool, u64)) -> (u8, bool, u64) { + tuple + } + // #endregion tuples-2 + + // #region bits512-3 + fn echo_b512(input: B512) -> B512 { + input + } + // #endregion bits512-3 + fn echo_u64(value: u64) -> u64 { + value + } + + // #region arrays-2 + fn echo_u64_array(u64_array: [u64; 2]) -> [u64; 2] { + u64_array + } + // #endregion arrays-2 +} +// #endregion understanding-fuel-binary-file diff --git a/apps/docs-snippets2/sway/employee-data/Forc.toml b/apps/docs-snippets2/sway/employee-data/Forc.toml new file mode 100644 index 00000000000..92204e48a8a --- /dev/null +++ b/apps/docs-snippets2/sway/employee-data/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "lib.sw" +license = "Apache-2.0" +name = "employee-data" + +[dependencies] diff --git a/apps/docs-snippets2/sway/employee-data/src/lib.sw b/apps/docs-snippets2/sway/employee-data/src/lib.sw new file mode 100644 index 00000000000..8f18b929e58 --- /dev/null +++ b/apps/docs-snippets2/sway/employee-data/src/lib.sw @@ -0,0 +1,12 @@ +library; + +// #region struct-1 +pub struct EmployeeData { + name: str[8], + age: u8, + salary: u64, + idHash: b256, + ratings: [u8; 3], + isActive: bool, +} +// #endregion struct-1 diff --git a/apps/docs-snippets2/sway/input-output-types/Forc.toml b/apps/docs-snippets2/sway/input-output-types/Forc.toml new file mode 100644 index 00000000000..3b8b0aaa654 --- /dev/null +++ b/apps/docs-snippets2/sway/input-output-types/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "input-output-types" + +[dependencies] diff --git a/apps/docs-snippets2/sway/input-output-types/src/main.sw b/apps/docs-snippets2/sway/input-output-types/src/main.sw new file mode 100644 index 00000000000..b3ceac88bb9 --- /dev/null +++ b/apps/docs-snippets2/sway/input-output-types/src/main.sw @@ -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 + } +} diff --git a/apps/docs-snippets2/sway/sum-option-u8/Forc.toml b/apps/docs-snippets2/sway/sum-option-u8/Forc.toml new file mode 100644 index 00000000000..d64b9dae2ef --- /dev/null +++ b/apps/docs-snippets2/sway/sum-option-u8/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "sum-option-u8" + +[dependencies] diff --git a/apps/docs-snippets2/sway/sum-option-u8/src/main.sw b/apps/docs-snippets2/sway/sum-option-u8/src/main.sw new file mode 100644 index 00000000000..c7d219d6d42 --- /dev/null +++ b/apps/docs-snippets2/sway/sum-option-u8/src/main.sw @@ -0,0 +1,23 @@ +contract; + +abi SumOptionU8 { + fn sum_optional_u8(input1: Option, input2: Option) -> u8; +} + +impl SumOptionU8 for Contract { + // #region options-2 + fn sum_optional_u8(input1: Option, input2: Option) -> u8 { + let value1 = match input1 { + Option::Some(v) => v, + Option::None => 0, + }; + + let value2 = match input2 { + Option::Some(v) => v, + Option::None => 0, + }; + + value1 + value2 + } + // #endregion options-2 +} diff --git a/apps/docs/src/guide/types/address.md b/apps/docs/src/guide/types/address.md index af446f25e22..455a790c40b 100644 --- a/apps/docs/src/guide/types/address.md +++ b/apps/docs/src/guide/types/address.md @@ -22,19 +22,19 @@ Thanks to the utility functions provided by the [`AbstractAddress`](../../api/In To create an [`Address`](../../api/Address/Address.md) from a `Bech32` address, use the following code snippet: -<<< @/../../docs-snippets/src/guide/types/address.test.ts#address-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/address/creating-an-address.ts#full{ts:line-numbers} ### From a Public Key To create an [`Address`](../../api/Address/Address.md) from a public key, use the following code snippet: -<<< @/../../docs-snippets/src/guide/types/address.test.ts#address-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/address/from-a-public-key.ts#full{ts:line-numbers} ### From a 256-bit Address To create an [`Address`](../../api/Address/Address.md) from a 256-bit address, use the following code snippet: -<<< @/../../docs-snippets/src/guide/types/address.test.ts#address-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/address/from-a-b256.ts#full{ts:line-numbers} ## Utility Functions @@ -42,12 +42,12 @@ The [`Address`](../../api/Address/Address.md) class also provides some practical 1. `fromString`: Create a new [`Address`](../../api/Address/Address.md) from an ambiguous source that may be a `Bech32` or `B256` address: -<<< @/../../docs-snippets/src/guide/types/address.test.ts#address-5{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/address/utilities-function-1.ts#full{ts:line-numbers} 2. `fromDynamicInput`: Create a new [`Address`](../../api/Address/Address.md) when the address source is unknown: -<<< @/../../docs-snippets/src/guide/types/address.test.ts#address-6{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/address/utilities-function-2.ts#full{ts:line-numbers} 3. `equals:` As you may already notice, the `equals` function can compare addresses instances: -<<< @/../../docs-snippets/src/guide/types/address.test.ts#address-7{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/address/utilities-function-3.ts#full{ts:line-numbers} diff --git a/apps/docs/src/guide/types/arrays.md b/apps/docs/src/guide/types/arrays.md index 3ebe5fd7970..469790e0fab 100644 --- a/apps/docs/src/guide/types/arrays.md +++ b/apps/docs/src/guide/types/arrays.md @@ -10,17 +10,17 @@ The SDK handles the conversion from TypeScript to Sway in the background, allowi An `Array` in Sway is simply a typed `Array`, as demonstrated in the following example: -<<< @/../../docs-snippets/src/guide/types/arrays.test.ts#arrays-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/arrays.ts#arrays-1{ts:line-numbers} In Sway, `Arrays` are fixed in size, so the storage size is determined at the time of program compilation, not during runtime. Let's say you have a contract that takes an `Array` of type `u64` with a size length of 2 as a parameter and returns it: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-u64-array/src/main.sw#arrays-2{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-values/src/main.sw#arrays-2{rust:line-numbers} To execute the contract call using the SDK, you would do something like this: -<<< @/../../docs-snippets/src/guide/types/arrays.test.ts#arrays-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/arrays.ts#arrays-3{ts:line-numbers} You can easily access and validate the `Array` returned by the contract method, as demonstrated in the previous example. @@ -28,11 +28,11 @@ As previously mentioned, Sway `Arrays` have a predefined type and size, so you n Passing an Array with an incorrect size, whether it has more or fewer elements than the specified length, will result in an error: -<<< @/../../docs-snippets/src/guide/types/arrays.test.ts#arrays-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/arrays.ts#arrays-4{ts:line-numbers} Similarly, passing an `Array` with an incorrect type will also result in an error: -<<< @/../../docs-snippets/src/guide/types/arrays.test.ts#arrays-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/arrays.ts#arrays-5{ts:line-numbers} ## Vectors diff --git a/apps/docs/src/guide/types/asset-id.md b/apps/docs/src/guide/types/asset-id.md index f3918f39c39..f277a18765e 100644 --- a/apps/docs/src/guide/types/asset-id.md +++ b/apps/docs/src/guide/types/asset-id.md @@ -2,18 +2,18 @@ An Asset ID can be represented using the `AssetId` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `Bits256` value. -<<< @/../../docs-snippets/src/guide/types/asset-id.test.ts#asset-id-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/asset-id/intro.ts#full{ts:line-numbers} ## Using an Asset ID -The `AssetId` type can be integrated with your contract calls. Consider the following contract that can compares and return an Asset ID: +You can easily use the `AssetId` type within your Sway programs. Consider the following contract that can compares and return an `AssetId`: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-asset-id/src/main.sw#asset-id-1{ts:line-numbers} +<<< @/../../docs-snippets2/sway/echo-asset-id/src/main.sw#asset-id-2{ts:line-numbers} -The `AssetId` type can be used with the SDK and passed to the contract function as follows: +The `AssetId` struct can be passed to the contract function as follows: -<<< @/../../docs-snippets/src/guide/types/asset-id.test.ts#asset-id-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/asset-id/using-an-asset-id-1.ts#snippet-1{ts:line-numbers} And to validate the returned value: -<<< @/../../docs-snippets/src/guide/types/asset-id.test.ts#asset-id-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/asset-id/using-an-asset-id-2.ts#snippet-1{ts:line-numbers} diff --git a/apps/docs/src/guide/types/bech32.md b/apps/docs/src/guide/types/bech32.md index cd9fb847c1b..624949957f8 100644 --- a/apps/docs/src/guide/types/bech32.md +++ b/apps/docs/src/guide/types/bech32.md @@ -14,6 +14,6 @@ A `Bech32` address consists of an HRP (Human-Readable Part) followed by the numb A complete `Bech32` address will resemble the following: -<<< @/../../docs-snippets/src/guide/types/bech32.test.ts#bech32-2{5 ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bech32.ts#bech32-2{5 ts:line-numbers} The HRP in the example above is `fuel`. This human-readable prefix is included to provide better readability and prevent users from accidentally using addresses on the wrong network. diff --git a/apps/docs/src/guide/types/bits256.md b/apps/docs/src/guide/types/bits256.md index 41c7a3dfc8c..893ff68cf79 100644 --- a/apps/docs/src/guide/types/bits256.md +++ b/apps/docs/src/guide/types/bits256.md @@ -6,16 +6,16 @@ The type `b256` in Fuel represents hashes and holds a 256-bit (32-bytes) value. To generate a random `b256` value, you can use the `getRandomB256()` function: -<<< @/../../docs-snippets/src/guide/types/bits256.test.ts#bits256-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bits256/generating-random-b256.ts#full{ts:line-numbers} ### Converting between `b256` and `Uint8Array` To convert between a `b256` hexlified string and a `Uint8Array`, you can use the `arrayify` and `hexlify` functions: -<<< @/../../docs-snippets/src/guide/types/bits256.test.ts#bits256-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bits256/converting-between-b256-uint8.ts#full{ts:line-numbers} ## Support from `Address` Class A `b256` value is also supported as part of the [`Address`](../../api/Address/Address.md) class, providing seamless integration with other components of your application. To create an [`Address`](../../api/Address/Address.md) instance from a b256 value, use the `Address.fromB256()` method: -<<< @/../../docs-snippets/src/guide/types/bits256.test.ts#bits256-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bits256/support-from-address-class.ts#full{ts:line-numbers} diff --git a/apps/docs/src/guide/types/bits512.md b/apps/docs/src/guide/types/bits512.md index b67bd18103f..ec2299402b4 100644 --- a/apps/docs/src/guide/types/bits512.md +++ b/apps/docs/src/guide/types/bits512.md @@ -4,22 +4,22 @@ In Sway, the `b512` type is commonly used to handle public keys and signatures. The `b512` type in Sway is a wrapper around two `b256` types, allowing for the representation of 64-byte values. It is defined as a struct: -<<< @/../../docs-snippets/src/guide/types/bits512.test.ts#bits512-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/bytecode-input/src/main.sw#b512-1{rs:line-numbers} ## `b512` in the SDK In the SDK, you can visualize a `b512` value by examining a wallet's public key: -<<< @/../../docs-snippets/src/guide/types/bits512.test.ts#bits512-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bits512/b512-in-the-sdk.ts#snippet-1{ts:line-numbers} ## Example: Echoing a `b512` Value in a Contract Function Let's consider a contract function that accepts a `b512` parameter and returns the same value: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-values/src/main.sw#bits512-3{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-values/src/main.sw#bits512-3{rust:line-numbers} To call this function and validate the returned value, follow these steps: -<<< @/../../docs-snippets/src/guide/types/bits512.test.ts#bits512-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bits512/echoing-a-b512.ts#snippet-1{ts:line-numbers} In this example, we generate a wallet, use its public key as the `b512` input, call the `echo_b512` contract function, and expect the returned value to match the original input. diff --git a/apps/docs/src/guide/types/bytes.md b/apps/docs/src/guide/types/bytes.md index d397b7ff59b..351a1e3f1e7 100644 --- a/apps/docs/src/guide/types/bytes.md +++ b/apps/docs/src/guide/types/bytes.md @@ -2,14 +2,12 @@ A dynamic array of byte values can be represented using the `Bytes` type, which represents raw bytes. -<<< @/../../docs-snippets/src/guide/types/bytes.test.ts#bytes-1{ts:line-numbers} - ## Using Bytes The `Bytes` type can be integrated with your contract calls. Consider the following contract that can compare and return a `Bytes`: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-bytes/src/main.sw#bytes-1{ts:line-numbers} +<<< @/../../docs-snippets2/sway/echo-bytes/src/main.sw#bytes-1{ts:line-numbers} A `Bytes` array can be created using a native JavaScript array of numbers or Big Numbers, and sent to a Sway contract: -<<< @/../../docs-snippets/src/guide/types/bytes.test.ts#bytes-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bytes.ts#snippet-1{ts:line-numbers} diff --git a/apps/docs/src/guide/types/bytes32.md b/apps/docs/src/guide/types/bytes32.md index 1ebb3818387..4477e40988f 100644 --- a/apps/docs/src/guide/types/bytes32.md +++ b/apps/docs/src/guide/types/bytes32.md @@ -6,13 +6,13 @@ In Sway and the FuelVM, `bytes32` is used to represent hashes. It holds a 256-bi To generate a random `bytes32` value, you can use the `randomBytes` function from the fuels module: -<<< @/../../docs-snippets/src/guide/types/bytes32.test.ts#bytes32-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bytes32/generating-random-bytes32.ts#snippet-1{ts:line-numbers} ## Converting Between Byte Arrays and Strings You can use the `hexlify` function to convert a byte array to a hex string, and the `arrayify` function to convert a hex string back to a byte array: -<<< @/../../docs-snippets/src/guide/types/bytes32.test.ts#bytes32-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bytes32/converting-between-byte.ts#snippet-1{ts:line-numbers} ## Working with b256 in Fuel diff --git a/apps/docs/src/guide/types/enums.md b/apps/docs/src/guide/types/enums.md index a243d4c42ad..1c11d0eea2b 100644 --- a/apps/docs/src/guide/types/enums.md +++ b/apps/docs/src/guide/types/enums.md @@ -6,7 +6,7 @@ Sway Enums are a little distinct from TypeScript Enums. In this document, we wil Consider the following basic Sway Enum called `StateError`: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-enum/src/main.sw#simple-enum-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-enum/src/main.sw#enums-1{rust:line-numbers} The type `()` indicates that there is no additional data associated with each Enum variant. Sway allows you to create Enums of Enums or associate types with Enum variants. @@ -14,11 +14,11 @@ The type `()` indicates that there is no additional data associated with each En Let's define a Sway contract function that takes a `StateError` Enum variant as an argument and returns it: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-enum/src/main.sw#simple-enum-2{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-enum/src/main.sw#enums-2{rust:line-numbers} To execute the contract function and validate the response, we can use the following code: -<<< @/../../docs-snippets/src/guide/types/enums.test.ts#simple-enum-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/enums/using-sway-enums.ts#snippet-1{ts:line-numbers} In this example, we simply pass the Enum variant as a value to execute the contract function call. @@ -26,23 +26,23 @@ In this example, we simply pass the Enum variant as a value to execute the contr In this example, the `Error` Enum is an Enum of two other Enums: `StateError` and `UserError`. -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-enum/src/main.sw#enum-of-enums-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-enum/src/main.sw#enums-4{rust:line-numbers} ### Using Enums of Enums with Contract Functions Now, let's create a Sway contract function that accepts any variant of the `Error` Enum as a parameter and returns it immediately. This variant could be from either the `StateError` or `UserError` Enums. -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-enum/src/main.sw#enum-of-enums-2{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-enum/src/main.sw#enums-5{rust:line-numbers} Since the `Error` Enum is an Enum of Enums, we need to pass the function parameter differently. The parameter will be a TypeScript object: -<<< @/../../docs-snippets/src/guide/types/enums.test.ts#enum-of-enums-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/enums/using-enums-of-enums-1.ts#snippet-1{ts:line-numbers} In this case, since the variant `InsufficientPermissions` belongs to the `UserError` Enum, we create a TypeScript object using the Enum name as the object key and the variant as the object value. We would follow the same approach if we intended to use a variant from the `StateError` Enum: -<<< @/../../docs-snippets/src/guide/types/enums.test.ts#enum-of-enums-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/enums/using-enums-of-enums-2.ts#snippet-1{ts:line-numbers} ## Errors @@ -52,16 +52,16 @@ While working with enums, you may run into the following issues: Thrown when the type being passed to the enum does not match that expected by it. -<<< @/../../docs-snippets/src/guide/types/enums.test.ts#enum-error-mismatch-type{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/enums/using-an-invalid-enum-type.ts#snippet-1{ts:line-numbers} ### Using an invalid enum value Thrown when the parameter passed is not an expected enum value. -<<< @/../../docs-snippets/src/guide/types/enums.test.ts#enum-error-value-mismatch{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/enums/using-an-invalid-enum-value.ts#snippet-1{ts:line-numbers} ### Using an invalid enum case key Thrown when the passed enum case is not an expected enum case value. -<<< @/../../docs-snippets/src/guide/types/enums.test.ts#enum-error-case-key-mismatch{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/enums/using-an-invalid-enum-case.ts#snippet-1{ts:line-numbers} diff --git a/apps/docs/src/guide/types/evm-address.md b/apps/docs/src/guide/types/evm-address.md index 2a5eebe54ce..2d06ff72607 100644 --- a/apps/docs/src/guide/types/evm-address.md +++ b/apps/docs/src/guide/types/evm-address.md @@ -2,24 +2,24 @@ An Ethereum Virtual Machine (EVM) Address can be represented using the `EvmAddress` type. It's definition matches the Sway standard library type being a `Struct` wrapper around an inner `Bits256` value. -<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/evm-address/intro.ts#snippet-1{ts:line-numbers} ## Creating an EVM Address An EVM Address only has 20 bytes therefore the first 12 bytes of the `Bits256` value are set to 0. Within the SDK, an `Address` can be instantiated and converted to a wrapped and Sway compatible EVM Address using the `toEvmAddress()` function: -<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/evm-address/creating-an-evm.ts#snippet-1{ts:line-numbers} ## Using an EVM Address The `EvmAddress` type can be integrated with your contract calls. Consider the following contract that can compare and return an EVM Address: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-evm-address/src/main.sw#evm-address-1{ts:line-numbers} +<<< @/../../docs-snippets2/sway/echo-evm-address/src/main.sw#evm-address-1{ts:line-numbers} The `EvmAddress` type can be used with the SDK and passed to the contract function as follows: -<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/evm-address/using-an-evm-address-1.ts#snippet-1{ts:line-numbers} And to validate the returned value: -<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#evm-address-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/evm-address/using-an-evm-address-2.ts#snippet-1{ts:line-numbers} diff --git a/apps/docs/src/guide/types/native-parameters.md b/apps/docs/src/guide/types/native-parameters.md index 3ec894d081c..f43099185b2 100644 --- a/apps/docs/src/guide/types/native-parameters.md +++ b/apps/docs/src/guide/types/native-parameters.md @@ -13,13 +13,13 @@ Below you can find examples of how to convert between common native Sway program To pass an `Address` as an input parameter to a Sway program, you can define the input as shown below: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#address-input +<<< @/../../docs-snippets2/src/types/native-parameters/address.ts#address-input ### `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: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#address-output +<<< @/../../docs-snippets2/src/types/native-parameters/address.ts#address-output ## `ContractId` @@ -27,13 +27,13 @@ For a Sway program that returns an `Address` type, you can convert the returned To pass a `ContractId` as an input parameter to a Sway program, you can define the input as shown below: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#contract-id-input +<<< @/../../docs-snippets2/src/types/native-parameters/contract-id.ts#contract-id-input ### `ContractIdOutput` For a Sway program that returns a `ContractId` type, you can convert the returned value to a `string` as shown below: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#contract-id-output +<<< @/../../docs-snippets2/src/types/native-parameters/contract-id.ts#contract-id-output ## `Identity` @@ -43,11 +43,11 @@ To pass an `Identity` as an input parameter to a Sway program, you can define th For an address: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-address-input +<<< @/../../docs-snippets2/src/types/native-parameters/identity-address.ts#identity-address-input For a contract: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-contract-input +<<< @/../../docs-snippets2/src/types/native-parameters/identity-contract.ts#identity-contract-input ### `IdentityOutput` @@ -55,11 +55,11 @@ For a Sway program that returns an `Identity` type, you can convert the returned For an address: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-address-output +<<< @/../../docs-snippets2/src/types/native-parameters/identity-address.ts#identity-address-output For a contract: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#identity-contract-output +<<< @/../../docs-snippets2/src/types/native-parameters/identity-contract.ts#identity-contract-output ## `AssetId` @@ -67,10 +67,10 @@ For a contract: To pass an `AssetId` as an input parameter to a Sway program, you can define the input as shown below: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#asset-id-input +<<< @/../../docs-snippets2/src/types/native-parameters/asset-id.ts#asset-id-input ### `AssetIdOutput` For a Sway program that returns an `AssetId` type, you can convert the returned value to a `string` as shown below: -<<< @/../../docs-snippets/src/guide/types/contract-types.test.ts#asset-id-output +<<< @/../../docs-snippets2/src/types/native-parameters/asset-id.ts#asset-id-output diff --git a/apps/docs/src/guide/types/numbers.md b/apps/docs/src/guide/types/numbers.md index a434d498a5f..6962bf90a1a 100644 --- a/apps/docs/src/guide/types/numbers.md +++ b/apps/docs/src/guide/types/numbers.md @@ -16,11 +16,11 @@ This guide explains how to create and interact with Sway numbers while using the When you pass in a `u64` or a `u256` to a Sway program from JavaScript, you must first convert it to a `BigNum` object. This is because these types can have extremely large maximum values (`2^64` and `2^256` respectively), and JavaScript's `Number` type can only hold up to 53 bits of precision (`2^53`). -<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/numbers/for-u64-and-u256-1.ts#snippet-1{ts:line-numbers} You can also create a `BigNum` from a string. This is useful when you want to pass in a number that is too large to be represented as a JavaScript number. Here's how you can do that: -<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/numbers/for-u64-and-u256-2.ts#snippet-1{ts:line-numbers} ### For `u8`, `u16`, and `u32` @@ -30,14 +30,14 @@ You don't need to do anything special to create these numbers. You can pass in a ### For `u64` and `u256` -<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/numbers/for-u64-and-u256-3.ts#snippet-1{ts:line-numbers} > Note: If a contract call returns a number that is too large to be represented as a JavaScript number, you can convert it to a string using the `.toString()` method instead of `.toNumber()`. ### For `u8`, `u16`, and `u32` -<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/numbers/for-u8-u16-and-u32-1.ts#snippet-1{ts:line-numbers} ### Using a `BigNum` from `ethers` with `fuels` -<<< @/../../docs-snippets/src/guide/types/numbers.test.ts#numbers-docs-5{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/numbers/for-u8-u16-and-u32-2.ts#snippet-1{ts:line-numbers} diff --git a/apps/docs/src/guide/types/options.md b/apps/docs/src/guide/types/options.md index e5fb87c2fbc..21e76d9dcbc 100644 --- a/apps/docs/src/guide/types/options.md +++ b/apps/docs/src/guide/types/options.md @@ -8,7 +8,7 @@ In this guide, we'll explain how to work with Option types in Sway and demonstra The `Option` type in Sway is a special wrapper type of Enum. In TypeScript, you can represent the `Option` type by using the `undefined` keyword, as shown in the following example -<<< @/../../docs-snippets/src/guide/types/options.test.ts#options-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/options/overview-of-option.ts#snippet-1{ts:line-numbers} In this example, the variable `input1` can be either a `number` or `undefined`. @@ -18,14 +18,14 @@ Let's say we have a contract function that accepts two `Option` parameters. Here's the contract function written in Sway: -<<< @/../../docs-snippets/test/fixtures/forc-projects/sum-option-u8/src/main.sw#options-2{rust:line-numbers} +<<< @/../../docs-snippets2/sway/sum-option-u8/src/main.sw#options-2{rust:line-numbers} You can interact with the contract function using the SDK as follows: -<<< @/../../docs-snippets/src/guide/types/options.test.ts#options-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/options/overview-of-option.ts#snippet-2{ts:line-numbers} In this case, the result of the contract function call is the sum of both input parameters. If we pass only one parameter, the contract function will default the other parameter's value to `0`. -<<< @/../../docs-snippets/src/guide/types/options.test.ts#options-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/options/example-option-u8.ts#snippet-1{ts:line-numbers} Using `Option` types in Sway allows you to elegantly handle situations where a variable may or may not have a defined value. diff --git a/apps/docs/src/guide/types/raw-slice.md b/apps/docs/src/guide/types/raw-slice.md index b84c89e7842..ff37f08c3b9 100644 --- a/apps/docs/src/guide/types/raw-slice.md +++ b/apps/docs/src/guide/types/raw-slice.md @@ -2,14 +2,12 @@ A dynamic array of values can be represented using the `RawSlice` type. A raw slice can be a value reference or a raw pointer. -<<< @/../../docs-snippets/src/guide/types/raw-slice.test.ts#raw-slice-1{ts:line-numbers} - ## Using a `RawSlice` The `RawSlice` type can be integrated with your contract calls. Consider the following contract that can compare and return a `RawSlice`: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-raw-slice/src/main.sw#raw-slice-1{ts:line-numbers} +<<< @/../../docs-snippets2/sway/echo-raw-slice/src/main.sw#raw-slice-1{ts:line-numbers} A `RawSlice` can be created using a native JavaScript array of numbers or Big Numbers, and sent to a Sway contract: -<<< @/../../docs-snippets/src/guide/types/raw-slice.test.ts#raw-slice-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/raw-slice.ts#raw-slice-2{ts:line-numbers} diff --git a/apps/docs/src/guide/types/std-string.md b/apps/docs/src/guide/types/std-string.md index 1315919d71c..9ca0340a3ce 100644 --- a/apps/docs/src/guide/types/std-string.md +++ b/apps/docs/src/guide/types/std-string.md @@ -2,14 +2,12 @@ A dynamic string of variable length can be represented using the `StdString` type, also known as a Standard Lib String or `std-lib-string`. It behaves much like a dynamic string in most languages, and is essentially an array of characters. -<<< @/../../docs-snippets/src/guide/types/std-string.test.ts#std-string-1{ts:line-numbers} - ## Using a `StdString` The `StdString` type can be integrated with your contract calls. Consider the following contract that can compare and return a String: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-std-string/src/main.sw#std-string-1{ts:line-numbers} +<<< @/../../docs-snippets2/sway/echo-std-string/src/main.sw#std-string-1{ts:line-numbers} A string can be created using a native JavaScript string, and sent to a Sway contract: -<<< @/../../docs-snippets/src/guide/types/std-string.test.ts#std-string-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/std-string.ts#std-string-2{ts:line-numbers} diff --git a/apps/docs/src/guide/types/string.md b/apps/docs/src/guide/types/string.md index 88cf91c263f..cbcef9f3694 100644 --- a/apps/docs/src/guide/types/string.md +++ b/apps/docs/src/guide/types/string.md @@ -5,16 +5,16 @@ This guide explains how to create and interact with statically-sized strings whi ## Creating Statically-Sized Strings -<<< @/../../docs-snippets/src/guide/types/string.test.ts#string-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/string.ts#string-1{ts:line-numbers} ## Interacting with Statically-Sized Strings in Contract Methods When a contract method accepts and returns a `str[8]`, the corresponding SDK wrapper method will also take and return a string of the same length. You can pass a string to the contract method like this: -<<< @/../../docs-snippets/src/guide/types/string.test.ts#string-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/string.ts#string-2{ts:line-numbers} When working with statically-sized strings, ensure that the input and output strings have the correct length to avoid erroneous behavior. If you pass a string that is either too long or too short for a contract method, the call will fail like this: -<<< @/../../docs-snippets/src/guide/types/string.test.ts#string-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/string.ts#string-3{ts:line-numbers} diff --git a/apps/docs/src/guide/types/structs.md b/apps/docs/src/guide/types/structs.md index 58f08346ef8..b2f34fe4988 100644 --- a/apps/docs/src/guide/types/structs.md +++ b/apps/docs/src/guide/types/structs.md @@ -6,11 +6,11 @@ In Sway, a `struct` serves a similar purpose as an `Object` in TypeScript. It de Here is an example of a `struct` in Sway: -<<< @/../../docs-snippets/test/fixtures/forc-projects/employee-data/src/lib.sw#struct-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/employee-data/src/lib.sw#struct-1{rust:line-numbers} And here is the equivalent structure represented in TypeScript: -<<< @/../../docs-snippets/src/guide/types/struct.test.ts#struct-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/structs.ts#struct-2{ts:line-numbers} ## Handling Different Data Types diff --git a/apps/docs/src/guide/types/tuples.md b/apps/docs/src/guide/types/tuples.md index e5f00765ba1..a4847376fc7 100644 --- a/apps/docs/src/guide/types/tuples.md +++ b/apps/docs/src/guide/types/tuples.md @@ -4,7 +4,7 @@ In Sway, Tuples are fixed-length collections of heterogeneous elements. Tuples c In TypeScript, you can represent Sway tuples using arrays with specified types for each element: -<<< @/../../docs-snippets/src/guide/types/tuples.test.ts#tuples-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/tuples.ts#tuples-1{ts:line-numbers} In this example, the Typescript `tuple` variable contains three elements of different types: a number, a boolean, and another number. @@ -12,11 +12,11 @@ In this example, the Typescript `tuple` variable contains three elements of diff Let's consider a contract function that accepts a tuple as a parameter and returns the same Tuple: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-values/src/main.sw#tuples-2{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-values/src/main.sw#tuples-2{rust:line-numbers} To execute and validate the contract function using the SDK, follow these steps: -<<< @/../../docs-snippets/src/guide/types/tuples.test.ts#tuples-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/tuples.ts#tuples-3{ts:line-numbers} In this example, we create a Tuple with three elements, call the `echo_tuple` contract function, and expect the returned tuple to match the original one. Note that we convert the third element of the returned tuple to a number using `new BN(value[2]).toNumber()`. diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 326dcaf3df6..a1307ffc061 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -6,26 +6,26 @@ In Sway, a Vector is a dynamic-sized collection of elements of the same type. Ve A basic Vector in Sway is similar to a TypeScript Array: -<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/vectors.ts#vector-1{ts:line-numbers} Consider the following example of a `EmployeeData` struct in Sway: -<<< @/../../docs-snippets/test/fixtures/forc-projects/employee-data/src/lib.sw#struct-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/employee-data/src/lib.sw#struct-1{rust:line-numbers} Now, let's look at the following contract method. It receives a Vector of the `Transaction` struct type as a parameter and returns the last `Transaction` entry from the Vector: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-employee-data-vector/src/main.sw#vector-3{ts:line-numbers} +<<< @/../../docs-snippets2/sway/echo-employee-data-vector/src/main.sw#vector-3{ts:line-numbers} The code snippet below demonstrates how to call this Sway contract method, which accepts a `Vec`: -<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/vectors.ts#vector-4{ts:line-numbers} ## Converting Bytecode to Vectors Some functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`, here's an example of how to pass bytecode to a function: -<<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} +<<< @/../../docs-snippets2/sway/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} To pass bytecode to this function, you can make use of the `arrayify` function to convert the bytecode file contents into a `UInt8Array`, the TS compatible type for Sway's `Vec` type and pass it the function like so: -<<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-bytecode-input-ts{ts:line-numbers} \ No newline at end of file +<<< @/../../docs-snippets2/src/types/vectors.ts#vector-bytecode-input-ts{ts:line-numbers} diff --git a/apps/docs/src/guide/utilities/address-conversion.md b/apps/docs/src/guide/utilities/address-conversion.md index 230af22f5e4..1703212f27b 100644 --- a/apps/docs/src/guide/utilities/address-conversion.md +++ b/apps/docs/src/guide/utilities/address-conversion.md @@ -4,15 +4,16 @@ Addresses and varying address formats are commonplace when interacting with dece The Fuel Network uses the [`Bech32`](../types/bech32.md) address format for its interactions, an example of which can be seen below: -<<< @/../../docs-snippets/src/guide/types/bech32.test.ts#addresses-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/bech32.ts#addresses-1{ts:line-numbers} However, a hexlified [Bits256](../types/bits256.md) (hex) is another common address format; an example can be seen below: -<<< @/../../docs-snippets/src/guide/types/bits256.test.ts#addresses-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/evm-address/creating-an-evm.ts#snippet-2{ts:line-numbers} +apps/ At times, these can even be wrapped in a [Struct](../types/structs.md). Such as an [Asset ID](../types/asset-id.md) or a [EVM Address](../types/evm-address.md): -<<< @/../../docs-snippets/src/guide/types/evm-address.test.ts#addresses-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/types/evm-address/using-an-evm-address-1.ts#snippet-2{ts:line-numbers} The TS-SDK makes converting between these addresses simple using the [Address](../types/address.md) helper, which provides various utilities for conversion. diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 68438299db6..ebf96cb0a59 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -561,9 +561,6 @@ importers: '@fuel-ts/utils': specifier: workspace:* version: link:../../packages/utils - ethers: - specifier: ^6.13.4 - version: 6.13.4(bufferutil@4.0.8)(utf-8-validate@6.0.4) fuels: specifier: workspace:* version: link:../../packages/fuels @@ -573,6 +570,9 @@ importers: apps/docs-snippets2: dependencies: + ethers: + specifier: ^6.13.2 + version: 6.13.4(bufferutil@4.0.8)(utf-8-validate@6.0.4) fuels: specifier: workspace:* version: link:../../packages/fuels