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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,7 +41,7 @@ | |
let subscriptions: Subscriptions = Subscriptions::default(); | ||
let subscriptions_for_closure = subscriptions.clone(); | ||
let msg_handler = Closure::wrap(Box::new(move |evt_js: JsValue| { | ||
let evt = evt_js.into_serde::<MessageEvent>().expect("Couldn't parse event data"); | ||
Check warning on line 44 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
log::trace!("Message from provider: {:?}", evt); | ||
match evt.event_type.as_str() { | ||
"eth_subscription" => { | ||
|
@@ -71,7 +71,7 @@ | |
pub fn connect_stream(&self) -> impl Stream<Item = Option<String>> { | ||
self.handle_ad_hoc_event("connect", |evt_js| { | ||
let evt = evt_js | ||
.into_serde::<ConnectEvent>() | ||
Check warning on line 74 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
.expect("couldn't parse connect event"); | ||
evt.chain_id | ||
}) | ||
|
@@ -81,7 +81,7 @@ | |
/// above. | ||
pub fn disconnect_stream(&self) -> impl Stream<Item = jsonrpc_core::Error> { | ||
self.handle_ad_hoc_event("disconnect", |evt_js| { | ||
evt_js.into_serde().expect("deserializing disconnect error failed") | ||
Check warning on line 84 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
}) | ||
} | ||
|
||
|
@@ -132,6 +132,29 @@ | |
.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 { | ||
Check warning on line 138 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
chain_id: chain_id.to_string(), | ||
}]) | ||
.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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
pub chain_id: String, | ||
} | ||
|
||
/// Event data sent from the JavaScript side to our callback. | ||
|
@@ -179,7 +202,7 @@ | |
.. | ||
}) => { | ||
let js_params = | ||
js_sys::Array::from(&JsValue::from_serde(¶ms).expect("couldn't send method params via JSON")); | ||
Check warning on line 205 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
let copy = self.provider_and_listeners.borrow().provider.clone(); | ||
Box::pin(async move { | ||
copy.request_wrapped(RequestArguments { | ||
|
@@ -263,8 +286,8 @@ | |
} | ||
|
||
let parsed_value = resp | ||
.map(|res| res.into_serde()) | ||
Check warning on line 289 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
.map_err(|err| err.into_serde::<RPCErrorExtra>()); | ||
Check warning on line 290 in src/transports/eip_1193.rs GitHub Actions / Check WASM
|
||
match parsed_value { | ||
Ok(Ok(res)) => Ok(res), | ||
Err(Ok(err)) => Err(Error::Rpc(err.into())), | ||
|
@@ -340,14 +363,14 @@ | |
|
||
fn json_to_js(json: &str) -> JsValue { | ||
let json_value = serde_json::from_str::<serde_json::Value>(json).unwrap(); | ||
let js_value = JsValue::from_serde(&json_value).unwrap(); | ||
js_value | ||
} | ||
|
||
#[wasm_bindgen_test] | ||
fn parses_valid_response_correctly() { | ||
let value = serde_json::from_str::<serde_json::Value>(r#"[1, false, null, "string"]"#).unwrap(); | ||
let response = Ok(JsValue::from_serde(&value).unwrap()); | ||
let expected_result = Ok(value); | ||
assert_eq!(Provider::parse_response(response), expected_result); | ||
|
||
|
There was a problem hiding this comment.
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.