From 811f5453a0bf5672ddfa9aeca443a04a00436f8a Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 26 Apr 2024 16:23:47 -0500 Subject: [PATCH 01/11] test: mock the compute_bytecode functions in the sway contracts --- .../src/guide/types/vector.test.ts | 3 +-- .../forc-projects/bytecode-input/Forc.toml | 6 ------ .../forc-projects/bytecode-input/src/main.sw | 15 ++++----------- apps/docs/src/guide/types/vectors.md | 6 +----- .../fuel-gauge/src/bytecode-sway-lib.test.ts | 3 +-- .../forc-projects/bytecode-sway-lib/Forc.toml | 8 +------- .../bytecode-sway-lib/src/main.sw | 19 +++++-------------- 7 files changed, 13 insertions(+), 47 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index febc7b1c04..80895c6174 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -55,8 +55,7 @@ describe(__filename, () => { expect(value.isActive).toEqual(employees[1].isActive); }); - // TODO: Unskip test after sway-libs become compatible with latest forc (0.52+) - it.skip('should successfully execute a contract call with a bytecode input', async () => { + it('should successfully execute a contract call with a bytecode input', async () => { const bytecodeContract = await createAndDeployContractFromProject( DocSnippetProjectsEnum.BYTECODE_INPUT ); diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml index 416c953741..c5200eb2a0 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml @@ -2,9 +2,3 @@ authors = ["Fuel Labs "] license = "Apache-2.0" name = "bytecode-input" - -# TODO: Uncomment bytecode-related stuff -# This requires sway-libs to be compatible with latest forc (0.52+) - -# [dependencies] -# bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw index 601d0cc508..902d23d897 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw @@ -1,21 +1,14 @@ contract; -// TODO: Uncomment bytecode-related stuff -// This requires sway-libs to be compatible with latest forc (0.52+) -// use bytecode::*; - - abi MyContract { - // fn compute_bytecode_root(bytecode_input: Vec) -> b256; - fn compute_bytecode_root(bytecode_input: Vec) -> bool; + fn compute_bytecode_root(bytecode_input: Vec) -> b256; } impl MyContract for Contract { // #region vector-bytecode-input-sway - fn compute_bytecode_root(bytecode_input: Vec) -> bool { - // let root = compute_bytecode_root(bytecode_input); - // return root; - return true; + fn compute_bytecode_root(bytecode_input: Vec) -> b256 { + //simply return a fixed b256 value created from a hexadecimal string from testing purposes + return b256::from_hex_str(&bytecode_input).unwrap(); } // #endregion vector-bytecode-input-sway } diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 19f771b943..387ee33796 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -26,15 +26,11 @@ Some Sway functions require you to pass in bytecode to the function. The type of Take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode.sw), for example. - - - +<<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} ```rust contract; -use bytecode::*; - abi MyContract { fn compute_bytecode_root(bytecode_input: Vec) -> b256; } diff --git a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts index 7129afccf4..f80390bddb 100644 --- a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts +++ b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts @@ -9,8 +9,7 @@ import { getSetupContract } from './utils'; /** * @group node */ -// TODO: Unskip test after sway-libs become compatible with latest forc (0.52+) -describe.skip('bytecode computations', () => { +describe('bytecode computations', () => { test('compute_bytecode_root', async () => { const { binHexlified: bytecodeFromFile } = getFuelGaugeForcProject( FuelGaugeProjectsEnum.CALL_TEST_CONTRACT diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml index 3f1ac99ca7..e9b602969c 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml @@ -1,10 +1,4 @@ [project] authors = ["Fuel Labs "] license = "Apache-2.0" -name = "bytecode-sway-lib" - -# TODO: Uncomment bytecode-related stuff -# This requires sway-libs to be compatible with latest forc (0.52+) - -# [dependencies] -# bytecode = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.19.0" } +name = "bytecode-sway-lib" \ No newline at end of file diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw index c9ff9034fa..24a8d24182 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw @@ -1,33 +1,24 @@ contract; -// TODO: Uncomment bytecode-related stuff -// This requires sway-libs to be compatible with latest forc (0.52+) -// use bytecode::*; - - abi MyContract { fn compute_bytecode_root(bytecode_input: Vec); fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool; - // fn compute_predicate_address(bytecode: Vec) -> Address; - fn compute_predicate_address(bytecode: Vec) -> bool; + fn compute_predicate_address(bytecode: Vec) -> Address; } impl MyContract for Contract { fn compute_bytecode_root(bytecode_input: Vec) { - // let mut bytecode = bytecode_input; - // let root = compute_bytecode_root(bytecode); - // log(root); + // simply logs the hexidecimal b256 string of the bytecode input for testing purposes + log(&b256::from_hex_str(&bytecode_input).unwrap().to_hex_str()); } fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool { - // verify_contract_bytecode(contract_id, bytecode); return true; } - fn compute_predicate_address(bytecode: Vec) -> bool { - // return compute_predicate_address(bytecode); - return true; + fn compute_predicate_address(bytecode: Vec) -> Address { + return Address::from_slice(&b256::from_hex_str(&bytecode).unwrap().to_bytes()); } } From bebd31dc5a47ddf24edf8491e654043073267051 Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 26 Apr 2024 16:27:54 -0500 Subject: [PATCH 02/11] doc: add changeset --- .changeset/rotten-bananas-explode.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .changeset/rotten-bananas-explode.md diff --git a/.changeset/rotten-bananas-explode.md b/.changeset/rotten-bananas-explode.md new file mode 100644 index 0000000000..a845151cc8 --- /dev/null +++ b/.changeset/rotten-bananas-explode.md @@ -0,0 +1,2 @@ +--- +--- From 74d4405367c5edb9d2f78944828048f850b235b8 Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 26 Apr 2024 16:41:08 -0500 Subject: [PATCH 03/11] build: forc format --- .../test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml | 2 +- .../test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml index e9b602969c..9dd8936f5c 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml @@ -1,4 +1,4 @@ [project] authors = ["Fuel Labs "] license = "Apache-2.0" -name = "bytecode-sway-lib" \ No newline at end of file +name = "bytecode-sway-lib" diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw index 24a8d24182..2493badd2e 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw @@ -10,7 +10,7 @@ abi MyContract { impl MyContract for Contract { fn compute_bytecode_root(bytecode_input: Vec) { - // simply logs the hexidecimal b256 string of the bytecode input for testing purposes + // simply logs the hexidecimal b256 string of the bytecode input for testing purposes log(&b256::from_hex_str(&bytecode_input).unwrap().to_hex_str()); } From 0c26571b4d67cc365988493b0478981d984d14ed Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 30 Apr 2024 13:55:07 -0500 Subject: [PATCH 04/11] wip --- .../fixtures/forc-projects/bytecode-input/Forc.toml | 2 ++ .../fixtures/forc-projects/bytecode-input/src/main.sw | 2 +- apps/docs/src/guide/types/vectors.md | 2 +- .../forc-projects/bytecode-sway-lib/src/main.sw | 11 ++++++----- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml index c5200eb2a0..f6e52ea153 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/Forc.toml @@ -2,3 +2,5 @@ authors = ["Fuel Labs "] license = "Apache-2.0" name = "bytecode-input" + +[dependencies] diff --git a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw index 902d23d897..82ac781982 100644 --- a/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw +++ b/apps/docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw @@ -8,7 +8,7 @@ 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 b256::from_hex_str(&bytecode_input).unwrap(); + return 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; } // #endregion vector-bytecode-input-sway } diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 387ee33796..3ce039adec 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -36,7 +36,7 @@ abi MyContract { } impl MyContract for Contract { - fn compute_bytecode_root(bytecode_input: Vec) -> bool { + fn compute_bytecode_root(bytecode_input: Vec) -> b256 { let root = compute_bytecode_root(bytecode_input); return root; } diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw index 2493badd2e..b43cedc506 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw @@ -5,20 +5,21 @@ abi MyContract { fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool; - fn compute_predicate_address(bytecode: Vec) -> Address; + // fn compute_predicate_address(bytecode: Vec) -> Address; } impl MyContract for Contract { fn compute_bytecode_root(bytecode_input: Vec) { // simply logs the hexidecimal b256 string of the bytecode input for testing purposes - log(&b256::from_hex_str(&bytecode_input).unwrap().to_hex_str()); + let ret: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; + log(ret); } fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool { return true; } - fn compute_predicate_address(bytecode: Vec) -> Address { - return Address::from_slice(&b256::from_hex_str(&bytecode).unwrap().to_bytes()); - } + // fn compute_predicate_address(bytecode: Vec) -> Address { + // return EvmAddress::from(); + // } } From a7b0181afe091d2942c94a6c39410abfd381bc26 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 6 May 2024 18:49:31 -0500 Subject: [PATCH 05/11] test: update vector test --- .../src/guide/types/vector.test.ts | 4 +++- apps/docs/src/guide/types/vectors.md | 23 ++----------------- 2 files changed, 5 insertions(+), 22 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index 80895c6174..bf3e583d69 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -68,12 +68,14 @@ describe(__filename, () => { // #import { arrayify, readFile }; const bytecode = await readFile(bytecodePath); + const bytecodeAsVecU8 = arrayify(bytecode); const { value: bytecodeRoot } = await bytecodeContract.functions - .compute_bytecode_root(arrayify(bytecode)) + .compute_bytecode_root(Array.from(bytecodeAsVecU8)) .call(); // #endregion vector-bytecode-input-ts + console.log('bytecodeRoot:', bytecodeRoot); expect(bytecodeRoot).toBeDefined(); expect(bytecodeRoot.length).toBe(66); }); diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 3ce039adec..07b108ea73 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -20,28 +20,9 @@ The code snippet below demonstrates how to call this Sway contract method, which <<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-4{ts:line-numbers} -## Working with Bytecode in the SDK +## Converting Bytecode to Vec -Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`. - -Take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://github.com/FuelLabs/sway-libs/tree/master/libs/src/bytecode.sw), for example. - -<<< @/../../docs-snippets/test/fixtures/forc-projects/bytecode-input/src/main.sw#vector-bytecode-input-sway{ts:line-numbers} - -```rust -contract; - -abi MyContract { - fn compute_bytecode_root(bytecode_input: Vec) -> b256; -} - -impl MyContract for Contract { - fn compute_bytecode_root(bytecode_input: Vec) -> b256 { - let root = compute_bytecode_root(bytecode_input); - return root; - } -} -``` +Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually Vec. For example, take the compute_bytecode_root function from the [bytecode Sway library](https://fuellabs.github.io/sway-libs/book/bytecode/index.html?highlight=bytecode#using-the-bytecode-library-in-sway). 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: From 461c7ff2b0817f9e5d9ae862988be23f874ebebe Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 7 May 2024 16:20:14 -0500 Subject: [PATCH 06/11] test: updated tests --- apps/docs-snippets/src/guide/types/vector.test.ts | 1 - apps/docs/src/guide/types/vectors.md | 4 ++-- packages/fuel-gauge/src/bytecode-sway-lib.test.ts | 6 +++--- .../forc-projects/bytecode-sway-lib/src/main.sw | 11 +++++------ 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index bf3e583d69..7643be6c0e 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -75,7 +75,6 @@ describe(__filename, () => { .call(); // #endregion vector-bytecode-input-ts - console.log('bytecodeRoot:', bytecodeRoot); expect(bytecodeRoot).toBeDefined(); expect(bytecodeRoot.length).toBe(66); }); diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 07b108ea73..5649ad8b81 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -20,9 +20,9 @@ The code snippet below demonstrates how to call this Sway contract method, which <<< @/../../docs-snippets/src/guide/types/vector.test.ts#vector-4{ts:line-numbers} -## Converting Bytecode to Vec +## Converting Bytecode to Vectors -Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually Vec. For example, take the compute_bytecode_root function from the [bytecode Sway library](https://fuellabs.github.io/sway-libs/book/bytecode/index.html?highlight=bytecode#using-the-bytecode-library-in-sway). +Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`. For example, take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://fuellabs.github.io/sway-libs/book/bytecode/index.html?highlight=bytecode#using-the-bytecode-library-in-sway). 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: diff --git a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts index f80390bddb..0f2d40c225 100644 --- a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts +++ b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts @@ -19,7 +19,7 @@ describe('bytecode computations', () => { const contract = await setupContract(); const { logs } = await contract.functions - .compute_bytecode_root(arrayify(bytecodeFromFile)) + .compute_bytecode_root(Array.from(arrayify(bytecodeFromFile))) .call(); const bytecodeRoot: string = logs[0]; @@ -41,7 +41,7 @@ describe('bytecode computations', () => { { bits: contract.id.toB256(), }, - arrayify(bytecodeFromFile) + Array.from(arrayify(bytecodeFromFile)) ) .call(); @@ -63,7 +63,7 @@ describe('bytecode computations', () => { const contract = await setupContract(); const { value } = await contract.functions - .compute_predicate_address(arrayify(defaultPredicateBytecode)) + .compute_predicate_address(Array.from(arrayify(defaultPredicateBytecode))) .call(); expect(value.bits).toEqual(address.toB256()); diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw index b43cedc506..09d33c9eaf 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/src/main.sw @@ -5,21 +5,20 @@ abi MyContract { fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool; - // fn compute_predicate_address(bytecode: Vec) -> Address; + fn compute_predicate_address(bytecode: Vec) -> Address; } impl MyContract for Contract { fn compute_bytecode_root(bytecode_input: Vec) { // simply logs the hexidecimal b256 string of the bytecode input for testing purposes - let ret: b256 = 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20; - log(ret); + log(0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20); } fn verify_contract_bytecode(contract_id: ContractId, bytecode: Vec) -> bool { return true; } - // fn compute_predicate_address(bytecode: Vec) -> Address { - // return EvmAddress::from(); - // } + fn compute_predicate_address(bytecode: Vec) -> Address { + return Address::from(0x6b6ef590390f0a7de75f8275ab5d7877c17236caba2514039c6565ec15f79111); + } } From 83b379f5af82b2bf7cecc6dd970ed39f3cd8a3a6 Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 10 May 2024 20:56:55 -0500 Subject: [PATCH 07/11] test: increase test timeout --- .../src/guide/types/vector.test.ts | 42 ++++++++++--------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index 7643be6c0e..aa5a8cf8e3 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -55,27 +55,31 @@ describe(__filename, () => { expect(value.isActive).toEqual(employees[1].isActive); }); - it('should successfully execute a contract call with a bytecode input', async () => { - const bytecodeContract = await createAndDeployContractFromProject( - DocSnippetProjectsEnum.BYTECODE_INPUT - ); - const bytecodePath = join( - __dirname, - '../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin' - ); + it( + 'should successfully execute a contract call with a bytecode input', + async () => { + const bytecodeContract = await createAndDeployContractFromProject( + DocSnippetProjectsEnum.BYTECODE_INPUT + ); + const bytecodePath = join( + __dirname, + '../../../test/fixtures/forc-projects/bytecode-input/out/release/bytecode-input.bin' + ); - // #region vector-bytecode-input-ts - // #import { arrayify, readFile }; + // #region vector-bytecode-input-ts + // #import { arrayify, readFile }; - const bytecode = await readFile(bytecodePath); - const bytecodeAsVecU8 = arrayify(bytecode); + const bytecode = await readFile(bytecodePath); + const bytecodeAsVecU8 = arrayify(bytecode); - const { value: bytecodeRoot } = await bytecodeContract.functions - .compute_bytecode_root(Array.from(bytecodeAsVecU8)) - .call(); - // #endregion vector-bytecode-input-ts + const { value: bytecodeRoot } = await bytecodeContract.functions + .compute_bytecode_root(Array.from(bytecodeAsVecU8)) + .call(); + // #endregion vector-bytecode-input-ts - expect(bytecodeRoot).toBeDefined(); - expect(bytecodeRoot.length).toBe(66); - }); + expect(bytecodeRoot).toBeDefined(); + expect(bytecodeRoot.length).toBe(66); + }, + { timeout: 10000 } + ); }); From ebe239bc2594e41ceb575fb4d582b8a1cf6be3c3 Mon Sep 17 00:00:00 2001 From: chad Date: Fri, 10 May 2024 21:56:21 -0500 Subject: [PATCH 08/11] update Forc and tests --- apps/docs/src/guide/types/vectors.md | 2 ++ .../test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index 5649ad8b81..a0b2da8e5f 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -24,6 +24,8 @@ The code snippet below demonstrates how to call this Sway contract method, which Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`. For example, take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://fuellabs.github.io/sway-libs/book/bytecode/index.html?highlight=bytecode#using-the-bytecode-library-in-sway). +<<< @/../../docs-snippets/test/fixtures/forc-projects/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} diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml index 9dd8936f5c..67f6654dec 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml @@ -2,3 +2,5 @@ authors = ["Fuel Labs "] license = "Apache-2.0" name = "bytecode-sway-lib" + +[dependencies] \ No newline at end of file From a75b701eddb4221d7f2e903599d972f5f6bd02df Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 13 May 2024 14:27:07 -0500 Subject: [PATCH 09/11] test: remove unnecessary array conversion --- apps/docs-snippets/src/guide/types/vector.test.ts | 2 +- packages/fuel-gauge/src/bytecode-sway-lib.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/docs-snippets/src/guide/types/vector.test.ts b/apps/docs-snippets/src/guide/types/vector.test.ts index aa5a8cf8e3..c5e10928f2 100644 --- a/apps/docs-snippets/src/guide/types/vector.test.ts +++ b/apps/docs-snippets/src/guide/types/vector.test.ts @@ -73,7 +73,7 @@ describe(__filename, () => { const bytecodeAsVecU8 = arrayify(bytecode); const { value: bytecodeRoot } = await bytecodeContract.functions - .compute_bytecode_root(Array.from(bytecodeAsVecU8)) + .compute_bytecode_root(bytecodeAsVecU8) .call(); // #endregion vector-bytecode-input-ts diff --git a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts index 0f2d40c225..4621d0b293 100644 --- a/packages/fuel-gauge/src/bytecode-sway-lib.test.ts +++ b/packages/fuel-gauge/src/bytecode-sway-lib.test.ts @@ -19,7 +19,7 @@ describe('bytecode computations', () => { const contract = await setupContract(); const { logs } = await contract.functions - .compute_bytecode_root(Array.from(arrayify(bytecodeFromFile))) + .compute_bytecode_root(arrayify(bytecodeFromFile)) .call(); const bytecodeRoot: string = logs[0]; From 2f6af7dfb4bec2e792ea837fb852c87c2b2d103e Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 13 May 2024 14:29:58 -0500 Subject: [PATCH 10/11] docs: update docs example --- apps/docs/src/guide/types/vectors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/docs/src/guide/types/vectors.md b/apps/docs/src/guide/types/vectors.md index a0b2da8e5f..9c2f974f0f 100644 --- a/apps/docs/src/guide/types/vectors.md +++ b/apps/docs/src/guide/types/vectors.md @@ -22,7 +22,7 @@ The code snippet below demonstrates how to call this Sway contract method, which ## Converting Bytecode to Vectors -Some Sway functions require you to pass in bytecode to the function. The type of the bytecode parameter is usually `Vec`. For example, take the `compute_bytecode_root` function from the [`bytecode` Sway library](https://fuellabs.github.io/sway-libs/book/bytecode/index.html?highlight=bytecode#using-the-bytecode-library-in-sway). +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} From 7c217bd8ecd195a20debe3f057adbccc56ea8683 Mon Sep 17 00:00:00 2001 From: chad Date: Mon, 13 May 2024 16:53:36 -0500 Subject: [PATCH 11/11] test: update forc formatting issue --- .../test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml index 67f6654dec..b4a52117f8 100644 --- a/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml +++ b/packages/fuel-gauge/test/fixtures/forc-projects/bytecode-sway-lib/Forc.toml @@ -3,4 +3,4 @@ authors = ["Fuel Labs "] license = "Apache-2.0" name = "bytecode-sway-lib" -[dependencies] \ No newline at end of file +[dependencies]