fix(provider): pass array arguments (#10)
ganache would accept malformed requests, but geth is more strict
This commit is contained in:
parent
d90b03da06
commit
57d8efd7cc
|
@ -60,7 +60,7 @@ impl JsonRpcClient for Provider {
|
||||||
async fn request<T: Serialize + Send + Sync, R: for<'a> Deserialize<'a>>(
|
async fn request<T: Serialize + Send + Sync, R: for<'a> Deserialize<'a>>(
|
||||||
&self,
|
&self,
|
||||||
method: &str,
|
method: &str,
|
||||||
params: Option<T>,
|
params: T,
|
||||||
) -> Result<R, ClientError> {
|
) -> Result<R, ClientError> {
|
||||||
let next_id = self.id.load(Ordering::SeqCst) + 1;
|
let next_id = self.id.load(Ordering::SeqCst) + 1;
|
||||||
self.id.store(next_id, Ordering::SeqCst);
|
self.id.store(next_id, Ordering::SeqCst);
|
||||||
|
@ -151,12 +151,12 @@ struct Request<'a, T> {
|
||||||
id: u64,
|
id: u64,
|
||||||
jsonrpc: &'a str,
|
jsonrpc: &'a str,
|
||||||
method: &'a str,
|
method: &'a str,
|
||||||
params: Option<T>,
|
params: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Request<'a, T> {
|
impl<'a, T> Request<'a, T> {
|
||||||
/// Creates a new JSON RPC request
|
/// Creates a new JSON RPC request
|
||||||
fn new(id: u64, method: &'a str, params: Option<T>) -> Self {
|
fn new(id: u64, method: &'a str, params: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id,
|
id,
|
||||||
jsonrpc: "2.0",
|
jsonrpc: "2.0",
|
||||||
|
|
|
@ -26,7 +26,7 @@ pub trait JsonRpcClient: Debug + Clone {
|
||||||
type Error: Error + Into<ProviderError>;
|
type Error: Error + Into<ProviderError>;
|
||||||
|
|
||||||
/// Sends a request with the provided JSON-RPC and parameters serialized as JSON
|
/// Sends a request with the provided JSON-RPC and parameters serialized as JSON
|
||||||
async fn request<T, R>(&self, method: &str, params: Option<T>) -> Result<R, Self::Error>
|
async fn request<T, R>(&self, method: &str, params: T) -> Result<R, Self::Error>
|
||||||
where
|
where
|
||||||
T: Serialize + Send + Sync,
|
T: Serialize + Send + Sync,
|
||||||
R: for<'a> Deserialize<'a>;
|
R: for<'a> Deserialize<'a>;
|
||||||
|
|
|
@ -85,7 +85,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
pub async fn get_block_number(&self) -> Result<U64, ProviderError> {
|
pub async fn get_block_number(&self) -> Result<U64, ProviderError> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_blockNumber", None::<()>)
|
.request("eth_blockNumber", ())
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -120,17 +120,15 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
Ok(match id {
|
Ok(match id {
|
||||||
BlockId::Hash(hash) => {
|
BlockId::Hash(hash) => {
|
||||||
let hash = utils::serialize(&hash);
|
let hash = utils::serialize(&hash);
|
||||||
let args = vec![hash, include_txs];
|
|
||||||
self.0
|
self.0
|
||||||
.request("eth_getBlockByHash", Some(args))
|
.request("eth_getBlockByHash", [hash, include_txs])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?
|
.map_err(Into::into)?
|
||||||
}
|
}
|
||||||
BlockId::Number(num) => {
|
BlockId::Number(num) => {
|
||||||
let num = utils::serialize(&num);
|
let num = utils::serialize(&num);
|
||||||
let args = vec![num, include_txs];
|
|
||||||
self.0
|
self.0
|
||||||
.request("eth_getBlockByNumber", Some(args))
|
.request("eth_getBlockByNumber", [num, include_txs])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?
|
.map_err(Into::into)?
|
||||||
}
|
}
|
||||||
|
@ -145,7 +143,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let hash = transaction_hash.into();
|
let hash = transaction_hash.into();
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_getTransactionByHash", Some(hash))
|
.request("eth_getTransactionByHash", [hash])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -158,7 +156,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let hash = transaction_hash.into();
|
let hash = transaction_hash.into();
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_getTransactionReceipt", Some(hash))
|
.request("eth_getTransactionReceipt", [hash])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -167,7 +165,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
pub async fn get_gas_price(&self) -> Result<U256, ProviderError> {
|
pub async fn get_gas_price(&self) -> Result<U256, ProviderError> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_gasPrice", None::<()>)
|
.request("eth_gasPrice", ())
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -176,7 +174,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
pub async fn get_accounts(&self) -> Result<Vec<Address>, ProviderError> {
|
pub async fn get_accounts(&self) -> Result<Vec<Address>, ProviderError> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_accounts", None::<()>)
|
.request("eth_accounts", ())
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -191,7 +189,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_getTransactionCount", Some(&[from, block]))
|
.request("eth_getTransactionCount", [from, block])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -206,7 +204,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_getBalance", Some(&[from, block]))
|
.request("eth_getBalance", [from, block])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -216,7 +214,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
pub async fn get_chainid(&self) -> Result<U256, ProviderError> {
|
pub async fn get_chainid(&self) -> Result<U256, ProviderError> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_chainId", None::<()>)
|
.request("eth_chainId", ())
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -236,7 +234,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_call", Some(vec![tx, block]))
|
.request("eth_call", [tx, block])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -258,7 +256,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
|
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_estimateGas", Some(args))
|
.request("eth_estimateGas", args)
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -281,7 +279,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
|
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_sendTransaction", Some(vec![tx]))
|
.request("eth_sendTransaction", [tx])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -292,7 +290,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let rlp = utils::serialize(&tx.rlp());
|
let rlp = utils::serialize(&tx.rlp());
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_sendRawTransaction", Some(rlp))
|
.request("eth_sendRawTransaction", [rlp])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -307,7 +305,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let from = utils::serialize(from);
|
let from = utils::serialize(from);
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_sign", Some(vec![from, data]))
|
.request("eth_sign", [from, data])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -318,7 +316,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
pub async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>, ProviderError> {
|
pub async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>, ProviderError> {
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_getLogs", Some(filter))
|
.request("eth_getLogs", [filter])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -360,11 +358,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
FilterKind::Logs(filter) => ("eth_newFilter", utils::serialize(&filter)),
|
FilterKind::Logs(filter) => ("eth_newFilter", utils::serialize(&filter)),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(self
|
Ok(self.0.request(method, [args]).await.map_err(Into::into)?)
|
||||||
.0
|
|
||||||
.request(method, Some(vec![args]))
|
|
||||||
.await
|
|
||||||
.map_err(Into::into)?)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Uninstalls a filter
|
/// Uninstalls a filter
|
||||||
|
@ -372,7 +366,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let id = utils::serialize(&id.into());
|
let id = utils::serialize(&id.into());
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_uninstallFilter", Some(vec![id]))
|
.request("eth_uninstallFilter", [id])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
@ -395,7 +389,7 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
let id = utils::serialize(&id.into());
|
let id = utils::serialize(&id.into());
|
||||||
Ok(self
|
Ok(self
|
||||||
.0
|
.0
|
||||||
.request("eth_getFilterChanges", Some(vec![id]))
|
.request("eth_getFilterChanges", [id])
|
||||||
.await
|
.await
|
||||||
.map_err(Into::into)?)
|
.map_err(Into::into)?)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue