Skip to content
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

Implement parity namespace #318

Merged
merged 5 commits into from
Jan 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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])]
);
}