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
use crate::errors::ExecutionError;
use crate::future::CompatCallFuture;
use crate::transaction::TransactionBuilder;
use futures::compat::Future01CompatExt;
use pin_project::pin_project;
use serde::Serialize;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use web3::api::{Eth, Namespace};
use web3::helpers::{self, CallFuture};
use web3::types::{Address, Bytes, U256};
use web3::Transport;
#[derive(Clone, Debug, Serialize)]
pub struct EstimateGasRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub from: Option<Address>,
#[serde(skip_serializing_if = "Option::is_none")]
pub to: Option<Address>,
#[serde(skip_serializing_if = "Option::is_none")]
pub gas: Option<U256>,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "gasPrice")]
pub gas_price: Option<U256>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value: Option<U256>,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<Bytes>,
}
#[must_use = "futures do nothing unless you `.await` or poll them"]
#[pin_project]
pub struct EstimateGasFuture<T: Transport>(#[pin] CompatCallFuture<T, U256>);
impl<T: Transport> EstimateGasFuture<T> {
pub fn from_builder(builder: TransactionBuilder<T>) -> Self {
let eth = builder.web3.eth();
let from = builder.from.map(|account| account.address());
let gas_price = builder.gas_price.and_then(|gas_price| gas_price.value());
let request = EstimateGasRequest {
from,
to: builder.to,
gas: None,
gas_price,
value: builder.value,
data: builder.data,
};
EstimateGasFuture::from_request(eth, request)
}
pub(crate) fn from_request(eth: Eth<T>, request: EstimateGasRequest) -> Self {
EstimateGasFuture(
CallFuture::new(
eth.transport()
.execute("eth_estimateGas", vec![helpers::serialize(&request)]),
)
.compat(),
)
}
pub(crate) fn into_inner(self) -> CompatCallFuture<T, U256> {
self.0
}
}
impl<T: Transport> Future for EstimateGasFuture<T> {
type Output = Result<U256, ExecutionError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
self.project().0.poll(cx).map_err(ExecutionError::from)
}
}