use crate::{bytes::Bytes, hash::{Address, H256}, maybe::MaybeEmpty, uint::Uint};
use serde::Deserialize;
#[derive(Debug, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Transaction {
pub data: Bytes,
pub gas_limit: Uint,
pub gas_price: Uint,
pub nonce: Uint,
pub to: MaybeEmpty<Address>,
pub value: Uint,
#[serde(default)]
pub r: MaybeEmpty<Uint>,
#[serde(default)]
pub s: MaybeEmpty<Uint>,
#[serde(default)]
pub v: MaybeEmpty<Uint>,
#[serde(rename = "secretKey")]
pub secret: Option<H256>,
}
#[cfg(test)]
mod tests {
use super::{Bytes, H256, MaybeEmpty, Transaction, Uint};
use ethereum_types::{H256 as Eth256, U256};
#[test]
fn transaction_deserialization() {
let s = r#"{
"data" : "0x",
"gasLimit" : "0xf388",
"gasPrice" : "0x09184e72a000",
"nonce" : "0x00",
"to" : "",
"value" : "0x00",
"r": "0",
"s": "1",
"v": "2",
"secretKey": "0x0000000000000000000000000000000000000000000000000000000000000000"
}"#;
let tx: Transaction = serde_json::from_str(s).expect("JSON string is valid");
assert_eq!(tx.data, Bytes::new(Vec::new()));
assert_eq!(tx.gas_limit, Uint(U256::from(0xf388)));
assert_eq!(tx.gas_price, Uint(U256::from(0x09184e72a000_u64)));
assert_eq!(tx.nonce, Uint(U256::zero()));
assert_eq!(tx.to, MaybeEmpty::None);
assert_eq!(tx.value, Uint(U256::zero()));
assert_eq!(tx.r, Uint(U256::zero()).into());
assert_eq!(tx.s, Uint(U256::one()).into());
assert_eq!(tx.v, Uint(U256::from(2)).into());
assert_eq!(tx.secret, Some(H256(Eth256::zero())));
}
}