1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
mod blocknative;
pub use blocknative::BlockNative;
mod eth_gas_station;
pub use eth_gas_station::EthGasStation;
mod etherchain;
pub use etherchain::Etherchain;
mod etherscan;
pub use etherscan::Etherscan;
mod middleware;
pub use middleware::{GasOracleMiddleware, MiddlewareError};
mod median;
pub use median::Median;
mod cache;
pub use cache::Cache;
mod polygon;
pub use polygon::Polygon;
mod gas_now;
pub use gas_now::GasNow;
mod provider_oracle;
pub use provider_oracle::ProviderOracle;
use ethers_core::types::U256;
use async_trait::async_trait;
use auto_impl::auto_impl;
use reqwest::Error as ReqwestError;
use std::error::Error;
use thiserror::Error;
const GWEI_TO_WEI: u64 = 1000000000;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub enum GasCategory {
SafeLow,
Standard,
Fast,
Fastest,
}
#[derive(Error, Debug)]
pub enum GasOracleError {
#[error(transparent)]
HttpClientError(#[from] ReqwestError),
#[error(transparent)]
SerdeJsonError(#[from] serde_json::Error),
#[error("invalid oracle response")]
InvalidResponse,
#[error(transparent)]
EtherscanError(#[from] ethers_etherscan::errors::EtherscanError),
#[error("gas category not supported")]
GasCategoryNotSupported,
#[error("EIP-1559 gas estimation not supported")]
Eip1559EstimationNotSupported,
#[error("None of the oracles returned a value")]
NoValues,
#[error("Chain is not supported by the oracle")]
UnsupportedChain,
#[error("Chain is not supported by the oracle")]
ProviderError(#[from] Box<dyn Error + Send + Sync>),
}
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
#[auto_impl(&, Box, Arc)]
pub trait GasOracle: Send + Sync + std::fmt::Debug {
async fn fetch(&self) -> Result<U256, GasOracleError>;
async fn estimate_eip1559_fees(&self) -> Result<(U256, U256), GasOracleError>;
}