Skip to content

Conversation

@ltfschoen
Copy link
Contributor

@ltfschoen ltfschoen commented May 13, 2023

Usage

Install Node.js and dependencies. Build JS to ESM. Run in browser.

cd examples/npm_lib
rm -rf node_modules
rm -rf yarn.lock
nvm use
nvm install
npm install -g yarn
yarn
yarn run esbuild
rustup update
rustup target add wasm32-unknown-unknown
cargo install --locked trunk
RUST_LOG=info trunk serve --address=127.0.0.1 --open

Click "Connect" in the UI at http://127.0.0.1:8080 and view it use Polkadot.js API to fetch and display a genesis hash

Test

cargo test --target wasm32-unknown-unknown

Issue Status

  • - Note: I'm not sure how to get it to pass the CI since the .mjs file is gitignored until the user runs yarn run esbuild Perhaps I need a #[allow(???)] https://doc.rust-lang.org/rustc/lints/listing/index.html
  • - Figure out how to run the tests since for wasm_bindgen_test it says "One other difference is that the tests must be in the root of the crate, or within a pub mod" and the implementation is in src/
  • For some reason, if i run kobold/ci/npm_lib.sh using Miri (i.e. ... cargo +nightly miri test ...) the CI passes even if i have a test that should fail, but if don't use Miri (i.e. ... cargo +nightly test ...) the CI doesn't pass, and it outputs errors as if that command was run in the parent kobold directory, even though it is being run in the kobold/examples/npm_lib/ folder, where it works and should output different messages the same as it does when it's run locally (i.e. it should be outputting DEBUG walrus::module)
  • wasm-pack wrapper not working
  • wasmtime alternative not working
2025-03-18T05:11:18.5535134Z Error: failed to run main module `./target/wasm32-unknown-unknown/debug/kobold_npm_lib_example.wasm`
2025-03-18T05:11:18.5535142Z 
2025-03-18T05:11:18.5535209Z Caused by:
2025-03-18T05:11:18.5535450Z     0: failed to instantiate "./target/wasm32-unknown-unknown/debug/kobold_npm_lib_example.wasm"
2025-03-18T05:11:18.5535660Z     1: unknown import: `__wbindgen_placeholder__::__wbindgen_describe` has not been defined
2025-03-18T05:11:18.5535664Z 
2025-03-18T05:11:18.5535738Z Stack backtrace:
2025-03-18T05:11:18.5535898Z    0: anyhow::error::<impl core::convert::From<E> for anyhow::Error>::from

const wsProvider = new WsProvider('wss://rpc.polkadot.io');
const api = await ApiPromise.create({ provider: wsProvider });

return api.genesisHash.toHex();
Copy link
Owner

Choose a reason for hiding this comment

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

What's the type of genesisHash? You could potentially send raw binary from Uint8Array to Rust via &[u8] or Vec<u8> much more cheaply than via string (no Utf16 to Utf8 conversion necessary).

Copy link
Contributor Author

@ltfschoen ltfschoen May 31, 2023

Choose a reason for hiding this comment

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

it's of type Hash that extends H256, and H256 extends U8aFixed, and U8aFixed extends Raw, which is a wrapper
around Uint8Array that implements IU8a.
i tried to send Uint8Array as you mentioned in this commit d921638, but not sure how to convert the bytes to string.
i also tried https://crates.io/crates/serde-wasm-bindgen/0.4.2 serde_wasm_bindgen::from_value without any luck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants