In javascript to connect metamask it uses window.ethereum
if (window.ethereum) {
window.ethereum
.request({ method: "eth_requestAccounts" })
.then((res) => accountChangeHandler(res[0]));
} else {
alert("install metamask extension!!");
}
How to do that in yew.rs?
if web_sys::window().unwrap().ethereum {
let eth = web_sys::window().unwrap().ethereum;
log!(eth);
} else {
log!("install metamask extension!!");
}
Code error: no field ethereum on type Window
It seems
etherumis something created by some script you have somewhere and not a standard DOM property. (https://developer.mozilla.org/en-US/search?q=ethereum)web-sysis just a convience library created on top ofwasm-bindgenthat gives easier access to the DOM API.Still you have several ways to get the
etherumvalue:wasm-bindgenhttps://rustwasm.github.io/docs/wasm-bindgen/reference/js-snippets.html#using-inline_jsReflect, you will needjs-syslibrary for this, because web-sys has no binding for it for some reason.js_sys::Reflect::get(&window, &"ethereum".into());