2020-09-24 21:33:09 +00:00
|
|
|
use async_trait::async_trait;
|
2021-10-29 12:29:35 +00:00
|
|
|
use ethers_core::types::{transaction::eip2718::TypedTransaction, *};
|
2020-12-17 11:26:01 +00:00
|
|
|
use ethers_providers::{FromErr, Middleware, PendingTransaction};
|
2020-09-24 21:33:09 +00:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2020-10-08 15:56:36 +00:00
|
|
|
/// Middleware used for calculating nonces locally, useful for signing multiple
|
|
|
|
/// consecutive transactions without waiting for them to hit the mempool
|
|
|
|
pub struct NonceManagerMiddleware<M> {
|
2020-12-31 19:08:12 +00:00
|
|
|
inner: M,
|
|
|
|
initialized: AtomicBool,
|
|
|
|
nonce: AtomicU64,
|
|
|
|
address: Address,
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 15:56:36 +00:00
|
|
|
impl<M> NonceManagerMiddleware<M>
|
2020-09-24 21:33:09 +00:00
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
{
|
2020-12-31 19:08:12 +00:00
|
|
|
/// Instantiates the nonce manager with a 0 nonce. The `address` should be the
|
|
|
|
/// address which you'll be sending transactions from
|
2020-09-24 21:33:09 +00:00
|
|
|
pub fn new(inner: M, address: Address) -> Self {
|
2021-10-29 12:29:35 +00:00
|
|
|
Self { initialized: false.into(), nonce: 0.into(), inner, address }
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the next nonce to be used
|
|
|
|
pub fn next(&self) -> U256 {
|
|
|
|
let nonce = self.nonce.fetch_add(1, Ordering::SeqCst);
|
|
|
|
nonce.into()
|
|
|
|
}
|
|
|
|
|
2022-01-31 19:04:26 +00:00
|
|
|
pub async fn initialize_nonce(
|
|
|
|
&self,
|
|
|
|
block: Option<BlockId>,
|
|
|
|
) -> Result<U256, NonceManagerError<M>> {
|
|
|
|
// initialize the nonce the first time the manager is called
|
|
|
|
if !self.initialized.load(Ordering::SeqCst) {
|
|
|
|
let nonce = self
|
|
|
|
.inner
|
|
|
|
.get_transaction_count(self.address, block)
|
|
|
|
.await
|
|
|
|
.map_err(FromErr::from)?;
|
|
|
|
self.nonce.store(nonce.as_u64(), Ordering::SeqCst);
|
|
|
|
self.initialized.store(true, Ordering::SeqCst);
|
2023-01-05 09:47:05 +00:00
|
|
|
Ok(nonce)
|
|
|
|
} else {
|
|
|
|
// return current nonce
|
|
|
|
Ok(self.nonce.load(Ordering::SeqCst).into())
|
2022-01-31 19:04:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
async fn get_transaction_count_with_manager(
|
|
|
|
&self,
|
2021-03-16 19:46:07 +00:00
|
|
|
block: Option<BlockId>,
|
2020-09-24 21:33:09 +00:00
|
|
|
) -> Result<U256, NonceManagerError<M>> {
|
|
|
|
// initialize the nonce the first time the manager is called
|
|
|
|
if !self.initialized.load(Ordering::SeqCst) {
|
|
|
|
let nonce = self
|
|
|
|
.inner
|
|
|
|
.get_transaction_count(self.address, block)
|
|
|
|
.await
|
|
|
|
.map_err(FromErr::from)?;
|
|
|
|
self.nonce.store(nonce.as_u64(), Ordering::SeqCst);
|
|
|
|
self.initialized.store(true, Ordering::SeqCst);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.next())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
2020-10-08 15:56:36 +00:00
|
|
|
/// Thrown when an error happens at the Nonce Manager
|
2020-09-24 21:33:09 +00:00
|
|
|
pub enum NonceManagerError<M: Middleware> {
|
2020-10-08 15:56:36 +00:00
|
|
|
/// Thrown when the internal middleware errors
|
2020-09-24 21:33:09 +00:00
|
|
|
#[error("{0}")]
|
|
|
|
MiddlewareError(M::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<M: Middleware> FromErr<M::Error> for NonceManagerError<M> {
|
|
|
|
fn from(src: M::Error) -> Self {
|
|
|
|
NonceManagerError::MiddlewareError(src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-23 09:56:44 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
|
|
|
|
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
|
2020-10-08 15:56:36 +00:00
|
|
|
impl<M> Middleware for NonceManagerMiddleware<M>
|
2020-09-24 21:33:09 +00:00
|
|
|
where
|
|
|
|
M: Middleware,
|
|
|
|
{
|
|
|
|
type Error = NonceManagerError<M>;
|
|
|
|
type Provider = M::Provider;
|
|
|
|
type Inner = M;
|
|
|
|
|
|
|
|
fn inner(&self) -> &M {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
|
2021-12-13 22:50:13 +00:00
|
|
|
async fn fill_transaction(
|
|
|
|
&self,
|
|
|
|
tx: &mut TypedTransaction,
|
|
|
|
block: Option<BlockId>,
|
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
if tx.nonce().is_none() {
|
|
|
|
tx.set_nonce(self.get_transaction_count_with_manager(block).await?);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(self.inner().fill_transaction(tx, block).await.map_err(FromErr::from)?)
|
|
|
|
}
|
|
|
|
|
2020-09-24 21:33:09 +00:00
|
|
|
/// Signs and broadcasts the transaction. The optional parameter `block` can be passed so that
|
|
|
|
/// gas cost and nonce calculations take it into account. For simple transactions this can be
|
|
|
|
/// left to `None`.
|
2021-08-09 00:31:11 +00:00
|
|
|
async fn send_transaction<T: Into<TypedTransaction> + Send + Sync>(
|
2020-09-24 21:33:09 +00:00
|
|
|
&self,
|
2021-08-09 00:31:11 +00:00
|
|
|
tx: T,
|
2021-03-16 19:46:07 +00:00
|
|
|
block: Option<BlockId>,
|
2020-12-17 11:26:01 +00:00
|
|
|
) -> Result<PendingTransaction<'_, Self::Provider>, Self::Error> {
|
2021-08-09 00:31:11 +00:00
|
|
|
let mut tx = tx.into();
|
|
|
|
|
|
|
|
if tx.nonce().is_none() {
|
|
|
|
tx.set_nonce(self.get_transaction_count_with_manager(block).await?);
|
2020-09-24 21:33:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-09 00:31:11 +00:00
|
|
|
match self.inner.send_transaction(tx.clone(), block).await {
|
2020-09-24 21:33:09 +00:00
|
|
|
Ok(tx_hash) => Ok(tx_hash),
|
|
|
|
Err(err) => {
|
|
|
|
let nonce = self.get_transaction_count(self.address, block).await?;
|
|
|
|
if nonce != self.nonce.load(Ordering::SeqCst).into() {
|
|
|
|
// try re-submitting the transaction with the correct nonce if there
|
|
|
|
// was a nonce mismatch
|
|
|
|
self.nonce.store(nonce.as_u64(), Ordering::SeqCst);
|
2021-08-09 00:31:11 +00:00
|
|
|
tx.set_nonce(nonce);
|
2021-10-29 12:29:35 +00:00
|
|
|
self.inner.send_transaction(tx, block).await.map_err(FromErr::from)
|
2020-09-24 21:33:09 +00:00
|
|
|
} else {
|
|
|
|
// propagate the error otherwise
|
|
|
|
Err(FromErr::from(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|