-
Notifications
You must be signed in to change notification settings - Fork 470
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
tomusdrw
merged 4 commits into
tomusdrw:master
from
HCastano:hc-add-deserialize-to-call-request
May 15, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2b0b6f9
Implement serde::Deserialize for CallRequest
HCastano 3451831
Implement serde::Deserialize for TransactionRequest
HCastano f4e8b92
Use raw JSON string as input for tests
HCastano c55c62f
Merge branch 'master' into hc-add-deserialize-to-call-request
HCastano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")] | ||
|
@@ -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, | ||
|
@@ -97,6 +97,28 @@ mod tests { | |
); | ||
} | ||
|
||
#[test] | ||
fn should_deserialize_call_request() { | ||
let call_request = CallRequest { | ||
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 | ||
|
@@ -128,4 +150,30 @@ mod tests { | |
}"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn should_deserialize_transaction_request() { | ||
let tx_request = TransactionRequest { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.