Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
fix comment
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Oct 29, 2024
1 parent 3f38e53 commit cf494bb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 58 deletions.
6 changes: 1 addition & 5 deletions src/bin/hive_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ async fn main() -> eyre::Result<()> {
let relayer_balance = starknet_provider.balance_at(args.relayer_address, BlockId::Tag(BlockTag::Latest)).await?;
let relayer_balance = into_via_try_wrapper!(relayer_balance)?;

let mut current_nonce = Felt::ZERO;
let relayer = Relayer::new(
args.relayer_address,
relayer_balance,
Expand Down Expand Up @@ -102,11 +101,8 @@ async fn main() -> eyre::Result<()> {
}

for transaction in &body.transactions {
relayer.relay_transaction(transaction, current_nonce).await?;
relayer.relay_transaction(transaction).await?;
tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

// Increase the relayer's nonce
current_nonce += Felt::ONE;
}
}

Expand Down
11 changes: 1 addition & 10 deletions src/pool/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,7 @@ impl<SP: starknet::providers::Provider + Send + Sync + Clone + 'static> AccountM
// Send the Ethereum transaction using the relayer
let transaction_signed = transaction.to_recovered_transaction().into_signed();

// Query the updated nonce for the account from the provider.
// Via on chain query we have the most up-to-date nonce.
let relayer_nonce = manager
.eth_client
.starknet_provider()
.get_nonce(starknet::core::types::BlockId::Tag(BlockTag::Pending), relayer.address())
.await
.unwrap_or_default();

let res = relayer.relay_transaction(&transaction_signed, relayer_nonce).await;
let res = relayer.relay_transaction(&transaction_signed).await;
if res.is_err() {
// If the relayer failed to relay the transaction, we need to reposition it in the mempool
tracing::error!(target: "account_manager", err = ?res.unwrap_err(), "failed to relay transaction");
Expand Down
15 changes: 12 additions & 3 deletions src/providers/eth_provider/starknet/relayer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::{
};
use reth_primitives::TransactionSigned;
use starknet::{
accounts::{Account, ExecutionEncoding, ExecutionV1, SingleOwnerAccount},
core::types::{Felt, NonZeroFelt},
accounts::{Account, ConnectedAccount, ExecutionEncoding, ExecutionV1, SingleOwnerAccount},
core::types::{BlockTag, Felt, NonZeroFelt},
providers::Provider,
signers::{LocalWallet, SigningKey},
};
Expand Down Expand Up @@ -57,7 +57,7 @@ where
/// This is a way to avoid nonce issues.
///
/// Returns the corresponding Starknet transaction hash.
pub async fn relay_transaction(&self, transaction: &TransactionSigned, relayer_nonce: Felt) -> EthApiResult<Felt> {
pub async fn relay_transaction(&self, transaction: &TransactionSigned) -> EthApiResult<Felt> {
// Transform the transaction's data to Starknet calldata
let relayer_address = self.account.address();
let calldata = transaction_data_to_starknet_calldata(transaction, relayer_address)?;
Expand All @@ -69,6 +69,15 @@ where
// Construct the call
let call = starknet::core::types::Call { to: eoa_address, selector: *EXECUTE_FROM_OUTSIDE, calldata };
let mut execution = ExecutionV1::new(vec![call], &self.account);

// Fetch the relayer nonce from the Starknet provider
let relayer_nonce = self
.account
.provider()
.get_nonce(starknet::core::types::BlockId::Tag(BlockTag::Pending), relayer_address)
.await
.unwrap_or_default();

execution = execution.nonce(relayer_nonce);

// We set the max fee to the balance of the account / 5. This means that the account could
Expand Down
18 changes: 2 additions & 16 deletions src/test_utils/eoa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,9 @@ impl<P: Provider + Send + Sync + Clone> KakarotEOA<P> {
.await?;
let relayer_balance = into_via_try_wrapper!(relayer_balance)?;

let nonce = self
.eth_client
.starknet_provider()
.get_nonce(BlockId::Tag(BlockTag::Latest), self.relayer.address())
.await
.unwrap_or_default();

// Relay the transaction
let starknet_transaction_hash = Relayer::new(self.relayer.address(), relayer_balance, self.starknet_provider())
.relay_transaction(&tx_signed, nonce)
.relay_transaction(&tx_signed)
.await
.expect("Failed to relay transaction");

Expand Down Expand Up @@ -232,16 +225,9 @@ impl<P: Provider + Send + Sync + Clone> KakarotEOA<P> {
.await?;
let relayer_balance = into_via_try_wrapper!(relayer_balance)?;

let nonce = self
.eth_client
.starknet_provider()
.get_nonce(BlockId::Tag(BlockTag::Latest), self.relayer.address())
.await
.unwrap_or_default();

// Relay the transaction
let starknet_transaction_hash = Relayer::new(self.relayer.address(), relayer_balance, self.starknet_provider())
.relay_transaction(&tx_signed, nonce)
.relay_transaction(&tx_signed)
.await
.expect("Failed to relay transaction");

Expand Down
27 changes: 3 additions & 24 deletions tests/tests/eth_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use rstest::*;
use starknet::{
accounts::Account,
core::types::{BlockId, BlockTag, Felt},
providers::Provider,
};
use std::sync::Arc;

Expand Down Expand Up @@ -747,15 +746,9 @@ async fn test_send_raw_transaction(#[future] katana_empty: Katana, _setup: ()) {
.expect("Failed to get relayer balance");
let relayer_balance = into_via_try_wrapper!(relayer_balance).expect("Failed to convert balance");

let nonce = eth_client
.starknet_provider()
.get_nonce(BlockId::Tag(BlockTag::Latest), katana.eoa.relayer.address())
.await
.unwrap_or_default();

// Relay the transaction
let _ = Relayer::new(katana.eoa.relayer.address(), relayer_balance, &(*(*eth_client.starknet_provider())))
.relay_transaction(&transaction_signed, nonce)
.relay_transaction(&transaction_signed)
.await
.expect("Failed to relay transaction");

Expand Down Expand Up @@ -996,17 +989,10 @@ async fn test_send_raw_transaction_pre_eip_155(#[future] katana_empty: Katana, _
.expect("Failed to get relayer balance");
let relayer_balance = into_via_try_wrapper!(relayer_balance).expect("Failed to convert balance");

let nonce = katana
.eth_client
.starknet_provider()
.get_nonce(BlockId::Tag(BlockTag::Latest), katana.eoa.relayer.address())
.await
.unwrap_or_default();

// Relay the transaction
let starknet_transaction_hash =
Relayer::new(katana.eoa.relayer.address(), relayer_balance, &(*(*katana.eth_client.starknet_provider())))
.relay_transaction(&transaction_signed, nonce)
.relay_transaction(&transaction_signed)
.await
.expect("Failed to relay transaction");

Expand Down Expand Up @@ -1351,20 +1337,13 @@ async fn test_transaction_by_hash(#[future] katana_empty: Katana, _setup: ()) {
.expect("Failed to get relayer balance");
let relayer_balance = into_via_try_wrapper!(relayer_balance).expect("Failed to convert balance");

let nonce = katana_empty
.eth_client
.starknet_provider()
.get_nonce(BlockId::Tag(BlockTag::Latest), katana_empty.eoa.relayer.address())
.await
.unwrap_or_default();

// Relay the transaction
let _ = Relayer::new(
katana_empty.eoa.relayer.address(),
relayer_balance,
&(*(*katana_empty.eth_client.starknet_provider())),
)
.relay_transaction(&transaction_signed, nonce)
.relay_transaction(&transaction_signed)
.await
.expect("Failed to relay transaction");

Expand Down

0 comments on commit cf494bb

Please sign in to comment.