ic_web3_rs/api/
parity.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use crate::{
    api::Namespace,
    helpers::{self, CallFuture},
    rpc::Value,
    transports::ic_http_client::CallOptions,
    types::{Bytes, CallRequest, ParityPendingTransactionFilter, Transaction},
    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>, options: CallOptions) -> CallFuture<Vec<Bytes>, T::Out> {
        let reqs = helpers::serialize(&reqs);

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

    /// Get pending transactions
    /// Blocked by https://github.com/openethereum/openethereum/issues/159
    pub fn pending_transactions(
        &self,
        limit: Option<usize>,
        filter: Option<ParityPendingTransactionFilter>,
        options: CallOptions,
    ) -> CallFuture<Vec<Transaction>, T::Out> {
        let limit = limit.map(Value::from);
        let filter = filter.as_ref().map(helpers::serialize);
        let params = match (limit, filter) {
            (l, Some(f)) => vec![l.unwrap_or(Value::Null), f],
            (Some(l), None) => vec![l],
            _ => vec![],
        };

        CallFuture::new(self.transport.execute("parity_pendingTransactions", params, options))
    }
}

#[cfg(test)]
mod tests {
    use super::Parity;
    use crate::{
        api::Namespace,
        rpc::Value,
        transports::ic_http_client::CallOptions,
        types::{Address, CallRequest, FilterCondition, ParityPendingTransactionFilter, Transaction, U64},
    };
    use hex_literal::hex;

    const EXAMPLE_PENDING_TX: &str = r#"{
    "blockHash": null,
    "blockNumber": null,
    "creates": null,
    "from": "0xee3ea02840129123d5397f91be0391283a25bc7d",
    "gas": "0x23b58",
    "gasPrice": "0xba43b7400",
    "hash": "0x160b3c30ab1cf5871083f97ee1cee3901cfba3b0a2258eb337dd20a7e816b36e",
    "input": "0x095ea7b3000000000000000000000000bf4ed7b27f1d666546e30d74d50d173d20bca75400000000000000000000000000002643c948210b4bd99244ccd64d5555555555",
    "condition": {
    "block": 1
    },
    "chainId": 1,
    "nonce": "0x5",
    "publicKey": "0x96157302dade55a1178581333e57d60ffe6fdf5a99607890456a578b4e6b60e335037d61ed58aa4180f9fd747dc50d44a7924aa026acbfb988b5062b629d6c36",
    "r": "0x92e8beb19af2bad0511d516a86e77fa73004c0811b2173657a55797bdf8558e1",
    "raw": "0xf8aa05850ba43b740083023b5894bb9bc244d798123fde783fcc1c72d3bb8c18941380b844095ea7b3000000000000000000000000bf4ed7b27f1d666546e30d74d50d173d20bca75400000000000000000000000000002643c948210b4bd99244ccd64d555555555526a092e8beb19af2bad0511d516a86e77fa73004c0811b2173657a55797bdf8558e1a062b4d4d125bbcb9c162453bc36ca156537543bb4414d59d1805d37fb63b351b8",
    "s": "0x62b4d4d125bbcb9c162453bc36ca156537543bb4414d59d1805d37fb63b351b8",
    "standardV": "0x1",
    "to": "0xbb9bc244d798123fde783fcc1c72d3bb8c189413",
    "transactionIndex": null,
    "v": "0x26",
    "value": "0x0"
}"#;

    rpc_test!(
        Parity:call,
        vec![
            CallRequest {
                from: None,
                to: Some(Address::from_low_u64_be(0x123)),
                gas: None,
                gas_price: None,
                value: Some(0x1.into()),
                data: None,
                transaction_type: None,
                access_list: None,
                max_fee_per_gas: None,
                max_priority_fee_per_gas: None,
            },
            CallRequest {
                from: Some(Address::from_low_u64_be(0x321)),
                to: Some(Address::from_low_u64_be(0x123)),
                gas: None,
                gas_price: None,
                value: None,
                data: Some(hex!("0493").into()),
                transaction_type: None,
                access_list: None,
                max_fee_per_gas: None,
                max_priority_fee_per_gas: None,
            },
            CallRequest {
                from: None,
                to: Some(Address::from_low_u64_be(0x765)),
                gas: None,
                gas_price: None,
                value: Some(0x5.into()),
                data: Some(hex!("0723").into()),
                transaction_type: None,
                access_list: None,
                max_fee_per_gas: None,
                max_priority_fee_per_gas: None,
            }
        ],CallOptions::default() => "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![hex!("010203").into(), hex!("7198ab").into(), hex!("de763f").into()]
    );

    rpc_test!(
        Parity:pending_transactions,
        1,
        ParityPendingTransactionFilter::builder()
            .from(Address::from_low_u64_be(0x32))
            .gas(U64::from(100_000))
            .gas_price(FilterCondition::GreaterThan(U64::from(100_000_000_000_u64)))
            .build(),CallOptions::default()
         => "parity_pendingTransactions",
            vec![r#"1"#, r#"{"from":{"eq":"0x0000000000000000000000000000000000000032"},"gas":{"eq":"0x186a0"},"gas_price":{"gt":"0x174876e800"}}"#]
        ;
        Value::Array(vec![::serde_json::from_str(EXAMPLE_PENDING_TX).unwrap()])
      => vec![::serde_json::from_str::<Transaction>(EXAMPLE_PENDING_TX).unwrap()]
    );
}