mwc_web3/api/
parity.rs

1use crate::{
2    api::Namespace,
3    helpers::{self, CallFuture},
4    rpc::Value,
5    types::{Bytes, CallRequest, ParityPendingTransactionFilter, Transaction},
6    Transport,
7};
8
9/// `Parity` namespace
10#[derive(Debug, Clone)]
11pub struct Parity<T> {
12    transport: T,
13}
14
15impl<T: Transport> Namespace<T> for Parity<T> {
16    fn new(transport: T) -> Self
17    where
18        Self: Sized,
19    {
20        Parity { transport }
21    }
22
23    fn transport(&self) -> &T {
24        &self.transport
25    }
26}
27
28impl<T: Transport> Parity<T> {
29    /// Sequentially call multiple contract methods in one request without changing the state of the blockchain.
30    pub fn call(&self, reqs: Vec<CallRequest>) -> CallFuture<Vec<Bytes>, T::Out> {
31        let reqs = helpers::serialize(&reqs);
32
33        CallFuture::new(self.transport.execute("parity_call", vec![reqs]))
34    }
35
36    /// Get pending transactions
37    /// Blocked by https://github.com/openethereum/openethereum/issues/159
38    pub fn pending_transactions(
39        &self,
40        limit: Option<usize>,
41        filter: Option<ParityPendingTransactionFilter>,
42    ) -> CallFuture<Vec<Transaction>, T::Out> {
43        let limit = limit.map(Value::from);
44        let filter = filter.as_ref().map(helpers::serialize);
45        let params = match (limit, filter) {
46            (l, Some(f)) => vec![l.unwrap_or(Value::Null), f],
47            (Some(l), None) => vec![l],
48            _ => vec![],
49        };
50
51        CallFuture::new(self.transport.execute("parity_pendingTransactions", params))
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::Parity;
58    use crate::{
59        api::Namespace,
60        rpc::Value,
61        types::{Address, CallRequest, FilterCondition, ParityPendingTransactionFilter, Transaction, U64},
62    };
63    use hex_literal::hex;
64
65    const EXAMPLE_PENDING_TX: &str = r#"{
66    "blockHash": null,
67    "blockNumber": null,
68    "creates": null,
69    "from": "0xee3ea02840129123d5397f91be0391283a25bc7d",
70    "gas": "0x23b58",
71    "gasPrice": "0xba43b7400",
72    "hash": "0x160b3c30ab1cf5871083f97ee1cee3901cfba3b0a2258eb337dd20a7e816b36e",
73    "input": "0x095ea7b3000000000000000000000000bf4ed7b27f1d666546e30d74d50d173d20bca75400000000000000000000000000002643c948210b4bd99244ccd64d5555555555",
74    "condition": {
75    "block": 1
76    },
77    "chainId": 1,
78    "nonce": "0x5",
79    "publicKey": "0x96157302dade55a1178581333e57d60ffe6fdf5a99607890456a578b4e6b60e335037d61ed58aa4180f9fd747dc50d44a7924aa026acbfb988b5062b629d6c36",
80    "r": "0x92e8beb19af2bad0511d516a86e77fa73004c0811b2173657a55797bdf8558e1",
81    "raw": "0xf8aa05850ba43b740083023b5894bb9bc244d798123fde783fcc1c72d3bb8c18941380b844095ea7b3000000000000000000000000bf4ed7b27f1d666546e30d74d50d173d20bca75400000000000000000000000000002643c948210b4bd99244ccd64d555555555526a092e8beb19af2bad0511d516a86e77fa73004c0811b2173657a55797bdf8558e1a062b4d4d125bbcb9c162453bc36ca156537543bb4414d59d1805d37fb63b351b8",
82    "s": "0x62b4d4d125bbcb9c162453bc36ca156537543bb4414d59d1805d37fb63b351b8",
83    "standardV": "0x1",
84    "to": "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
85    "transactionIndex": null,
86    "v": "0x26",
87    "value": "0x0"
88}"#;
89
90    rpc_test!(
91        Parity:call,
92        vec![
93            CallRequest {
94                from: None,
95                to: Some(Address::from_low_u64_be(0x123)),
96                gas: None,
97                gas_price: None,
98                value: Some(0x1.into()),
99                data: None,
100            },
101            CallRequest {
102                from: Some(Address::from_low_u64_be(0x321)),
103                to: Some(Address::from_low_u64_be(0x123)),
104                gas: None,
105                gas_price: None,
106                value: None,
107                data: Some(hex!("0493").into()),
108            },
109            CallRequest {
110                from: None,
111                to: Some(Address::from_low_u64_be(0x765)),
112                gas: None,
113                gas_price: None,
114                value: Some(0x5.into()),
115                data: Some(hex!("0723").into())
116            }
117        ] => "parity_call", vec![
118            r#"[{"to":"0x0000000000000000000000000000000000000123","value":"0x1"},{"data":"0x0493","from":"0x0000000000000000000000000000000000000321","to":"0x0000000000000000000000000000000000000123"},{"data":"0x0723","to":"0x0000000000000000000000000000000000000765","value":"0x5"}]"#
119        ];
120        Value::Array(vec![Value::String("0x010203".into()), Value::String("0x7198ab".into()), Value::String("0xde763f".into())]) => vec![hex!("010203").into(), hex!("7198ab").into(), hex!("de763f").into()]
121    );
122
123    rpc_test!(
124        Parity:pending_transactions,
125        1,
126        ParityPendingTransactionFilter::builder()
127            .from(Address::from_low_u64_be(0x32))
128            .gas(U64::from(100_000))
129            .gas_price(FilterCondition::GreaterThan(U64::from(100_000_000_000 as u64)))
130            .build()
131         => "parity_pendingTransactions",
132            vec![r#"1"#, r#"{"from":{"eq":"0x0000000000000000000000000000000000000032"},"gas":{"eq":"0x186a0"},"gas_price":{"gt":"0x174876e800"}}"#]
133        ;
134        Value::Array(vec![::serde_json::from_str(EXAMPLE_PENDING_TX).unwrap()])
135      => vec![::serde_json::from_str::<Transaction>(EXAMPLE_PENDING_TX).unwrap()]
136    );
137}