snaps icon indicating copy to clipboard operation
snaps copied to clipboard

I cannot initialize my WebAssembly built from Rust. It told me failed to fetch when I call init()

Open martinrhan opened this issue 1 year ago • 2 comments

Hi, I am trying to add the WebAssembly I built from Rust by this guide into my Snap. I added the pkg folder into /packages/snap

In my index.tsx, I added import init, { greet, get_hello_world_string } from '../pkg/hello_wasm.js';.

Then I was trying to add await init('../pkg/hello_wasm_bg.wasm'); into onRpcRequest. I did not work and I was given error "failed to fetch" when I click send message in the webpage of localhost:8000.

I also tried await fetch('./hello_wasm_bg.wasm'); and await fetch('../pkg/hello_wasm_bg.wasm'); They were all giving the same error failed to fetch.

I already added "endowment:network-access": {}, "endowment:webassembly": {} into my snap.manifest.json

Can any tell me how can I add my WebAssembly correctly into my Snap?

martinrhan avatar Jan 23 '25 07:01 martinrhan

For improved compatibility with WebAssembly, make sure you are running the latest Snaps CLI and have the experimental wasm flag enabled: https://github.com/MetaMask/snaps/blob/main/packages/examples/packages/wasm/snap.config.ts#L15-L17

Once you've done this you should be able to import the WASM bundle and bindgen code without requiring fetch!

Let me know if you run into more problems!

FrederikBolding avatar Jan 23 '25 10:01 FrederikBolding

I tried to put hello_wasm_bg.wasm onto liveserver and fetch it via http, now failed to fetch no longer appear, but I got this error instead

Failed to execute 'compile' on 'WebAssembly': An argument must be provided, which must be a Response or Promise<Response> object

martinrhan avatar Jan 24 '25 01:01 martinrhan