Skip to content

Commit

Permalink
Implement Serde Deserialize for Call and Transaction Requests (#346)
Browse files Browse the repository at this point in the history
* Implement serde::Deserialize for CallRequest

* Implement serde::Deserialize for TransactionRequest

* Use raw JSON string as input for tests
  • Loading branch information
HCastano authored May 15, 2020
1 parent dc5cdc0 commit 27ec387
Showing 1 changed file with 43 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/types/transaction_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::types::{Address, Bytes, U256};
use serde::{Deserialize, Serialize};

/// Call contract request (eth_call / eth_estimateGas)
#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct CallRequest {
/// Sender address (None for arbitrary address)
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -25,7 +25,7 @@ pub struct CallRequest {
}

/// Send Transaction Parameters
#[derive(Clone, Debug, PartialEq, Serialize)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TransactionRequest {
/// Sender address
pub from: Address,
Expand Down Expand Up @@ -97,6 +97,24 @@ mod tests {
);
}

#[test]
fn should_deserialize_call_request() {
let serialized = r#"{
"to": "0x0000000000000000000000000000000000000005",
"gas": "0x5208",
"value": "0x4c4b40",
"data": "0x010203"
}"#;
let deserialized: CallRequest = serde_json::from_str(&serialized).unwrap();

assert_eq!(deserialized.from, None);
assert_eq!(deserialized.to, Address::from_low_u64_be(5));
assert_eq!(deserialized.gas, Some(21_000.into()));
assert_eq!(deserialized.gas_price, None);
assert_eq!(deserialized.value, Some(5_000_000.into()));
assert_eq!(deserialized.data, Some(vec![1, 2, 3].into()));
}

#[test]
fn should_serialize_transaction_request() {
// given
Expand Down Expand Up @@ -128,4 +146,27 @@ mod tests {
}"#
);
}

#[test]
fn should_deserialize_transaction_request() {
let serialized = r#"{
"from": "0x0000000000000000000000000000000000000005",
"gas": "0x5208",
"value": "0x4c4b40",
"data": "0x010203",
"condition": {
"block": 5
}
}"#;
let deserialized: TransactionRequest = serde_json::from_str(&serialized).unwrap();

assert_eq!(deserialized.from, Address::from_low_u64_be(5));
assert_eq!(deserialized.to, None);
assert_eq!(deserialized.gas, Some(21_000.into()));
assert_eq!(deserialized.gas_price, None);
assert_eq!(deserialized.value, Some(5_000_000.into()));
assert_eq!(deserialized.data, Some(vec![1, 2, 3].into()));
assert_eq!(deserialized.nonce, None);
assert_eq!(deserialized.condition, Some(TransactionCondition::Block(5)));
}
}

0 comments on commit 27ec387

Please sign in to comment.