ic_web3_rs/api/
parity.rs

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