diff --git a/Cargo.lock b/Cargo.lock index 292f819a..937f83d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -251,6 +251,15 @@ dependencies = [ "toml 0.5.11", ] +[[package]] +name = "async-lock" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" +dependencies = [ + "event-listener", +] + [[package]] name = "async-trait" version = "0.1.73" @@ -487,6 +496,15 @@ dependencies = [ "generic-array 0.12.4", ] +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array 0.14.7", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -1551,6 +1569,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "event-listener" +version = "2.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" + [[package]] name = "eyre" version = "0.6.8" @@ -1901,6 +1925,27 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b" +[[package]] +name = "gloo-net" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ac9e8288ae2c632fa9f8657ac70bfe38a1530f345282d7ba66a1f70b72b7dc4" +dependencies = [ + "futures-channel", + "futures-core", + "futures-sink", + "gloo-utils", + "http", + "js-sys", + "pin-project", + "serde", + "serde_json", + "thiserror", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + [[package]] name = "gloo-timers" version = "0.2.6" @@ -1913,6 +1958,19 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gloo-utils" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5555354113b18c547c1d3a98fbf7fb32a9ff4f6fa112ce823a21641a0ba3aa" +dependencies = [ + "js-sys", + "serde", + "serde_json", + "wasm-bindgen", + "web-sys", +] + [[package]] name = "goblin" version = "0.6.1" @@ -2318,13 +2376,41 @@ version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "affdc52f7596ccb2d7645231fc6163bb314630c989b64998f3699a28b4d5d4dc" dependencies = [ + "jsonrpsee-client-transport", "jsonrpsee-core", "jsonrpsee-http-client", "jsonrpsee-proc-macros", + "jsonrpsee-server", "jsonrpsee-types", + "jsonrpsee-wasm-client", + "jsonrpsee-ws-client", + "tokio", "tracing", ] +[[package]] +name = "jsonrpsee-client-transport" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b005c793122d03217da09af68ba9383363caa950b90d3436106df8cabce935" +dependencies = [ + "futures-channel", + "futures-util", + "gloo-net", + "http", + "jsonrpsee-core", + "pin-project", + "rustls-native-certs", + "soketto", + "thiserror", + "tokio", + "tokio-rustls", + "tokio-util", + "tracing", + "url", + "webpki-roots 0.25.2", +] + [[package]] name = "jsonrpsee-core" version = "0.20.3" @@ -2332,16 +2418,23 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "da2327ba8df2fdbd5e897e2b5ed25ce7f299d345b9736b6828814c3dbd1fd47b" dependencies = [ "anyhow", + "async-lock", "async-trait", "beef", + "futures-timer", "futures-util", "hyper", "jsonrpsee-types", + "parking_lot", + "rand 0.8.5", + "rustc-hash", "serde", "serde_json", + "soketto", "thiserror", "tokio", "tracing", + "wasm-bindgen-futures", ] [[package]] @@ -2377,6 +2470,29 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "jsonrpsee-server" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82c39a00449c9ef3f50b84fc00fc4acba20ef8f559f07902244abf4c15c5ab9c" +dependencies = [ + "futures-util", + "http", + "hyper", + "jsonrpsee-core", + "jsonrpsee-types", + "route-recognizer", + "serde", + "serde_json", + "soketto", + "thiserror", + "tokio", + "tokio-stream", + "tokio-util", + "tower", + "tracing", +] + [[package]] name = "jsonrpsee-types" version = "0.20.3" @@ -2391,6 +2507,30 @@ dependencies = [ "tracing", ] +[[package]] +name = "jsonrpsee-wasm-client" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c7cbb3447cf14fd4d2f407c3cc96e6c9634d5440aa1fbed868a31f3c02b27f0" +dependencies = [ + "jsonrpsee-client-transport", + "jsonrpsee-core", + "jsonrpsee-types", +] + +[[package]] +name = "jsonrpsee-ws-client" +version = "0.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bca9cb3933ccae417eb6b08c3448eb1cb46e39834e5b503e395e5e5bd08546c0" +dependencies = [ + "http", + "jsonrpsee-client-transport", + "jsonrpsee-core", + "jsonrpsee-types", + "url", +] + [[package]] name = "jsonwebtoken" version = "8.3.0" @@ -3513,12 +3653,24 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "route-recognizer" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afab94fb28594581f62d981211a9a4d53cc8130bbcbbb89a0440d9b8e81a7746" + [[package]] name = "rustc-demangle" version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" +[[package]] +name = "rustc-hash" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" + [[package]] name = "rustc-hex" version = "2.1.0" @@ -3946,6 +4098,19 @@ dependencies = [ "syn 2.0.29", ] +[[package]] +name = "sha-1" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" +dependencies = [ + "block-buffer 0.9.0", + "cfg-if", + "cpufeatures", + "digest 0.9.0", + "opaque-debug 0.3.0", +] + [[package]] name = "sha1" version = "0.10.5" @@ -4071,6 +4236,22 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "soketto" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d1c5305e39e09653383c2c7244f2f78b3bcae37cf50c64cb4789c9f5096ec2" +dependencies = [ + "base64 0.13.1", + "bytes", + "futures", + "http", + "httparse", + "log", + "rand 0.8.5", + "sha-1", +] + [[package]] name = "solang-parser" version = "0.3.2" @@ -4481,6 +4662,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-tungstenite" version = "0.20.0" @@ -4504,6 +4696,7 @@ checksum = "806fe8c2c87eccc8b3267cbae29ed3ab2d0bd37fca70ab622e46aaa9375ddb7d" dependencies = [ "bytes", "futures-core", + "futures-io", "futures-sink", "pin-project-lite", "tokio", diff --git a/bindings/wasm/Cargo.toml b/bindings/wasm/Cargo.toml index 49fa8a6a..56b6a27b 100644 --- a/bindings/wasm/Cargo.toml +++ b/bindings/wasm/Cargo.toml @@ -24,7 +24,7 @@ reqwest = { version = "0.11", default-features = false, features = ["blocking", [target.'cfg(target_arch = "wasm32")'.dependencies] wasm-bindgen-futures = "0.4" -jsonrpsee = { version = "0.20.1", features = ["macros","jsonrpsee-types","client-core"] } +jsonrpsee = { version = "0.20", features = ["macros","jsonrpsee-types","client-core"] } uuid = "0.8" [features] diff --git a/bindings/wasm/src/rpc_client.rs b/bindings/wasm/src/rpc_client.rs index 8424ba67..10bf6966 100644 --- a/bindings/wasm/src/rpc_client.rs +++ b/bindings/wasm/src/rpc_client.rs @@ -23,7 +23,7 @@ macro_rules! rpc_request { ($method:expr,$builder:expr, $server_url:expr, $resp_type: ty) => {{ let params = $builder .to_rpc_params() - .map_err(RpcError::ParseParamsError)?; + .map_err(RpcError::InvalidArgument)?; let request = Request::new( $method.into(), params.as_ref().map(|p| p.as_ref()), diff --git a/provider/Cargo.toml b/provider/Cargo.toml index 992c71b6..7148d6aa 100644 --- a/provider/Cargo.toml +++ b/provider/Cargo.toml @@ -8,17 +8,17 @@ edition = "2021" bigdecimal = { version = "0.3.0", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] } reqwest = { version = "0.11", default-features = false, features = ["blocking", "json", "rustls-tls"] } -serde = "1.0.137" +serde = "1.0" thiserror = "1.0" wasm-bindgen = { version = "0.2.87", features = ["serde-serialize"] } zklink_sdk_signers = { path = "../signers" } zklink_sdk_types = { path = "../types" } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] -jsonrpsee = { version = "0.20.1", features = ["http-client","macros"] } +jsonrpsee = { version = "0.20", features = ["client","macros", "server"] } [target.'cfg(target_arch = "wasm32")'.dependencies] -jsonrpsee = { version = "0.20.1", features = ["macros","jsonrpsee-types","client-core"] } +jsonrpsee = { version = "0.20", features = ["macros","jsonrpsee-types","client-core"] } getrandom = { version = "0.2.11", features = ["js"] } uuid = "0.8" diff --git a/provider/src/error.rs b/provider/src/error.rs index fdc814c0..11976ac2 100644 --- a/provider/src/error.rs +++ b/provider/src/error.rs @@ -1,4 +1,4 @@ -use jsonrpsee::core::error::Error as jsonrpseeError; +use jsonrpsee::core::Error as JError; use thiserror::Error; use wasm_bindgen::JsValue; @@ -8,8 +8,8 @@ pub enum RpcError { InvalidNetwork, #[error("Invalid input parameter")] InvalidInputParameter, - #[error("Parse params error: {0}")] - ParseParamsError(jsonrpseeError), + #[error("Invalid argument: {0}")] + InvalidArgument(JError), #[error("HTTP request error: {0}")] RequestError(reqwest::Error), #[error("Parse response error: {0}")] diff --git a/provider/src/lib.rs b/provider/src/lib.rs index afd32117..c293dec5 100644 --- a/provider/src/lib.rs +++ b/provider/src/lib.rs @@ -1,11 +1,13 @@ pub mod error; pub mod network; pub mod response; -#[cfg(not(any(feature = "ffi", target_arch = "wasm32")))] -pub mod rpc; +#[cfg(not(target_arch = "wasm32"))] +mod rpc; #[cfg(not(any(feature = "ffi", target_arch = "wasm32")))] mod not_ffi { + pub use crate::rpc::{ZkLinkRpcClient, ZkLinkRpcServer}; + use crate::network::Network; use jsonrpsee::http_client::{HttpClient, HttpClientBuilder}; use std::ops::Deref; @@ -25,20 +27,16 @@ mod not_ffi { } impl ZkLinkRpcProvider { - pub fn new(network: Network, timeout: Duration) -> Self { - let zklink_client = HttpClientBuilder::default() - .request_timeout(timeout) - .build(network.url()) - .unwrap(); - - Self(zklink_client) + pub fn new(network: Network, timeout: Option) -> Self { + let mut builder = HttpClientBuilder::default(); + if let Some(timeout) = timeout { + builder = builder.request_timeout(timeout); + } + let client = builder.build(network.url()).unwrap(); + Self(client) } } } -#[cfg(not(any(feature = "ffi", target_arch = "wasm32")))] -pub use crate::rpc::ZkLinkRpcClient; -#[cfg(not(any(feature = "ffi", target_arch = "wasm32")))] -pub use jsonrpsee::core::Error as RpcError; #[cfg(not(any(feature = "ffi", target_arch = "wasm32")))] pub use not_ffi::*; diff --git a/provider/src/rpc.rs b/provider/src/rpc.rs index 2e00a034..1ca5eb4c 100644 --- a/provider/src/rpc.rs +++ b/provider/src/rpc.rs @@ -2,6 +2,7 @@ use jsonrpsee::proc_macros::rpc; use std::collections::HashMap; use super::response::*; +use jsonrpsee::core::RpcResult; use zklink_sdk_signers::zklink_signer::signature::ZkLinkSignature; use zklink_sdk_types::basic_types::tx_hash::TxHash; use zklink_sdk_types::basic_types::{ @@ -11,7 +12,7 @@ use zklink_sdk_types::prelude::BigUintSerdeWrapper; use zklink_sdk_types::signatures::TxLayer1Signature; use zklink_sdk_types::tx_type::zklink_tx::{ZkLinkTx, ZkLinkTxType}; -#[rpc(client)] +#[rpc(client, server)] pub trait ZkLinkRpc { #[method(name = "getSupportChains")] async fn get_support_chains(&self) -> RpcResult>; @@ -45,6 +46,9 @@ pub trait ZkLinkRpc { #[method(name = "getAccount")] async fn account_info(&self, account_query: AccountQuery) -> RpcResult; + #[method(name = "getSubAccountGlobalVars")] + async fn global_vars_info(&self, sub_account_query: SubAccountId) -> RpcResult; + #[method(name = "getAccountBalances")] async fn account_balances( &self, @@ -59,6 +63,13 @@ pub trait ZkLinkRpc { sub_account_id: Option, ) -> RpcResult; + #[method(name = "getAccountPositions")] + async fn account_positions( + &self, + account_id: AccountId, + sub_account_id: Option, + ) -> RpcResult; + #[method(name = "getTokenReserve")] async fn token_remain( &self, @@ -89,33 +100,29 @@ pub trait ZkLinkRpc { #[method(name = "getWithdrawTxs")] async fn tx_withdraw( &self, - last_tx_timestamp: u64, + last_tx_timestamp_micro: u64, max_txs: u32, ) -> RpcResult>; + // TODO: fix in issue #156 + // #[method(name = "getWebSocketEvents")] + // async fn get_websocket_events( + // &self, + // topic: Topic, + // offset: ClientOffset, + // ) -> RpcResult>; + #[method(name = "getChangePubkeyChainId")] async fn get_change_pubkey_chain_id(&self) -> RpcResult; #[method(name = "getEthProperty")] async fn get_eth_property(&self) -> RpcResult; - #[method(name = "pullForwardTxs")] - async fn pull_forward_txs( - &self, - sub_account_id: SubAccountId, - offset_id: i64, - limit: i64, - ) -> RpcResult>; - - #[method(name = "estimateTransactionFee")] - #[deprecated(note = "This rpc will be removed in a future release")] - async fn get_tx_fee(&self, tx: ZkLinkTx) -> RpcResult; - #[method(name = "sendTransaction")] async fn tx_submit( &self, tx: ZkLinkTx, - eth_signature: Option, + l1_signature: Option, submitter_signature: Option, ) -> RpcResult; }