Crates.io | ethereum-provider |
lib.rs | ethereum-provider |
version | 0.2.0 |
source | src |
created_at | 2023-08-04 15:50:03.354379 |
updated_at | 2023-08-09 14:28:56.960905 |
description | EIP-1193 Ethereum Provider API for Rust |
homepage | https://github.com/LouisBrunner/rs-ethereum-browser-tools |
repository | https://github.com/LouisBrunner/rs-ethereum-browser-tools |
max_upload_size | |
id | 935352 |
size | 27,207 |
This project implements a Provider
type which wraps the browser's window.ethereum
for use in Rust, which is useful for wasm-based projects (e.g. front-ends).
cargo add ethereum-provider
ethereum-provider = "0.2.0"
yew
(optional): provides a hook, use_provider
, which simplifies the interaction with the provider when using yew
use ethereum_provider::{Provider, ProviderError};
use web_sys::window;
# async fn foo() -> Result<(), Box<dyn std::error::Error>> {
// create a provider
let provider = Provider::new(&window().unwrap())?;
// request accounts
let v = provider.request::<()>("eth_requestAccounts".to_string(), None).await?;
println!("eth_requestAccounts: {:?}", v);
// or use the convenience method
let v = provider.request_accounts().await?;
println!("accounts: {:?}", v);
# Ok(())
# }
use ethereum_provider::yew::use_provider;
use yew::prelude::*;
#[function_component]
fn Wallet() -> Html {
let status = use_provider();
html! {
<div>
{
match status {
Some(status) => match status {
Ok(status) => html! {
<div>
<pre>{ format!("Wallet: {:?}", status) }</pre>
</div>
},
Err(e) => html! { <pre>{ format!("Error: {:?}", e) }</pre> },
},
None => html! { <pre>{ "Loading wallet provider..." }</pre> },
}
}
</div>
}
}