russellwmy / yew-dapp-examples

Build Dapp with yew.rs
8 stars 4 forks source link

This is dark magic #5

Open g00nix opened 2 years ago

g00nix commented 2 years ago

Hello Russel,

First of all, congrats on making this work. I find this repo exceptionally cool. You earned your star.

I was under the impression that a JS binding was required in order to connect to phantom, like so: in your Rust app:

#[wasm_bindgen(module = "/js/solana.js")]
extern "C" {
    #[wasm_bindgen(catch)]
    async fn connect_to_wallet() -> Result<JsValue, JsValue>;
}

and in /js/solana.js:

export async function connect_to_wallet() {
  try {
    const resp = await window.solana.connect();
    return resp.publicKey.toString();
  } catch (err) {
    return err;
  }
}

However I see all you have is https://github.com/russellwmy/yew-dapp-examples/blob/main/solana-dapp-example/src/phantom.rs.

I suspect wasm can do an implicit binding to JS Objects that already exist under window., but I think some extra documentation (maybe comments) would help to better understand the behavior.

Thank you, g00nix

russellwmy commented 2 years ago

@g00nix good question. You can use the window object for the purpose. But when we use Rust, we want strong typed api. That's why I bind the solana.js instead of call the connect function directly.

BTW, I am working on a native RPC api in Rust. You can check it out. It would be better. https://github.com/russellwmy/web3-rs

g00nix commented 2 years ago

BTW, I am working on a native RPC api in Rust. You can check it out. It would be better. https://github.com/russellwmy/web3-rs

looks great! I will keep an eye on that!

That's why I bind the solana.js instead of call the connect function directly.

Regarding this, do you know how we could suppress this warning?

warning: unused manifest key: package.author
warning: static variable `solana` should have an upper case name
  --> src/phantom.rs:13:16
   |
13 |     pub static solana: Solana;
   |                ^^^^^^ help: convert the identifier to upper case: `SOLANA`
   |
   = note: `#[warn(non_upper_case_globals)]` on by default

warning: `solana-dapp-example` (bin "solana-dapp-example") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.23s
russellwmy commented 2 years ago

BTW, I am working on a native RPC api in Rust. You can check it out. It would be better. https://github.com/russellwmy/web3-rs

looks great! I will keep an eye on that!

That's why I bind the solana.js instead of call the connect function directly.

Regarding this, do you know how we could suppress this warning?

warning: unused manifest key: package.author
warning: static variable `solana` should have an upper case name
  --> src/phantom.rs:13:16
   |
13 |     pub static solana: Solana;
   |                ^^^^^^ help: convert the identifier to upper case: `SOLANA`
   |
   = note: `#[warn(non_upper_case_globals)]` on by default

warning: `solana-dapp-example` (bin "solana-dapp-example") generated 1 warning
    Finished dev [unoptimized + debuginfo] target(s) in 0.23s

You can use #[allow(non_upper_case_globals)]

g00nix commented 2 years ago

@russellwmy image does this work for you?