diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3c956b148c..4554973d23 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -131,6 +131,24 @@ 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: 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: | + 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 + - name: Install rustfmt if: ${{ matrix.command == 'fmt' }} run: rustup component add rustfmt 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 24288280af..046f1e964a 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! {}, ) @@ -116,9 +117,9 @@ impl Abigen { ( quote! { use fuels::contract::contract::{Contract, ContractCall}; - use fuels::prelude::InvalidOutputType; use fuels::signers::LocalWallet; use fuels::tx::{ContractId, Address}; + use fuels::core::{Detokenize, EnumSelector, InvalidOutputType, ParamType, Tokenizable, Token}; use std::str::FromStr; use fuels::core::{Detokenize, EnumSelector, ParamType, Tokenizable, Token, Parameterize}; }, diff --git a/packages/wasm-tests/.cargo/config.toml b/packages/wasm-tests/.cargo/config.toml new file mode 100644 index 0000000000..00ec8ee28d --- /dev/null +++ b/packages/wasm-tests/.cargo/config.toml @@ -0,0 +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 new file mode 100644 index 0000000000..ce0a91c593 --- /dev/null +++ b/packages/wasm-tests/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "wasm-tests" +version = "0.0.0" +edition = "2021" +publish = false + +[lib] +crate-type = ['cdylib'] + +[dependencies] +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 new file mode 100644 index 0000000000..34ec583570 --- /dev/null +++ b/packages/wasm-tests/src/lib.rs @@ -0,0 +1,78 @@ +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":[] + } + ] + "# +); + +pub fn the_fn() { + 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; + + #[webassembly_test] + fn test() { + the_fn(); + } +}