pink_web3/api/
parity.rs

1use crate::prelude::*;
2use crate::{
3    api::Namespace,
4    helpers::{self, CallFuture},
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 = helpers::serialize(&limit);
44        let filter = filter.as_ref().map(helpers::serialize);
45        let params = match (limit, filter) {
46            (l, Some(f)) => vec![l, f],
47            (l, None) => vec![l],
48        };
49
50        CallFuture::new(self.transport.execute("parity_pendingTransactions", params))
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::Parity;
57    use crate::{
58        api::Namespace,
59        rpc::Value,
60        types::{Address, CallRequest, FilterCondition, ParityPendingTransactionFilter, Transaction, U64},
61    };
62    use hex_literal::hex;
63
64    const EXAMPLE_PENDING_TX: &str = r#"{
65    "blockHash": null,
66    "blockNumber": null,
67    "creates": null,
68    "from": "0xee3ea02840129123d5397f91be0391283a25bc7d",
69    "gas": "0x23b58",
70    "gasPrice": "0xba43b7400",
71    "hash": "0x160b3c30ab1cf5871083f97ee1cee3901cfba3b0a2258eb337dd20a7e816b36e",
72    "input": "0x095ea7b3000000000000000000000000bf4ed7b27f1d666546e30d74d50d173d20bca75400000000000000000000000000002643c948210b4bd99244ccd64d5555555555",
73    "condition": {
74    "block": 1
75    },
76    "chainId": 1,
77    "nonce": "0x5",
78    "publicKey": "0x96157302dade55a1178581333e57d60ffe6fdf5a99607890456a578b4e6b60e335037d61ed58aa4180f9fd747dc50d44a7924aa026acbfb988b5062b629d6c36",
79    "r": "0x92e8beb19af2bad0511d516a86e77fa73004c0811b2173657a55797bdf8558e1",
80    "raw": "0xf8aa05850ba43b740083023b5894bb9bc244d798123fde783fcc1c72d3bb8c18941380b844095ea7b3000000000000000000000000bf4ed7b27f1d666546e30d74d50d173d20bca75400000000000000000000000000002643c948210b4bd99244ccd64d555555555526a092e8beb19af2bad0511d516a86e77fa73004c0811b2173657a55797bdf8558e1a062b4d4d125bbcb9c162453bc36ca156537543bb4414d59d1805d37fb63b351b8",
81    "s": "0x62b4d4d125bbcb9c162453bc36ca156537543bb4414d59d1805d37fb63b351b8",
82    "standardV": "0x1",
83    "to": "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
84    "transactionIndex": null,
85    "v": "0x26",
86    "value": "0x0"
87}"#;
88
89    rpc_test!(
90        Parity:call,
91        vec![
92            CallRequest {
93                from: None,
94                to: Some(Address::from_low_u64_be(0x123)),
95                gas: None,
96                gas_price: None,
97                value: Some(0x1.into()),
98                data: None,
99                transaction_type: None,
100                access_list: None,
101                max_fee_per_gas: None,
102                max_priority_fee_per_gas: None,
103            },
104            CallRequest {
105                from: Some(Address::from_low_u64_be(0x321)),
106                to: Some(Address::from_low_u64_be(0x123)),
107                gas: None,
108                gas_price: None,
109                value: None,
110                data: Some(hex!("0493").into()),
111                transaction_type: None,
112                access_list: None,
113                max_fee_per_gas: None,
114                max_priority_fee_per_gas: None,
115            },
116            CallRequest {
117                from: None,
118                to: Some(Address::from_low_u64_be(0x765)),
119                gas: None,
120                gas_price: None,
121                value: Some(0x5.into()),
122                data: Some(hex!("0723").into()),
123                transaction_type: None,
124                access_list: None,
125                max_fee_per_gas: None,
126                max_priority_fee_per_gas: None,
127            }
128        ] => "parity_call", vec![
129            r#"[{"to":"0x0000000000000000000000000000000000000123","value":"0x1"},{"from":"0x0000000000000000000000000000000000000321","to":"0x0000000000000000000000000000000000000123","data":"0x0493"},{"to":"0x0000000000000000000000000000000000000765","value":"0x5","data":"0x0723"}]"#
130        ];
131        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()]
132    );
133
134    rpc_test!(
135        Parity:pending_transactions,
136        1,
137        ParityPendingTransactionFilter::builder()
138            .from(Address::from_low_u64_be(0x32))
139            .gas(U64::from(100_000))
140            .gas_price(FilterCondition::GreaterThan(U64::from(100_000_000_000_u64)))
141            .build()
142         => "parity_pendingTransactions",
143            vec![r#"1"#, r#"{"from":{"eq":"0x0000000000000000000000000000000000000032"},"gas":{"eq":"0x186a0"},"gas_price":{"gt":"0x174876e800"}}"#]
144        ;
145        Value::Array(vec![::serde_json::from_str(EXAMPLE_PENDING_TX).unwrap()])
146      => vec![::serde_json::from_str::<Transaction>(EXAMPLE_PENDING_TX).unwrap()]
147    );
148}