Skip to content

Commit

Permalink
fix(gelato-client): add ethers middleware to gelato client for gas es…
Browse files Browse the repository at this point in the history
…timation
  • Loading branch information
luketchang committed May 2, 2022
1 parent db782f6 commit 208fd6b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
28 changes: 16 additions & 12 deletions chains/nomad-ethereum/src/gelato.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ethers::providers::Middleware;
use gelato_relay::{GelatoClient, RelayResponse, TaskState};
use nomad_core::Signers;
use std::marker::PhantomData;
use std::sync::Arc;

/*
{
Expand All @@ -28,8 +28,10 @@ pub(crate) const ACCEPTABLE_STATES: [TaskState; 4] = [
/// Gelato client for submitting txs to single chain
#[derive(Debug, Clone)]
pub struct SingleChainGelatoClient<M> {
/// Base client
pub client: GelatoClient,
/// Gelato client
pub gelato: GelatoClient,
/// Ethers client (for estimating gas)
pub eth_client: Arc<M>,
/// Sponsor signer
pub sponsor: Signers,
/// Chain id
Expand All @@ -38,30 +40,32 @@ pub struct SingleChainGelatoClient<M> {
pub fee_token: String,
/// Transactions are of high priority
pub is_high_priority: bool,
/// Unused
_middleware: PhantomData<M>,
}

impl<M: Middleware + 'static> SingleChainGelatoClient<M> {
impl<M> SingleChainGelatoClient<M>
where
M: Middleware + 'static,
{
/// Get reference to base client
pub fn client(&self) -> &GelatoClient {
&self.client
pub fn gelato(&self) -> &GelatoClient {
&self.gelato
}

/// Instantiate single chain client with default Gelato url
pub fn with_default_url(
eth_client: Arc<M>,
sponsor: Signers,
chain_id: usize,
fee_token: String,
is_high_priority: bool,
) -> Self {
Self {
client: GelatoClient::default(),
gelato: GelatoClient::default(),
eth_client,
sponsor,
chain_id,
fee_token,
is_high_priority,
_middleware: Default::default(),
}
}

Expand All @@ -73,7 +77,7 @@ impl<M: Middleware + 'static> SingleChainGelatoClient<M> {
gas_limit: usize,
) -> Result<RelayResponse, reqwest::Error> {
let relayer_fee = self
.client
.gelato()
.get_estimated_fee(
self.chain_id,
&self.fee_token,
Expand All @@ -82,7 +86,7 @@ impl<M: Middleware + 'static> SingleChainGelatoClient<M> {
)
.await?;

self.client
self.gelato()
.send_relay_transaction(self.chain_id, dest, data, &self.fee_token, relayer_fee)
.await
}
Expand Down
16 changes: 9 additions & 7 deletions chains/nomad-ethereum/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ macro_rules! wrap_http {
/// Create ethers::SignerMiddleware from websockets connection
#[macro_export]
macro_rules! wrap_ws {
($provider:expr, $signer:ident) => {{
($provider:expr, $signer:expr) => {{
// First set the chain ID locally
let provider_chain_id = $provider.get_chainid().await?;
let signer = ethers::signers::Signer::with_chain_id($signer, provider_chain_id.as_u64());
Expand Down Expand Up @@ -188,13 +188,16 @@ macro_rules! tx_submitter_local {
/// Create TxSubmitter::Gelato
#[macro_export]
macro_rules! tx_submitter_gelato {
($chain_id:expr, $gelato_conf:ident) => {{
let sponsor = Signers::try_from_signer_conf(&$gelato_conf.signer).await?;
($base_provider:expr, $gelato_conf:ident) => {{
let signer = Signers::try_from_signer_conf(&$gelato_conf.signer).await?;
let sponsor = signer.clone();
let chain_id = $base_provider.get_chainid().await?.as_usize();
let signing_provider: Arc<_> = wrap_http!($base_provider.clone(), signer); // kludge: only using signing provider for type consistency with TxSubmitter::Local

// TODO: is_high_priority set to false currently, want configurable
let client = SingleChainGelatoClient::with_default_url(
signing_provider,
sponsor,
$chain_id,
chain_id,
$gelato_conf.fee_token,
false,
);
Expand All @@ -220,8 +223,7 @@ macro_rules! boxed_contract {
tx_submitter_local!($base_provider, signer_conf)
}
nomad_xyz_configuration::ethereum::TxSubmitterConf::Gelato(gelato_conf) => {
let chain_id = $base_provider.get_chainid().await?.as_usize();
tx_submitter_gelato!(chain_id, gelato_conf)
tx_submitter_gelato!($base_provider, gelato_conf)
}
};

Expand Down
15 changes: 13 additions & 2 deletions chains/nomad-ethereum/src/submitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ where
Ok(outcome)
}
SubmitterClient::Gelato(client) => {
// If gas limit not hardcoded in tx, eth_estimateGas
let gas_limit = tx
.gas()
.unwrap_or(
&client
.eth_client
.estimate_gas(&tx)
.await
.map_err(|e| ChainCommunicationError::MiddlewareError(e.into()))?,
)
.as_usize();

let tx_data = tx.data().expect("!tx data");
let data = format!("{:x}", tx_data);
let address = format!("{:x}", contract_address);
Expand All @@ -87,7 +99,6 @@ where
"Dispatching tx to Gelato relay."
);

let gas_limit = 100_000; // TODO: clear up with Gelato
let RelayResponse { task_id } = client
.send_relay_transaction(&address, &data, gas_limit)
.await
Expand All @@ -96,7 +107,7 @@ where

loop {
let status = client
.client()
.gelato()
.get_task_status(&task_id)
.await
.map_err(|e| ChainCommunicationError::TxSubmissionError(e.into()))?
Expand Down
3 changes: 3 additions & 0 deletions nomad-core/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ pub enum ChainCommunicationError {
/// Contract Error
#[error("{0}")]
ContractError(Box<dyn StdError + Send + Sync>),
/// Middleware error
#[error("{0}")]
MiddlewareError(Box<dyn StdError + Send + Sync>),
/// Provider Error
#[error("{0}")]
ProviderError(#[from] ProviderError),
Expand Down

0 comments on commit 208fd6b

Please sign in to comment.