Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the wallet_switchEthereumChain method to the eip1193 transport #657

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/transports/eip_1193.rs
Expand Up @@ -132,6 +132,29 @@ impl Eip1193 {
.into_deserializer();
O::deserialize(deserializer).expect(&format!("couldn't deserialize {}", name))
}

/// EIP-3326: Switch a wallet to another chain
pub async fn switch_chain(&self, chain_id: &str) -> Result<serde_json::value::Value, error::Error> {
let js_params = JsValue::from_serde(&vec![&ChainId {
chain_id: chain_id.to_string(),
}])
Comment on lines +138 to +140
Copy link
Owner

Choose a reason for hiding this comment

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

might be easier to use json! macro for such a simple parameter.

.expect("couldn't send method params via JSON");

self.provider_and_listeners
.borrow()
.provider
.request_wrapped(RequestArguments {
method: String::from("wallet_switchEthereumChain"),
params: js_sys::Array::from(&js_params),
})
.await
}
}

#[derive(serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ChainId {
Copy link
Owner

Choose a reason for hiding this comment

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

Please move the struct declaration into the function, since I don't think it's required anywhere outside it (or just use json! internally)

pub chain_id: String,
}

/// Event data sent from the JavaScript side to our callback.
Expand Down