Skip to content

Commit

Permalink
Implement parity namespace (#318)
Browse files Browse the repository at this point in the history
* `Parity` namespace

* Add `parity_call` doc

* Fix a typo

* Remove unused imports.

* Format code.
  • Loading branch information
gulitsky committed Jan 29, 2020
1 parent ee6c360 commit 07b960a
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/api/mod.rs
Expand Up @@ -4,6 +4,7 @@ mod eth;
mod eth_filter;
mod eth_subscribe;
mod net;
mod parity;
mod parity_accounts;
mod parity_set;
mod personal;
Expand All @@ -14,6 +15,7 @@ pub use self::eth::Eth;
pub use self::eth_filter::{BaseFilter, CreateFilter, EthFilter, FilterStream};
pub use self::eth_subscribe::{EthSubscribe, SubscriptionId, SubscriptionResult, SubscriptionStream};
pub use self::net::Net;
pub use self::parity::Parity;
pub use self::parity_accounts::ParityAccounts;
pub use self::parity_set::ParitySet;
pub use self::personal::Personal;
Expand Down Expand Up @@ -76,6 +78,11 @@ impl<T: Transport> Web3<T> {
self.api()
}

/// Access methods from `parity` namespace
pub fn parity(&self) -> parity::Parity<T> {
self.api()
}

/// Access methods from `parity_accounts` namespace
pub fn parity_accounts(&self) -> parity_accounts::ParityAccounts<T> {
self.api()
Expand Down
78 changes: 78 additions & 0 deletions src/api/parity.rs
@@ -0,0 +1,78 @@
use crate::{
api::Namespace,
helpers::{self, CallFuture},
types::{Bytes, CallRequest},
Transport,
};

/// `Parity` namespace
#[derive(Debug, Clone)]
pub struct Parity<T> {
transport: T,
}

impl<T: Transport> Namespace<T> for Parity<T> {
fn new(transport: T) -> Self
where
Self: Sized,
{
Parity { transport }
}

fn transport(&self) -> &T {
&self.transport
}
}

impl<T: Transport> Parity<T> {
/// Sequentially call multiple contract methods in one request without changing the state of the blockchain.
pub fn call(&self, reqs: Vec<CallRequest>) -> CallFuture<Vec<Bytes>, T::Out> {
let reqs = helpers::serialize(&reqs);

CallFuture::new(self.transport.execute("parity_call", vec![reqs]))
}
}

#[cfg(test)]
mod tests {
use super::Parity;
use crate::{
api::Namespace,
rpc::Value,
types::{Address, Bytes, CallRequest},
};
use futures::Future;

rpc_test!(
Parity:call,
vec![
CallRequest {
from: None,
to: Address::from_low_u64_be(0x123),
gas: None,
gas_price: None,
value: Some(0x1.into()),
data: None,
},
CallRequest {
from: Some(Address::from_low_u64_be(0x321)),
to: Address::from_low_u64_be(0x123),
gas: None,
gas_price: None,
value: None,
data: Some(Bytes(vec![0x04, 0x93])),
},
CallRequest {
from: None,
to: Address::from_low_u64_be(0x765),
gas: None,
gas_price: None,
value: Some(0x5.into()),
data: Some(Bytes(vec![0x07, 0x23]))
}
] => "parity_call", vec![
r#"[{"to":"0x0000000000000000000000000000000000000123","value":"0x1"},{"data":"0x0493","from":"0x0000000000000000000000000000000000000321","to":"0x0000000000000000000000000000000000000123"},{"data":"0x0723","to":"0x0000000000000000000000000000000000000765","value":"0x5"}]"#
];
Value::Array(vec![Value::String("0x010203".into()), Value::String("0x7198ab".into()), Value::String("0xde763f".into())]) => vec![Bytes(vec![1, 2, 3]), Bytes(vec![0x71, 0x98, 0xab]), Bytes(vec![0xde, 0x76, 0x3f])]
);
}

0 comments on commit 07b960a

Please sign in to comment.