Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Serde Deserialize for Call and Transaction Requests #346

Merged
merged 4 commits into from
May 15, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 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,28 @@ mod tests {
);
}

#[test]
fn should_deserialize_call_request() {
let call_request = CallRequest {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here - let's just hardcode a string (or two) and check we deserialize it correctly.

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

let serialized = serde_json::to_string(&call_request).unwrap();
let deserialized: CallRequest = serde_json::from_str(&serialized).unwrap();

assert_eq!(deserialized.from, call_request.from);
assert_eq!(deserialized.to, call_request.to);
assert_eq!(deserialized.gas, call_request.gas);
assert_eq!(deserialized.gas_price, call_request.gas_price);
assert_eq!(deserialized.value, call_request.value);
assert_eq!(deserialized.data, call_request.data);
}

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

#[test]
fn should_deserialize_transaction_request() {
let tx_request = TransactionRequest {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a test for serialization, I think for simplicity of the test I'd prefer to just use a raw JSON as serialized.

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

let serialized = serde_json::to_string(&tx_request).unwrap();
let deserialized: TransactionRequest = serde_json::from_str(&serialized).unwrap();

assert_eq!(deserialized.from, tx_request.from);
assert_eq!(deserialized.to, tx_request.to);
assert_eq!(deserialized.gas, tx_request.gas);
assert_eq!(deserialized.gas_price, tx_request.gas_price);
assert_eq!(deserialized.value, tx_request.value);
assert_eq!(deserialized.data, tx_request.data);
assert_eq!(deserialized.nonce, tx_request.nonce);
assert_eq!(deserialized.condition, tx_request.condition);
}
}