From 1b2139ff0cd40c844400d16c97c57d9d542c6f85 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 31 May 2022 18:18:00 -0400 Subject: [PATCH 01/11] Allow WASM compatibility --- packages/fuels-core/src/code_gen/abigen.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fuels-core/src/code_gen/abigen.rs b/packages/fuels-core/src/code_gen/abigen.rs index 73a4eb2395..7fd7bfbc56 100644 --- a/packages/fuels-core/src/code_gen/abigen.rs +++ b/packages/fuels-core/src/code_gen/abigen.rs @@ -149,7 +149,7 @@ impl Abigen { #![allow(unused_imports)] #includes - use fuels::core::{Detokenize, EnumSelector, ParamType, Tokenizable, Token}; + use fuels_core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token}; #code From 07fe1e255dc711676cbc47e61f37a240e73900ac Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 31 May 2022 18:43:28 -0400 Subject: [PATCH 02/11] Add wasm-test --- Cargo.toml | 1 + packages/fuels-core/src/code_gen/abigen.rs | 1 - packages/wasm-tests/.cargo/config.toml | 2 + packages/wasm-tests/Cargo.toml | 12 +++++ packages/wasm-tests/src/lib.rs | 58 ++++++++++++++++++++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 packages/wasm-tests/.cargo/config.toml create mode 100644 packages/wasm-tests/Cargo.toml create mode 100644 packages/wasm-tests/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index cc58ac0e29..068d4115b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ members = [ "packages/fuels-signers", "packages/fuels-test-helpers", "packages/fuels-types", + "packages/wasm-tests", "tools/fuels-abi-cli", "scripts/build-test-projects", "examples/contracts", diff --git a/packages/fuels-core/src/code_gen/abigen.rs b/packages/fuels-core/src/code_gen/abigen.rs index 7fd7bfbc56..dc6ea380d6 100644 --- a/packages/fuels-core/src/code_gen/abigen.rs +++ b/packages/fuels-core/src/code_gen/abigen.rs @@ -116,7 +116,6 @@ impl Abigen { ( quote! { use fuels::contract::contract::{Contract, ContractCall}; - use fuels::prelude::InvalidOutputType; use fuels::signers::LocalWallet; use fuels::tx::{ContractId, Address}; use std::str::FromStr; diff --git a/packages/wasm-tests/.cargo/config.toml b/packages/wasm-tests/.cargo/config.toml new file mode 100644 index 0000000000..f4e8c002fc --- /dev/null +++ b/packages/wasm-tests/.cargo/config.toml @@ -0,0 +1,2 @@ +[build] +target = "wasm32-unknown-unknown" diff --git a/packages/wasm-tests/Cargo.toml b/packages/wasm-tests/Cargo.toml new file mode 100644 index 0000000000..915f7906a9 --- /dev/null +++ b/packages/wasm-tests/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "wasm-tests" +version = "0.1.0" +edition = "2021" +publish = false + +[lib] +crate-type = ['cdylib'] + +[dependencies] +fuels-abigen-macro = { path = "../fuels-abigen-macro" } +fuels-core = { path = "../fuels-core" } diff --git a/packages/wasm-tests/src/lib.rs b/packages/wasm-tests/src/lib.rs new file mode 100644 index 0000000000..e0471b45fa --- /dev/null +++ b/packages/wasm-tests/src/lib.rs @@ -0,0 +1,58 @@ +extern crate alloc; +use fuels_abigen_macro::wasm_abigen; + +wasm_abigen!( + no_name, + r#"[ + { + "type":"contract", + "inputs":[ + { + "name":"SomeEvent", + "type":"struct SomeEvent", + "components": [ + { + "name": "id", + "type": "u64" + }, + { + "name": "account", + "type": "b256" + } + ] + }, + { + "name":"AnotherEvent", + "type":"struct AnotherEvent", + "components": [ + { + "name": "id", + "type": "u64" + }, + { + "name": "hash", + "type": "b256" + }, + { + "name": "bar", + "type": "bool" + } + ] + } + ], + "name":"takes_struct", + "outputs":[] + } + ] + "# +); + + +fn main() { + let a_struct = SomeEvent { + id: 20, + account: Default::default(), + }; + + println!("It works! {}", a_struct.id); +} From f297f27c78623da3ee0acb7a839edf80783985be Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 31 May 2022 18:48:16 -0400 Subject: [PATCH 03/11] Fix --- packages/wasm-tests/Cargo.toml | 1 + packages/wasm-tests/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/wasm-tests/Cargo.toml b/packages/wasm-tests/Cargo.toml index 915f7906a9..0a162979c2 100644 --- a/packages/wasm-tests/Cargo.toml +++ b/packages/wasm-tests/Cargo.toml @@ -10,3 +10,4 @@ crate-type = ['cdylib'] [dependencies] fuels-abigen-macro = { path = "../fuels-abigen-macro" } fuels-core = { path = "../fuels-core" } +getrandom = { version = "0.2", features = ["js"] } diff --git a/packages/wasm-tests/src/lib.rs b/packages/wasm-tests/src/lib.rs index e0471b45fa..737d7bed99 100644 --- a/packages/wasm-tests/src/lib.rs +++ b/packages/wasm-tests/src/lib.rs @@ -48,7 +48,7 @@ wasm_abigen!( ); -fn main() { +pub fn the_fn() { let a_struct = SomeEvent { id: 20, account: Default::default(), From 52acd70f645ae9b5682c64ca23d602316b55cdb1 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 31 May 2022 19:02:43 -0400 Subject: [PATCH 04/11] fmt --- packages/wasm-tests/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/wasm-tests/src/lib.rs b/packages/wasm-tests/src/lib.rs index 737d7bed99..4b9d2f5419 100644 --- a/packages/wasm-tests/src/lib.rs +++ b/packages/wasm-tests/src/lib.rs @@ -47,7 +47,6 @@ wasm_abigen!( "# ); - pub fn the_fn() { let a_struct = SomeEvent { id: 20, From a976e1251bc2698b89611bfab57374c4ea982778 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 31 May 2022 19:27:54 -0400 Subject: [PATCH 05/11] Fix build --- packages/fuels-core/src/code_gen/abigen.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/fuels-core/src/code_gen/abigen.rs b/packages/fuels-core/src/code_gen/abigen.rs index dc6ea380d6..b2228fea5e 100644 --- a/packages/fuels-core/src/code_gen/abigen.rs +++ b/packages/fuels-core/src/code_gen/abigen.rs @@ -109,6 +109,7 @@ impl Abigen { ( quote! { use alloc::{vec, vec::Vec}; + use fuels_core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token}; }, quote! {}, ) @@ -118,6 +119,7 @@ impl Abigen { use fuels::contract::contract::{Contract, ContractCall}; use fuels::signers::LocalWallet; use fuels::tx::{ContractId, Address}; + use fuels::core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token}; use std::str::FromStr; }, quote! { @@ -148,7 +150,6 @@ impl Abigen { #![allow(unused_imports)] #includes - use fuels_core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token}; #code From 05119ddb10ce16f2c19663adb8450beed6688478 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Wed, 1 Jun 2022 13:51:32 -0400 Subject: [PATCH 06/11] Update version --- packages/wasm-tests/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/wasm-tests/Cargo.toml b/packages/wasm-tests/Cargo.toml index 0a162979c2..d1c9ee6471 100644 --- a/packages/wasm-tests/Cargo.toml +++ b/packages/wasm-tests/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wasm-tests" -version = "0.1.0" +version = "0.0.0" edition = "2021" publish = false From f9550e36deb7ba8d539c0145dc14677a8101298f Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Fri, 3 Jun 2022 12:56:10 -0400 Subject: [PATCH 07/11] Add WASM test harness --- .github/workflows/ci.yml | 6 +++++ packages/wasm-tests/.cargo/config.toml | 3 +++ packages/wasm-tests/Cargo.toml | 3 +++ packages/wasm-tests/src/lib.rs | 33 ++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 578e2d25ea..fd3a49a5c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,6 +50,12 @@ jobs: chmod +x forc-binaries/forc mv forc-binaries/forc /usr/local/bin/forc + - name: Install WebAssembly Test harness + uses: actions-rs/cargo@v1 + with: + command: install + args: webassembly-test-runner + - name: Build Sway Examples uses: actions-rs/cargo@v1 with: diff --git a/packages/wasm-tests/.cargo/config.toml b/packages/wasm-tests/.cargo/config.toml index f4e8c002fc..00ec8ee28d 100644 --- a/packages/wasm-tests/.cargo/config.toml +++ b/packages/wasm-tests/.cargo/config.toml @@ -1,2 +1,5 @@ [build] target = "wasm32-unknown-unknown" + +[target.wasm32-unknown-unknown] +runner = "webassembly-test-runner" diff --git a/packages/wasm-tests/Cargo.toml b/packages/wasm-tests/Cargo.toml index d1c9ee6471..ce0a91c593 100644 --- a/packages/wasm-tests/Cargo.toml +++ b/packages/wasm-tests/Cargo.toml @@ -11,3 +11,6 @@ crate-type = ['cdylib'] fuels-abigen-macro = { path = "../fuels-abigen-macro" } fuels-core = { path = "../fuels-core" } getrandom = { version = "0.2", features = ["js"] } + +[dev-dependencies] +webassembly-test = "0.1" diff --git a/packages/wasm-tests/src/lib.rs b/packages/wasm-tests/src/lib.rs index 4b9d2f5419..57e18c4a1d 100644 --- a/packages/wasm-tests/src/lib.rs +++ b/packages/wasm-tests/src/lib.rs @@ -47,11 +47,34 @@ wasm_abigen!( "# ); + pub fn the_fn() { - let a_struct = SomeEvent { - id: 20, - account: Default::default(), - }; + use fuels_core::{abi_decoder::ABIDecoder, ParamType}; + let data = vec![ + 0, 0, 0, 0, 0, 0, 3, 252, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, + 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, 175, + 175, + ]; + + let mut decoder = ABIDecoder::new(); + + let obj = decoder + .decode(&[ParamType::U64, ParamType::B256], &data) + .expect("Failed to decode"); + + let a_struct = SomeEvent::new_from_tokens(&obj); + + assert_eq!(1020, a_struct.id); +} + + +#[cfg(test)] +mod tests { + use super::*; + use webassembly_test::webassembly_test; - println!("It works! {}", a_struct.id); + #[webassembly_test] + fn test() { + the_fn(); + } } From 40bdcd5564224ce86ee85a3aeff7b629f8529727 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Fri, 3 Jun 2022 13:07:24 -0400 Subject: [PATCH 08/11] fmt --- packages/wasm-tests/src/lib.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/wasm-tests/src/lib.rs b/packages/wasm-tests/src/lib.rs index 57e18c4a1d..34ec583570 100644 --- a/packages/wasm-tests/src/lib.rs +++ b/packages/wasm-tests/src/lib.rs @@ -47,7 +47,6 @@ wasm_abigen!( "# ); - pub fn the_fn() { use fuels_core::{abi_decoder::ABIDecoder, ParamType}; let data = vec![ @@ -67,7 +66,6 @@ pub fn the_fn() { assert_eq!(1020, a_struct.id); } - #[cfg(test)] mod tests { use super::*; From e38398df32c846f8cf13cc79485469a12088579a Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 7 Jun 2022 10:07:10 -0400 Subject: [PATCH 09/11] Add CI step --- .github/workflows/ci.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fd3a49a5c9..6ab51857e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -137,6 +137,17 @@ jobs: with: key: "${{ matrix.command }} ${{ matrix.args }} ${{ matrix.package }}" + - name: Add WASM target + if: ${{ matrix.command == 'test' }} + run: rustup target add wasm32-unknown-unknown + + - name: Test WASM package + if: ${{ matrix.command == 'test' }} + run: | + cd packages/wasm-test + cargo test --target wasm32-unknown-unknown --all-targets --all-features + cargo test --target wasm32-unknown-unknown --all-targets --no-default-features + - name: Install rustfmt if: ${{ matrix.command == 'fmt' }} run: rustup component add rustfmt From 272b918f485f584322ec0cad6c9a3e91926aea17 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 7 Jun 2022 11:24:40 -0400 Subject: [PATCH 10/11] typo --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ab51857e3..b5df69dc2a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -144,7 +144,7 @@ jobs: - name: Test WASM package if: ${{ matrix.command == 'test' }} run: | - cd packages/wasm-test + cd packages/wasm-tests cargo test --target wasm32-unknown-unknown --all-targets --all-features cargo test --target wasm32-unknown-unknown --all-targets --no-default-features From 89cab886bee71ebe8b73c50da54ef36bf33da105 Mon Sep 17 00:00:00 2001 From: TJ Sharp Date: Tue, 7 Jun 2022 11:33:57 -0400 Subject: [PATCH 11/11] Wrong block --- .github/workflows/ci.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b5df69dc2a..2ef15fa758 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,12 +50,6 @@ jobs: chmod +x forc-binaries/forc mv forc-binaries/forc /usr/local/bin/forc - - name: Install WebAssembly Test harness - uses: actions-rs/cargo@v1 - with: - command: install - args: webassembly-test-runner - - name: Build Sway Examples uses: actions-rs/cargo@v1 with: @@ -141,6 +135,13 @@ jobs: if: ${{ matrix.command == 'test' }} run: rustup target add wasm32-unknown-unknown + - name: Install WebAssembly Test harness + if: ${{ matrix.command == 'test' }} + uses: actions-rs/cargo@v1 + with: + command: install + args: webassembly-test-runner + - name: Test WASM package if: ${{ matrix.command == 'test' }} run: |