refactor: move FeeHistory to core types (#688)

This commit is contained in:
James Prestwich 2021-12-13 14:49:49 -08:00 committed by GitHub
parent f4c89c6cb1
commit 47e9e7d3c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 36 deletions

View File

@ -0,0 +1,34 @@
use std::str::FromStr;
use crate::types::U256;
use serde::{de::Deserializer, Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FeeHistory {
pub base_fee_per_gas: Vec<U256>,
pub gas_used_ratio: Vec<f64>,
#[serde(deserialize_with = "from_int_or_hex")]
/// oldestBlock is returned as an unsigned integer up to geth v1.10.6. From
/// geth v1.10.7, this has been updated to return in the hex encoded form.
/// The custom deserializer allows backward compatibility for those clients
/// not running v1.10.7 yet.
pub oldest_block: U256,
pub reward: Vec<Vec<U256>>,
}
fn from_int_or_hex<'de, D>(deserializer: D) -> Result<U256, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum IntOrHex {
Int(u64),
Hex(String),
}
match IntOrHex::deserialize(deserializer)? {
IntOrHex::Int(n) => Ok(U256::from(n)),
IntOrHex::Hex(s) => U256::from_str(s.as_str()).map_err(serde::de::Error::custom),
}
}

View File

@ -54,3 +54,6 @@ pub use chain::*;
mod proof; mod proof;
pub use proof::*; pub use proof::*;
mod fee;
pub use fee::*;

View File

@ -27,8 +27,8 @@ pub use pubsub::{PubsubClient, SubscriptionStream};
use async_trait::async_trait; use async_trait::async_trait;
use auto_impl::auto_impl; use auto_impl::auto_impl;
use ethers_core::types::transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed}; use ethers_core::types::transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed};
use serde::{de::DeserializeOwned, Deserialize, Deserializer, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::{error::Error, fmt::Debug, future::Future, pin::Pin, str::FromStr}; use std::{error::Error, fmt::Debug, future::Future, pin::Pin};
pub use provider::{FilterKind, Provider, ProviderError}; pub use provider::{FilterKind, Provider, ProviderError};
@ -636,36 +636,6 @@ pub trait Middleware: Sync + Send + Debug {
} }
} }
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct FeeHistory {
pub base_fee_per_gas: Vec<U256>,
pub gas_used_ratio: Vec<f64>,
#[serde(deserialize_with = "from_int_or_hex")]
/// oldestBlock is returned as an unsigned integer up to geth v1.10.6. From
/// geth v1.10.7, this has been updated to return in the hex encoded form.
/// The custom deserializer allows backward compatibility for those clients
/// not running v1.10.7 yet.
pub oldest_block: U256,
pub reward: Vec<Vec<U256>>,
}
fn from_int_or_hex<'de, D>(deserializer: D) -> Result<U256, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum IntOrHex {
Int(u64),
Hex(String),
}
match IntOrHex::deserialize(deserializer)? {
IntOrHex::Int(n) => Ok(U256::from(n)),
IntOrHex::Hex(s) => U256::from_str(s.as_str()).map_err(serde::de::Error::custom),
}
}
#[cfg(feature = "celo")] #[cfg(feature = "celo")]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)] #[cfg_attr(not(target_arch = "wasm32"), async_trait)]

View File

@ -2,7 +2,7 @@ use crate::{
ens, ens,
pubsub::{PubsubClient, SubscriptionStream}, pubsub::{PubsubClient, SubscriptionStream},
stream::{FilterWatcher, DEFAULT_POLL_INTERVAL}, stream::{FilterWatcher, DEFAULT_POLL_INTERVAL},
FeeHistory, FromErr, Http as HttpProvider, JsonRpcClient, JsonRpcClientWrapper, MockProvider, FromErr, Http as HttpProvider, JsonRpcClient, JsonRpcClientWrapper, MockProvider,
PendingTransaction, QuorumProvider, PendingTransaction, QuorumProvider,
}; };
@ -15,9 +15,10 @@ use ethers_core::{
abi::{self, Detokenize, ParamType}, abi::{self, Detokenize, ParamType},
types::{ types::{
transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed}, transaction::{eip2718::TypedTransaction, eip2930::AccessListWithGasUsed},
Address, Block, BlockId, BlockNumber, BlockTrace, Bytes, EIP1186ProofResponse, Filter, Log, Address, Block, BlockId, BlockNumber, BlockTrace, Bytes, EIP1186ProofResponse, FeeHistory,
NameOrAddress, Selector, Signature, Trace, TraceFilter, TraceType, Transaction, Filter, Log, NameOrAddress, Selector, Signature, Trace, TraceFilter, TraceType,
TransactionReceipt, TxHash, TxpoolContent, TxpoolInspect, TxpoolStatus, H256, U256, U64, Transaction, TransactionReceipt, TxHash, TxpoolContent, TxpoolInspect, TxpoolStatus, H256,
U256, U64,
}, },
utils, utils,
}; };