Fix 'get_transactions' response. (#1632)

* Fix 'get_transactions' response.

* Add some explanation to GenesisOption.

* One-line-ifying

* Update changelog
This commit is contained in:
Jacob 2022-08-23 17:17:00 +01:00 committed by GitHub
parent b302ac05be
commit ff66008cbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 20 deletions

View File

@ -82,6 +82,7 @@
- Fix handling of Websocket connection errors [#1287](https://github.com/gakonst/ethers-rs/pull/1287)
- Add Arithmetic Shift Right operation for I256 [#1323](https://github.com/gakonst/ethers-rs/issues/1323)
- [#1535](https://github.com/gakonst/ethers-rs/pull/1535) Add support to Aurora and Aurora testnet networks.
- [#1632](https://github.com/gakonst/ethers-rs/pull/1632) Re-export `H32` from `ethabi`.
## ethers-contract-abigen

View File

@ -5,7 +5,7 @@ pub type Selector = [u8; 4];
/// A transaction Hash
pub use ethabi::ethereum_types::H256 as TxHash;
pub use ethabi::ethereum_types::{Address, Bloom, H160, H256, H512, H64, U128, U256, U64};
pub use ethabi::ethereum_types::{Address, Bloom, H160, H256, H32, H512, H64, U128, U256, U64};
pub mod transaction;
pub use transaction::{

View File

@ -1,7 +1,7 @@
use crate::{Client, EtherscanError, Query, Response, Result};
use ethers_core::{
abi::Address,
types::{serde_helpers::*, BlockNumber, Bytes, H256, U256},
types::{serde_helpers::*, BlockNumber, Bytes, H256, H32, U256},
};
use serde::{Deserialize, Serialize};
use std::{
@ -17,7 +17,7 @@ pub struct AccountBalance {
pub balance: String,
}
mod jsonstring {
mod genesis_string {
use super::*;
use serde::{
de::{DeserializeOwned, Error as _},
@ -52,9 +52,9 @@ mod jsonstring {
{
let json = Cow::<'de, str>::deserialize(deserializer)?;
if !json.is_empty() && !json.starts_with("GENESIS") {
let value =
serde_json::from_str(&format!("\"{}\"", &json)).map_err(D::Error::custom)?;
Ok(GenesisOption::Some(value))
serde_json::from_str(&format!("\"{}\"", &json))
.map(GenesisOption::Some)
.map_err(D::Error::custom)
} else if json.starts_with("GENESIS") {
Ok(GenesisOption::Genesis)
} else {
@ -63,7 +63,82 @@ mod jsonstring {
}
}
/// Possible values for some field responses
mod json_string {
use super::*;
use serde::{
de::{DeserializeOwned, Error as _},
ser::Error as _,
Deserializer, Serializer,
};
pub fn serialize<T, S>(value: &Option<T>, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
let json = match value {
Option::None => Cow::from(""),
Option::Some(value) => serde_json::to_string(value).map_err(S::Error::custom)?.into(),
};
serializer.serialize_str(&json)
}
pub fn deserialize<'de, T, D>(deserializer: D) -> std::result::Result<Option<T>, D::Error>
where
T: DeserializeOwned,
D: Deserializer<'de>,
{
let json = Cow::<'de, str>::deserialize(deserializer)?;
if json.is_empty() {
Ok(Option::None)
} else {
serde_json::from_str(&format!("\"{}\"", &json))
.map(Option::Some)
.map_err(D::Error::custom)
}
}
}
mod hex_string {
use super::*;
use serde::{
de::{DeserializeOwned, Error as _},
ser::Error as _,
Deserializer, Serializer,
};
pub fn serialize<T, S>(value: &Option<T>, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer,
{
let json = match value {
Option::None => Cow::from("0x"),
Option::Some(value) => serde_json::to_string(value).map_err(S::Error::custom)?.into(),
};
serializer.serialize_str(&json)
}
pub fn deserialize<'de, T, D>(deserializer: D) -> std::result::Result<Option<T>, D::Error>
where
T: DeserializeOwned,
D: Deserializer<'de>,
{
let json = Cow::<'de, str>::deserialize(deserializer)?;
if json.is_empty() || json == "0x" {
Ok(Option::None)
} else {
serde_json::from_str(&format!("\"{}\"", &json))
.map(Option::Some)
.map_err(D::Error::custom)
}
}
}
/// Possible values for some field responses.
///
/// Transactions from the Genesis block may contain fields that do not conform to the expected
/// types.
#[derive(Debug)]
pub enum GenesisOption<T> {
None,
@ -101,15 +176,15 @@ pub struct NormalTransaction {
#[serde(deserialize_with = "deserialize_stringified_block_number")]
pub block_number: BlockNumber,
pub time_stamp: String,
#[serde(with = "jsonstring")]
#[serde(with = "genesis_string")]
pub hash: GenesisOption<H256>,
#[serde(with = "jsonstring")]
pub nonce: GenesisOption<U256>,
#[serde(with = "jsonstring")]
pub block_hash: GenesisOption<U256>,
#[serde(with = "json_string")]
pub nonce: Option<U256>,
#[serde(with = "json_string")]
pub block_hash: Option<U256>,
#[serde(deserialize_with = "deserialize_stringified_u64_opt")]
pub transaction_index: Option<u64>,
#[serde(with = "jsonstring")]
#[serde(with = "genesis_string")]
pub from: GenesisOption<Address>,
pub to: Option<Address>,
#[serde(deserialize_with = "deserialize_stringified_numeric")]
@ -120,16 +195,19 @@ pub struct NormalTransaction {
pub gas_price: Option<U256>,
#[serde(rename = "txreceipt_status")]
pub tx_receipt_status: String,
#[serde(with = "jsonstring")]
pub input: GenesisOption<Bytes>,
#[serde(with = "jsonstring")]
pub contract_address: GenesisOption<Address>,
pub input: Bytes,
#[serde(with = "json_string")]
pub contract_address: Option<Address>,
#[serde(deserialize_with = "deserialize_stringified_numeric")]
pub gas_used: U256,
#[serde(deserialize_with = "deserialize_stringified_numeric")]
pub cumulative_gas_used: U256,
#[serde(deserialize_with = "deserialize_stringified_u64")]
pub confirmations: u64,
#[serde(with = "hex_string")]
pub method_id: Option<H32>,
#[serde(with = "json_string")]
pub function_name: Option<String>,
}
/// The raw response from the internal transaction list API endpoint
@ -141,13 +219,13 @@ pub struct InternalTransaction {
pub time_stamp: String,
pub hash: H256,
pub from: Address,
#[serde(with = "jsonstring")]
#[serde(with = "genesis_string")]
pub to: GenesisOption<Address>,
#[serde(deserialize_with = "deserialize_stringified_numeric")]
pub value: U256,
#[serde(with = "jsonstring")]
#[serde(with = "genesis_string")]
pub contract_address: GenesisOption<Address>,
#[serde(with = "jsonstring")]
#[serde(with = "genesis_string")]
pub input: GenesisOption<Bytes>,
#[serde(rename = "type")]
pub result_type: String,