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

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/transports/eip_1193.rs
Original file line number Diff line number Diff line change
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