Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat: wasm support #390

Merged
merged 34 commits into from
Aug 23, 2021
Merged

feat: wasm support #390

merged 34 commits into from
Aug 23, 2021

Conversation

mattsse
Copy link
Collaborator

@mattsse mattsse commented Aug 18, 2021

Motivation

Add wasm support

Solution

  • make everything compile with --target wasm32-unknown-unknown. the biggest challenge is probably disabling transports.
  • implement a wasm compatible WS provider using web_sys::WebSocket
  • make sure all abi generated stuff is wasm compatible.

Afterwards we can look at how to shrink it etc.

@gakonst
Copy link
Owner

gakonst commented Aug 19, 2021

Nice start - we should also add a CI job for it

@mattsse
Copy link
Collaborator Author

mattsse commented Aug 21, 2021

Some parts work, however anything related to deploying and interacting hangs, could be due to misconfig on my end 🤷‍♂️ or the wasm package is too large.

Update

added wasm-pack example in examples/ethers-wasm.
Found the bug that prevented tx, and everything seems to work.
I ported the contract_with_abi example to ethers-wasm/tests.
To run it:

cd examples/ethers-wasm
yarn install
# start ganache in separate shell
yarn ganache
wasm-pack test --headless --firefox
# alternatively check in browser
yarn serve

This executes abigen, deploys the contract and sents tx, the test logs to console which should contain:

console.log div contained:
    starting
    deployed to 0xdcda…d7f2
    Value: hi. Logs: JsValue([Object({"author":"0xdad3bedb88f52b415a1aedf281213d8380747960","old_value":"","new_value":"initial value"}), Object({"author":"0xdad3bedb88f52b415a1aedf281213d8380747960","old_value":"initial value","new_value":"hi"})])

The two Websocket implementations are very similar and could probably easily be combined with some additional cfg's, based on #399

Unified both websocket implementations with only a few extra cfg's

@mattsse mattsse marked this pull request as ready for review August 21, 2021 17:36
Copy link
Owner

@gakonst gakonst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, follow ups from our discussion:

  • Simplify the example, remove the need for webpack and make it as "vanilla" as possible
  • NodeJS does not seem to be working right now. Unclear if it's a bug in how we do the WASM inside Rust, or if it's a config issue.

ethers-providers/Cargo.toml Outdated Show resolved Hide resolved
ethers-providers/Cargo.toml Outdated Show resolved Hide resolved
cfg-if = "1.0.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be used anywhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, we don't actually use this directly, however we need to set this for it to compile standalone:
https://docs.rs/getrandom/0.2.3/getrandom/#indirect-dependencies

@gakonst gakonst merged commit ea56666 into gakonst:master Aug 23, 2021
meetmangukiya pushed a commit to meetmangukiya/ethers-rs that referenced this pull request Mar 21, 2022
* abi detection when sending

* fix doctests
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WASM Support
2 participants