Expand description

ethereum-provider

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).

Installation

cargo add ethereum-provider
ethereum-provider = "0.2.0"

Features

  • yew (optional): provides a hook, use_provider, which simplifies the interaction with the provider when using yew

Examples

use ethereum_provider::{Provider, ProviderError};
use web_sys::window;

// 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);

Yew examples

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>
    }
}

Re-exports

Modules