From 57d8efd7ccab925706aae4e69b16f6001ccac6ca Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 15 Jun 2020 13:53:40 +0300 Subject: [PATCH] fix(provider): pass array arguments (#10) ganache would accept malformed requests, but geth is more strict --- ethers-providers/src/http.rs | 6 ++--- ethers-providers/src/lib.rs | 2 +- ethers-providers/src/provider.rs | 44 ++++++++++++++------------------ 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/ethers-providers/src/http.rs b/ethers-providers/src/http.rs index 8c35aa7a..2f17ad03 100644 --- a/ethers-providers/src/http.rs +++ b/ethers-providers/src/http.rs @@ -60,7 +60,7 @@ impl JsonRpcClient for Provider { async fn request Deserialize<'a>>( &self, method: &str, - params: Option, + params: T, ) -> Result { let next_id = self.id.load(Ordering::SeqCst) + 1; self.id.store(next_id, Ordering::SeqCst); @@ -151,12 +151,12 @@ struct Request<'a, T> { id: u64, jsonrpc: &'a str, method: &'a str, - params: Option, + params: T, } impl<'a, T> Request<'a, T> { /// Creates a new JSON RPC request - fn new(id: u64, method: &'a str, params: Option) -> Self { + fn new(id: u64, method: &'a str, params: T) -> Self { Self { id, jsonrpc: "2.0", diff --git a/ethers-providers/src/lib.rs b/ethers-providers/src/lib.rs index 82bfc083..ace6151d 100644 --- a/ethers-providers/src/lib.rs +++ b/ethers-providers/src/lib.rs @@ -26,7 +26,7 @@ pub trait JsonRpcClient: Debug + Clone { type Error: Error + Into; /// Sends a request with the provided JSON-RPC and parameters serialized as JSON - async fn request(&self, method: &str, params: Option) -> Result + async fn request(&self, method: &str, params: T) -> Result where T: Serialize + Send + Sync, R: for<'a> Deserialize<'a>; diff --git a/ethers-providers/src/provider.rs b/ethers-providers/src/provider.rs index c90254d6..e34ec711 100644 --- a/ethers-providers/src/provider.rs +++ b/ethers-providers/src/provider.rs @@ -85,7 +85,7 @@ impl Provider

{ pub async fn get_block_number(&self) -> Result { Ok(self .0 - .request("eth_blockNumber", None::<()>) + .request("eth_blockNumber", ()) .await .map_err(Into::into)?) } @@ -120,17 +120,15 @@ impl Provider

{ Ok(match id { BlockId::Hash(hash) => { let hash = utils::serialize(&hash); - let args = vec![hash, include_txs]; self.0 - .request("eth_getBlockByHash", Some(args)) + .request("eth_getBlockByHash", [hash, include_txs]) .await .map_err(Into::into)? } BlockId::Number(num) => { let num = utils::serialize(&num); - let args = vec![num, include_txs]; self.0 - .request("eth_getBlockByNumber", Some(args)) + .request("eth_getBlockByNumber", [num, include_txs]) .await .map_err(Into::into)? } @@ -145,7 +143,7 @@ impl Provider

{ let hash = transaction_hash.into(); Ok(self .0 - .request("eth_getTransactionByHash", Some(hash)) + .request("eth_getTransactionByHash", [hash]) .await .map_err(Into::into)?) } @@ -158,7 +156,7 @@ impl Provider

{ let hash = transaction_hash.into(); Ok(self .0 - .request("eth_getTransactionReceipt", Some(hash)) + .request("eth_getTransactionReceipt", [hash]) .await .map_err(Into::into)?) } @@ -167,7 +165,7 @@ impl Provider

{ pub async fn get_gas_price(&self) -> Result { Ok(self .0 - .request("eth_gasPrice", None::<()>) + .request("eth_gasPrice", ()) .await .map_err(Into::into)?) } @@ -176,7 +174,7 @@ impl Provider

{ pub async fn get_accounts(&self) -> Result, ProviderError> { Ok(self .0 - .request("eth_accounts", None::<()>) + .request("eth_accounts", ()) .await .map_err(Into::into)?) } @@ -191,7 +189,7 @@ impl Provider

{ let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest)); Ok(self .0 - .request("eth_getTransactionCount", Some(&[from, block])) + .request("eth_getTransactionCount", [from, block]) .await .map_err(Into::into)?) } @@ -206,7 +204,7 @@ impl Provider

{ let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest)); Ok(self .0 - .request("eth_getBalance", Some(&[from, block])) + .request("eth_getBalance", [from, block]) .await .map_err(Into::into)?) } @@ -216,7 +214,7 @@ impl Provider

{ pub async fn get_chainid(&self) -> Result { Ok(self .0 - .request("eth_chainId", None::<()>) + .request("eth_chainId", ()) .await .map_err(Into::into)?) } @@ -236,7 +234,7 @@ impl Provider

{ let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest)); Ok(self .0 - .request("eth_call", Some(vec![tx, block])) + .request("eth_call", [tx, block]) .await .map_err(Into::into)?) } @@ -258,7 +256,7 @@ impl Provider

{ Ok(self .0 - .request("eth_estimateGas", Some(args)) + .request("eth_estimateGas", args) .await .map_err(Into::into)?) } @@ -281,7 +279,7 @@ impl Provider

{ Ok(self .0 - .request("eth_sendTransaction", Some(vec![tx])) + .request("eth_sendTransaction", [tx]) .await .map_err(Into::into)?) } @@ -292,7 +290,7 @@ impl Provider

{ let rlp = utils::serialize(&tx.rlp()); Ok(self .0 - .request("eth_sendRawTransaction", Some(rlp)) + .request("eth_sendRawTransaction", [rlp]) .await .map_err(Into::into)?) } @@ -307,7 +305,7 @@ impl Provider

{ let from = utils::serialize(from); Ok(self .0 - .request("eth_sign", Some(vec![from, data])) + .request("eth_sign", [from, data]) .await .map_err(Into::into)?) } @@ -318,7 +316,7 @@ impl Provider

{ pub async fn get_logs(&self, filter: &Filter) -> Result, ProviderError> { Ok(self .0 - .request("eth_getLogs", Some(filter)) + .request("eth_getLogs", [filter]) .await .map_err(Into::into)?) } @@ -360,11 +358,7 @@ impl Provider

{ FilterKind::Logs(filter) => ("eth_newFilter", utils::serialize(&filter)), }; - Ok(self - .0 - .request(method, Some(vec![args])) - .await - .map_err(Into::into)?) + Ok(self.0.request(method, [args]).await.map_err(Into::into)?) } /// Uninstalls a filter @@ -372,7 +366,7 @@ impl Provider

{ let id = utils::serialize(&id.into()); Ok(self .0 - .request("eth_uninstallFilter", Some(vec![id])) + .request("eth_uninstallFilter", [id]) .await .map_err(Into::into)?) } @@ -395,7 +389,7 @@ impl Provider

{ let id = utils::serialize(&id.into()); Ok(self .0 - .request("eth_getFilterChanges", Some(vec![id])) + .request("eth_getFilterChanges", [id]) .await .map_err(Into::into)?) }