ethexe-ethereum 2.0.0

Ethereum contract bindings and transaction helpers for ethexe
Documentation
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! # ethexe-ethereum
//!
//! Ethereum contract-interaction layer for the ethexe system: typed Rust wrappers around the
//! ethexe Solidity contracts (Router, Mirror, WrappedVara, Middleware), built on the `alloy`
//! client stack.
//!
//! Consumed by `ethexe-observer`, `ethexe-consensus`, `ethexe-cli`, `ethexe-service`, and
//! `ethexe-sdk`; depends on `ethexe-common`, `gsigner`, and `gprimitives`. This is purely a
//! client/binding layer: it does not execute Gear programs (`ethexe-processor`), watch the
//! chain (`ethexe-observer`), implement consensus (`ethexe-consensus`), or persist data
//! (`ethexe-db`).
//!
//! ## Usage
//!
//! Build a client with [`EthereumBuilder`], then call methods on the returned [`Ethereum`]:
//!
//! ```rust,no_run
//! use ethexe_ethereum::{Ethereum, EthereumBuilder};
//!
//! # async fn example(signer: gsigner::secp256k1::Signer, sender: gsigner::secp256k1::Address) -> anyhow::Result<()> {
//! let ethereum = EthereumBuilder::default()
//!     .router_address(Ethereum::DEFAULT_ROUTER_ADDRESS.into())
//!     .signer(signer)
//!     .sender_address(sender)
//!     .build()
//!     .await?;
//!
//! let router = ethereum.router(); // Router.sol write handle
//! let mirror = ethereum.mirror(Default::default()); // per-program Mirror.sol handle
//! mirror.send_message(vec![], 0).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Public API
//!
//! - [`EthereumBuilder`] — Fluent builder: RPC URL, router address, signer, fee tuning
//! - [`Ethereum`] — Central connection; vends contract handles and block queries
//! - [`AlloyProvider`] — Concrete alloy provider stack; returned by [`Ethereum::provider`]
//! - [`router::Router`] — Router.sol: code validation, batch commitment, program creation
//! - [`mirror::Mirror`] — Mirror.sol: send/reply messages, claim value, top up balances
//! - [`wvara::WVara`] — WrappedVara ERC-20: transfer, approve, mint, balance queries
//! - [`middleware::Middleware`] — Validator election/permissions contract
//! - [`TryGetReceipt`] — Retry-aware receipt retrieval with log parsing and revert detection
//! - [`IntoBlockId`] — Convert `H256` / `u32` / `u64` into a block id
//! - [`deploy::EthereumDeployer`] — Deploy the full contract set in local/test environments
//! - [`primitives`] / [`abi`] — Re-exported `alloy::primitives`; generated ABI bindings
//!
//! Each contract handle also vends a read-only query counterpart (`RouterQuery`, `MirrorQuery`,
//! `WVaraQuery`, `MiddlewareQuery`); `Router`, `Mirror`, and `WVara` additionally vend
//! event-filter builders.
//!
//! ## Invariants
//!
//! - [`EthereumBuilder::build`] errors if `signer` or `sender_address` are not set.
//! - [`Ethereum::middleware`] panics if the middleware address is zero, which happens when the
//!   contract set was deployed without the middleware flag on [`deploy::EthereumDeployer`].
//! - [`TryGetReceipt::try_get_message_send_receipt`] requires a `MessageQueueingRequested` log
//!   in the receipt or returns an error.

#![allow(dead_code, clippy::new_without_default)]

use crate::{
    abi::{
        IMirror, IRouter, IWrappedVara,
        utils::{self as abi_utils, Permit},
    },
    wvara::WVaraQuery,
};
use alloy::{
    consensus::SignableTransaction,
    eips::BlockId,
    network::{self, Ethereum as AlloyEthereum, EthereumWallet, Network, TxSigner},
    primitives::{Address as AlloyAddress, B256, ChainId, Signature, U256 as AlloyU256, address},
    providers::{
        Identity, PendingTransactionBuilder, PendingTransactionError, Provider, ProviderBuilder,
        RootProvider, WalletProvider,
        fillers::{
            BlobGasEstimator, BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill,
            NonceFiller, SimpleNonceManager, WalletFiller,
        },
        utils::{self, Eip1559Estimator},
    },
    rpc::types::{TransactionReceipt, TransactionRequest, eth::Log},
    signers::{
        self as alloy_signer, Error as SignerError, Result as SignerResult, Signer as AlloySigner,
        SignerSync, sign_transaction_with_chain_id,
    },
    sol_types::{SolEvent, SolStruct},
    transports::RpcError,
};
use anyhow::{Context, Result, anyhow};
use async_trait::async_trait;
use ethexe_common::{BlockHeader, Digest, SimpleBlockData, ecdsa::PublicKey};
use gprimitives::{ActorId, H256, MessageId};
use gsigner::secp256k1::{Address, Secp256k1SignerExt, Signer};
use middleware::Middleware;
use mirror::Mirror;
use router::{Router, RouterQuery};
use std::time::Duration;

pub mod abi;
pub mod deploy;
pub mod middleware;
pub mod mirror;
pub mod router;
pub mod wvara;

pub mod primitives {
    pub use alloy::primitives::*;
}

pub type AlloyProvider = FillProvider<
    JoinFill<
        JoinFill<
            JoinFill<
                JoinFill<JoinFill<Identity, GasFiller>, BlobGasFiller>,
                NonceFiller<SimpleNonceManager>,
            >,
            ChainIdFiller,
        >,
        WalletFiller<EthereumWallet>,
    >,
    RootProvider,
>;

#[derive(Debug, Clone, Default)]
pub struct EthereumBuilder {
    rpc_url: Option<String>,
    router_address: Option<Address>,
    signer: Option<Signer>,
    sender_address: Option<Address>,
    eip1559_fee_increase_percentage: Option<u64>,
    eip1559_max_fee_per_gas_in_gwei: Option<u128>,
    blob_gas_multiplier: Option<u128>,
    initialize_addresses: Option<bool>,
}

impl EthereumBuilder {
    /// Sets the Ethereum RPC URL to connect to.
    ///
    /// The default is [`Ethereum::DEFAULT_ETHEREUM_RPC`].
    pub fn rpc_url(mut self, rpc_url: impl Into<String>) -> Self {
        self.rpc_url = Some(rpc_url.into());
        self
    }

    /// Sets the Ethereum router contract address.
    ///
    /// The default is [`Ethereum::DEFAULT_ROUTER_ADDRESS`].
    pub fn router_address(mut self, router_address: Address) -> Self {
        self.router_address = Some(router_address);
        self
    }

    /// Sets the signer to use for signing transactions.
    pub fn signer(mut self, signer: Signer) -> Self {
        self.signer = Some(signer);
        self
    }

    /// Sets the sender address to use for signing transactions.
    pub fn sender_address(mut self, sender_address: Address) -> Self {
        self.sender_address = Some(sender_address);
        self
    }

    /// Sets the EIP-1559 fee increase percentage (from "medium") to use for transaction fee estimation.
    pub fn eip1559_fee_increase_percentage_opt(
        mut self,
        eip1559_fee_increase_percentage: Option<u64>,
    ) -> Self {
        self.eip1559_fee_increase_percentage = eip1559_fee_increase_percentage;
        self
    }

    /// Sets the EIP-1559 fee increase percentage (from "medium") to use for transaction fee estimation.
    ///
    /// The default is [`Ethereum::NO_EIP1559_FEE_INCREASE_PERCENTAGE`].
    pub fn eip1559_fee_increase_percentage(self, eip1559_fee_increase_percentage: u64) -> Self {
        self.eip1559_fee_increase_percentage_opt(Some(eip1559_fee_increase_percentage))
    }

    /// Sets the EIP-1559 fee increase percentage to value that increases the estimated fee
    /// by 15% compared to "medium" estimation.
    pub fn with_eip1559_increased_fee(self) -> Self {
        self.eip1559_fee_increase_percentage(Ethereum::INCREASED_EIP1559_FEE_INCREASE_PERCENTAGE)
    }

    /// Sets the EIP-1559 max fee per gas in gwei to use for transaction fee estimation
    /// (for batch commitments).
    pub fn eip1559_max_fee_per_gas_in_gwei(
        mut self,
        eip1559_max_fee_per_gas_in_gwei: u128,
    ) -> Self {
        self.eip1559_max_fee_per_gas_in_gwei = Some(eip1559_max_fee_per_gas_in_gwei);
        self
    }

    /// Sets the blob gas multiplier to use for transaction fee estimation.
    pub fn blob_gas_multiplier_opt(mut self, blob_gas_multiplier: Option<u128>) -> Self {
        self.blob_gas_multiplier = blob_gas_multiplier;
        self
    }

    /// Sets the blob gas multiplier to use for transaction fee estimation.
    ///
    /// The default is [`Ethereum::NO_BLOB_GAS_MULTIPLIER`].
    pub fn blob_gas_multiplier(self, blob_gas_multiplier: u128) -> Self {
        self.blob_gas_multiplier_opt(Some(blob_gas_multiplier))
    }

    /// Sets the blob gas multiplier to value that increases the estimated blob gas by 3x.
    pub fn with_increased_blob_gas_multiplier(self) -> Self {
        self.blob_gas_multiplier(Ethereum::INCREASED_BLOB_GAS_MULTIPLIER)
    }

    /// Sets whether to initialize the router-related addresses (wvara and middleware)
    /// during the construction of the [`Ethereum`] instance.
    pub(crate) fn initialize_addresses(mut self, initialize_addresses: bool) -> Self {
        self.initialize_addresses = Some(initialize_addresses);
        self
    }

    /// Sets whether to initialize the router-related addresses (wvara and middleware)
    /// during the construction of the [`Ethereum`] instance.
    pub(crate) fn without_initializing_addresses(self) -> Self {
        self.initialize_addresses(false)
    }

    /// Constructs [`Ethereum`] instance from the builder.
    pub async fn build(self) -> Result<Ethereum> {
        let rpc_url = self
            .rpc_url
            .unwrap_or_else(|| Ethereum::DEFAULT_ETHEREUM_RPC.into());
        let router_address = self
            .router_address
            .unwrap_or(Ethereum::DEFAULT_ROUTER_ADDRESS.into());
        let signer = self.signer.context("signer is required")?;
        let sender_address = self.sender_address.context("sender address is required")?;
        let eip1559_fee_increase_percentage = self
            .eip1559_fee_increase_percentage
            .unwrap_or(Ethereum::NO_EIP1559_FEE_INCREASE_PERCENTAGE);
        let eip1559_max_fee_per_gas_in_gwei = self
            .eip1559_max_fee_per_gas_in_gwei
            .unwrap_or(Ethereum::NO_EIP1559_MAX_FEE_PER_GAS_IN_GWEI);
        let blob_gas_multiplier = self
            .blob_gas_multiplier
            .unwrap_or(Ethereum::NO_BLOB_GAS_MULTIPLIER);
        let initialize_addresses = self.initialize_addresses.unwrap_or(true);

        Ethereum::new(
            &rpc_url,
            router_address,
            signer,
            sender_address,
            eip1559_fee_increase_percentage,
            eip1559_max_fee_per_gas_in_gwei,
            blob_gas_multiplier,
            initialize_addresses,
        )
        .await
    }
}

#[derive(Debug)]
pub(crate) struct Eip712PermitData {
    pub deadline: AlloyU256,
    pub v: u8,
    pub r: B256,
    pub s: B256,
}

#[derive(Clone)]
pub struct Ethereum {
    router: AlloyAddress,
    wvara: AlloyAddress,
    /// NOTE: Middleware address will be zero if `with_middleware` flag was not passed
    /// for [`deploy::EthereumDeployer`].
    middleware: AlloyAddress,
    provider: AlloyProvider,
    signer: Signer,
    sender_address: Address,
    eip1559_estimator: Eip1559Estimator,
    eip1559_max_fee_per_gas_in_gwei: u128,
}

impl Ethereum {
    /// Default Ethereum RPC.
    pub const DEFAULT_ETHEREUM_RPC: &str = "ws://localhost:8545";
    /// Default Ethereum router contract address.
    pub const DEFAULT_ROUTER_ADDRESS: AlloyAddress =
        address!("Cf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9");

    /// Default EIP-1559 fee increase percentage for transaction fee estimation.
    pub const NO_EIP1559_FEE_INCREASE_PERCENTAGE: u64 = 0;
    /// Default EIP-1559 max fee per gas in gwei for transaction fee estimation (for batch commitments).
    pub const NO_EIP1559_MAX_FEE_PER_GAS_IN_GWEI: u128 = 0;
    /// EIP-1559 fee increase percentage for transaction fee estimation that increases the estimated fee
    /// by 15% compared to "medium" estimation.
    ///
    /// This is useful for faster batch commitment.
    pub const INCREASED_EIP1559_FEE_INCREASE_PERCENTAGE: u64 = 15;

    /// Default blob gas multiplier for transaction fee estimation.
    pub const NO_BLOB_GAS_MULTIPLIER: u128 = 1;
    /// Blob gas multiplier for transaction fee estimation that increases the estimated blob gas by 3x.
    ///
    /// This is useful mostly on testnets, where a lot of L2s can spam the network with blob transactions.
    pub const INCREASED_BLOB_GAS_MULTIPLIER: u128 = 3;

    /// Default offset for permit deadline from the current block timestamp.
    pub const PERMIT_DEADLINE_OFFSET: u64 = 300; // 5 minutes

    #[allow(clippy::too_many_arguments)]
    pub(crate) async fn new(
        ethereum_rpc_url: &str,
        router_address: Address,
        signer: Signer,
        sender_address: Address,
        eip1559_fee_increase_percentage: u64,
        eip1559_max_fee_per_gas_in_gwei: u128,
        blob_gas_multiplier: u128,
        initialize_addresses: bool,
    ) -> Result<Ethereum> {
        let (provider, eip1559_estimator) = create_provider(
            ethereum_rpc_url,
            signer.clone(),
            sender_address,
            eip1559_fee_increase_percentage,
            blob_gas_multiplier,
        )
        .await?;
        let router_query = RouterQuery::from_provider(router_address, provider.root().clone());
        let router = router_address.into();
        let (wvara, middleware) = if initialize_addresses {
            (
                router_query.wvara_address().await?.into(),
                router_query.middleware_address().await?.into(),
            )
        } else {
            (AlloyAddress::ZERO, AlloyAddress::ZERO)
        };
        Ok(Self {
            router,
            wvara,
            middleware,
            provider,
            signer,
            sender_address,
            eip1559_estimator,
            eip1559_max_fee_per_gas_in_gwei,
        })
    }

    pub(crate) async fn initialize_addresses(&mut self) -> Result<()> {
        if self.wvara == AlloyAddress::ZERO && self.middleware == AlloyAddress::ZERO {
            let router_query =
                RouterQuery::from_provider(self.router, self.provider.root().clone());
            self.wvara = router_query.wvara_address().await?.into();
            self.middleware = router_query.middleware_address().await?.into();
        }
        Ok(())
    }

    pub fn signer(&self) -> &Signer {
        &self.signer
    }

    pub(crate) fn sender(&self) -> Sender {
        Sender::new(self.signer.clone(), self.sender_address).expect("infallible")
    }

    pub fn sender_address(&self) -> Address {
        self.sender_address
    }

    pub fn provider(&self) -> AlloyProvider {
        self.provider.clone()
    }

    pub async fn chain_id(&self) -> Result<u64> {
        self.provider.get_chain_id().await.map_err(Into::into)
    }

    pub(crate) async fn get_latest_block_inner(
        provider: &AlloyProvider,
    ) -> Result<SimpleBlockData> {
        Self::get_block_inner(provider, BlockId::latest()).await
    }

    pub(crate) async fn get_block_inner(
        provider: &AlloyProvider,
        block_id: impl IntoBlockId,
    ) -> Result<SimpleBlockData> {
        let block_resp = provider
            .get_block(block_id.into_block_id())
            .await
            .with_context(|| "failed to get latest block")?
            .ok_or_else(|| anyhow!("latest block not found"))?;
        let height = block_resp
            .number()
            .try_into()
            .with_context(|| "block number overflow")?;
        let hash = block_resp.hash().0.into();
        let header = block_resp.into_header();
        let parent_hash = header.parent_hash.0.into();
        let timestamp = header.timestamp;
        let header = BlockHeader {
            height,
            timestamp,
            parent_hash,
        };
        Ok(SimpleBlockData { hash, header })
    }

    pub async fn get_latest_block(&self) -> Result<SimpleBlockData> {
        Self::get_latest_block_inner(&self.provider()).await
    }

    pub async fn get_block(&self, block_id: impl IntoBlockId) -> Result<SimpleBlockData> {
        Self::get_block_inner(&self.provider(), block_id).await
    }

    pub(crate) async fn prepare_permit_data(
        provider: &AlloyProvider,
        wvara_query: WVaraQuery,
        sender: &Sender,
        spender: ActorId,
        value: u128,
    ) -> Result<Eip712PermitData> {
        let signer_address = provider.default_signer_address();
        let signer_actor_id = Address::from(signer_address).into();

        let nonce = abi_utils::u256_to_uint256(wvara_query.nonces(signer_actor_id).await?);
        let eip712_domain = wvara_query.eip712_domain().await?;
        let SimpleBlockData {
            header: BlockHeader { timestamp, .. },
            ..
        } = Self::get_latest_block_inner(provider).await?;
        let deadline = AlloyU256::from(
            timestamp
                .checked_add(Self::PERMIT_DEADLINE_OFFSET)
                .expect("infallible"),
        );

        let permit = Permit {
            owner: signer_address,
            spender: spender.into(),
            value: AlloyU256::from(value),
            nonce,
            deadline,
        };

        let hash = permit.eip712_signing_hash(&eip712_domain);
        let signature = sender.sign_hash(&hash).await?;

        let v = (signature.v() as u8) + 27;
        let r = signature.r().into();
        let s = signature.s().into();

        Ok(Eip712PermitData { deadline, v, r, s })
    }

    pub fn mirror(&self, actor_id: ActorId) -> Mirror {
        Mirror::new(actor_id.into(), self.wvara, self.sender(), self.provider())
    }

    pub fn router(&self) -> Router {
        Router::new(
            self.router,
            self.wvara,
            self.eip1559_estimator.clone(),
            self.eip1559_max_fee_per_gas_in_gwei,
            self.sender(),
            self.provider(),
        )
    }

    pub fn wrapped_vara(&self) -> WVara {
        WVara::new(self.wvara, self.provider())
    }

    pub fn middleware(&self) -> Middleware {
        assert_ne!(
            self.middleware,
            AlloyAddress::ZERO,
            "Middleware address is zero. Make sure to deploy the middleware contract and pass `with_middleware` flag to `EthereumDeployer`."
        );
        Middleware::new(self.middleware, self.provider())
    }
}

pub(crate) async fn create_provider(
    rpc_url: &str,
    signer: Signer,
    sender_address: Address,
    eip1559_fee_increase_percentage: u64,
    blob_gas_multiplier: u128,
) -> Result<(AlloyProvider, Eip1559Estimator)> {
    let eip1559_estimator = Eip1559Estimator::new(move |base_fee_per_gas, rewards| {
        utils::eip1559_default_estimator(base_fee_per_gas, rewards)
            .scaled_by_pct(eip1559_fee_increase_percentage)
    });
    Ok((
        ProviderBuilder::default()
            .with_eip1559_estimator(eip1559_estimator.clone())
            .with_blob_gas_estimator(BlobGasEstimator::scaled(blob_gas_multiplier))
            .with_simple_nonce_management()
            .fetch_chain_id()
            .wallet(EthereumWallet::new(Sender::new(signer, sender_address)?))
            .connect(rpc_url)
            .await?,
        eip1559_estimator,
    ))
}

#[derive(Debug, Clone)]
struct Sender {
    signer: Signer,
    sender: PublicKey,
    chain_id: Option<ChainId>,
}

impl Sender {
    pub fn new(signer: Signer, sender_address: Address) -> Result<Self> {
        let sender = signer
            .get_key_by_address(sender_address)?
            .ok_or_else(|| anyhow!("no key found for {sender_address}"))?;

        Ok(Self {
            signer,
            sender,
            chain_id: None,
        })
    }
}

#[async_trait]
impl AlloySigner for Sender {
    async fn sign_hash(&self, hash: &B256) -> SignerResult<Signature> {
        self.sign_hash_sync(hash)
    }

    fn address(&self) -> AlloyAddress {
        self.sender.to_address().into()
    }

    fn chain_id(&self) -> Option<ChainId> {
        self.chain_id
    }

    fn set_chain_id(&mut self, chain_id: Option<ChainId>) {
        self.chain_id = chain_id;
    }
}

#[async_trait]
impl TxSigner<Signature> for Sender {
    fn address(&self) -> AlloyAddress {
        self.sender.to_address().into()
    }

    async fn sign_transaction(
        &self,
        tx: &mut dyn SignableTransaction<Signature>,
    ) -> SignerResult<Signature> {
        sign_transaction_with_chain_id!(self, tx, self.sign_hash_sync(&tx.signature_hash()))
    }
}

impl SignerSync for Sender {
    fn sign_hash_sync(&self, hash: &B256) -> SignerResult<Signature> {
        let digest = Digest(hash.0);
        let signature = self
            .signer
            .sign_digest(self.sender, digest, None)
            .map_err(|err| SignerError::Other(err.into()))?;
        Signature::from_raw(&signature.as_raw_bytes()).map_err(|err| SignerError::Other(err.into()))
    }

    fn chain_id_sync(&self) -> Option<ChainId> {
        self.chain_id
    }
}

#[async_trait::async_trait]
pub trait TryGetReceipt<N: Network> {
    /// Works like `self.get_receipt().await`, but retries a few times if rpc returns a null response.
    async fn try_get_receipt(self) -> Result<N::ReceiptResponse>;

    /// Works like `self.try_get_receipt().await`, but also extracts the message id from the logs.
    async fn try_get_message_send_receipt(self) -> Result<(H256, MessageId)>;

    /// Works like `self.try_get_receipt().await`, but also checks if the transaction was reverted.
    async fn try_get_receipt_check_reverted(self) -> Result<N::ReceiptResponse>;
}

#[async_trait::async_trait]
impl TryGetReceipt<network::Ethereum> for PendingTransactionBuilder<network::Ethereum> {
    async fn try_get_receipt(self) -> Result<TransactionReceipt> {
        let tx_hash = *self.tx_hash();
        let provider = self.provider().clone();

        let mut err = match self.get_receipt().await {
            Ok(r) => return Ok(r),
            Err(err) => err,
        };

        log::trace!("Failed to get transaction receipt for {tx_hash}. Retrying...");
        for n in 0..20 {
            log::trace!("Attempt {n}. Error - {err}");
            match err {
                PendingTransactionError::TransportError(RpcError::NullResp) => {}
                _ => break,
            }

            tokio::time::sleep(Duration::from_millis(100)).await;

            match provider.get_transaction_receipt(tx_hash).await {
                Ok(Some(r)) => return Ok(r),
                Ok(None) => {}
                Err(e) => err = e.into(),
            }
        }

        Err(anyhow!(
            "Failed to get transaction receipt for {tx_hash}: {err}"
        ))
    }

    async fn try_get_message_send_receipt(self) -> Result<(H256, MessageId)> {
        let receipt = self.try_get_receipt().await?;
        let tx_hash = (*receipt.transaction_hash).into();
        let mut message_id = None;

        for log in receipt.inner.logs() {
            if log.topic0() == Some(&mirror::signatures::MESSAGE_QUEUEING_REQUESTED) {
                let event = crate::decode_log::<IMirror::MessageQueueingRequested>(log)?;

                message_id = Some((*event.id).into());

                break;
            }
        }

        let message_id =
            message_id.ok_or_else(|| anyhow!("Couldn't find `MessageQueueingRequested` log"))?;

        Ok((tx_hash, message_id))
    }

    async fn try_get_receipt_check_reverted(self) -> Result<TransactionReceipt> {
        let provider = self.provider().clone();
        let receipt = self.try_get_receipt().await?;

        let try_request_error_reason = async |provider: RootProvider| {
            let tx = provider
                .get_transaction_by_hash(receipt.transaction_hash)
                .await
                .ok()??;
            let request = TransactionRequest::from_recovered_transaction(tx.into_recovered());
            provider
                .call(request)
                .block(receipt.block_hash?.into())
                .await
                .err()
        };

        if receipt.status() {
            Ok(receipt)
        } else if let Some(err) = try_request_error_reason(provider).await {
            Err(anyhow!(
                "Transaction {:?} was reverted at block {:?}: {err}",
                receipt.transaction_hash,
                receipt.block_hash
            ))
        } else {
            Err(anyhow!(
                "Transaction {:?} was reverted by unknown reason at block {:?}",
                receipt.transaction_hash,
                receipt.block_hash
            ))
        }
    }
}

pub(crate) fn decode_log<E: SolEvent>(log: &Log) -> Result<E> {
    E::decode_raw_log(log.topics(), &log.data().data).map_err(Into::into)
}

macro_rules! signatures_consts {
    (
        $type_name:ident;
        $( $const_name:ident: $name:ident, )*
    ) => {
        $(
            pub const $const_name: alloy::primitives::B256 = $type_name::$name::SIGNATURE_HASH;
        )*

        pub const ALL: &[alloy::primitives::B256] = &[$($const_name,)*];
    };
}

pub(crate) use signatures_consts;

use crate::wvara::WVara;

/// A helping trait for converting various types into `alloy::eips::BlockId`.
pub trait IntoBlockId {
    fn into_block_id(self) -> BlockId;
}

impl IntoBlockId for H256 {
    fn into_block_id(self) -> BlockId {
        BlockId::hash(self.0.into())
    }
}

impl IntoBlockId for u32 {
    fn into_block_id(self) -> BlockId {
        BlockId::number(self.into())
    }
}

impl IntoBlockId for u64 {
    fn into_block_id(self) -> BlockId {
        BlockId::number(self)
    }
}

impl IntoBlockId for BlockId {
    fn into_block_id(self) -> BlockId {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn sender_signs_prehashed_message() {
        let signer = Signer::memory();
        let public_key = signer.generate().unwrap();
        let address = signer.address(public_key);

        let sender = Sender::new(signer.clone(), address).expect("sender init");

        let hash = B256::from([0xAA; 32]);
        let signature = sender.sign_hash_sync(&hash).expect("signature");

        let recovered_vk = signature.recover_from_prehash(&hash).expect("recover");
        let recovered_bytes: [u8; 33] = recovered_vk
            .to_encoded_point(true)
            .as_bytes()
            .try_into()
            .expect("compressed size");
        let recovered_address = gsigner::secp256k1::PublicKey::from_bytes(recovered_bytes)
            .expect("valid public key")
            .to_address();

        assert_eq!(recovered_address, address);
    }
}