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
//! # Serialize JSON RPC parameters

use super::{Error, ToHex, align_bytes, to_arr, to_even_str, to_u64, trim_hex};
use super::core::{Address, Transaction};
use hex::FromHex;
use jsonrpc_core::{Params, Value as JsonRpcValue};

#[derive(Deserialize, Debug)]
pub struct RPCTransaction {
    pub from: String,
    pub to: String,
    pub gas: String,
    #[serde(rename = "gasPrice")]
    pub gas_price: String,
    #[serde(default)]
    pub value: String,
    #[serde(default)]
    pub data: String,
    pub nonce: String,
}

impl RPCTransaction {
    pub fn try_into(self) -> Result<Transaction, Error> {
        let gp_str = trim_hex(self.gas_price.as_str());
        let v_str = trim_hex(self.value.as_str());

        let gas_limit = Vec::from_hex(trim_hex(self.gas.as_str()))?;
        let gas_price = Vec::from_hex(gp_str)?;
        let value = Vec::from_hex(v_str)?;
        let nonce = Vec::from_hex(to_even_str(trim_hex(self.nonce.as_str())))?;

        Ok(Transaction {
            nonce: to_u64(&nonce),
            gas_price: to_arr(&align_bytes(&gas_price, 32)),
            gas_limit: to_u64(&gas_limit),
            to: self.to.as_str().parse::<Address>().ok(),
            value: to_arr(&align_bytes(&value, 32)),
            data: Vec::from_hex(trim_hex(self.data.as_str()))?,
        })
    }
}

impl Transaction {
    //    /// Sign transaction and return as raw data
    //    pub fn to_raw_params(&self, pk: PrivateKey, chain: u8) -> Params {
    //        self.to_signed_raw(pk, chain)
    //            .map(|v| format!("0x{}", v.to_hex()))
    //            .map(|s| Params::Array(vec![JsonRpcValue::String(s)]))
    //            .expect("Expect to sign a transaction")
    //    }
    /// Signed transaction into raw data
    pub fn to_raw_params(signed_rlp: Vec<u8>) -> Params {
        let str = format!("0x{}", signed_rlp.to_hex());
        Params::Array(vec![JsonRpcValue::String(str)])
    }
}