diff --git a/Cargo.lock b/Cargo.lock index 2680399..1c67622 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb07629a5d0645d29f68d2fb6f4d0cf15c89ec0965be915f303967180929743f" dependencies = [ "num_enum", + "serde", "strum", ] @@ -701,9 +702,11 @@ name = "arena-core" version = "0.1.4" dependencies = [ "alloy", + "alloy-chains", "alloy-contract", "alloy-sol-macro", "alloy-sol-types", + "alloy-transport-http", "async-trait", "clap", "csv", diff --git a/Cargo.toml b/Cargo.toml index d2b7ac2..cded306 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,4 +22,6 @@ alloy-sol-types = "0.8.0" serde = { version = "1.0", features = ["derive"] } clap = { version = "4.5.16", features = ["derive"] } tokio = { version = "1.39.2", features = ["macros"] } -alloy = { version = "0.3.0", features = ["full", "node-bindings", "json"] } \ No newline at end of file +alloy = { version = "0.3.0", features = ["full", "node-bindings", "json"] } +alloy-chains = "0.1.29" +alloy-transport-http = "0.3.0" diff --git a/src/arena.rs b/src/arena.rs index 7a43e6d..0fc095f 100644 --- a/src/arena.rs +++ b/src/arena.rs @@ -1,6 +1,11 @@ use std::collections::HashMap; -use alloy::{primitives::{U256, Uint}, providers::ProviderBuilder, signers::local::PrivateKeySigner}; +use alloy::{ + primitives::{Uint, U256}, + providers::{Provider, ProviderBuilder, WalletProvider}, + signers::local::PrivateKeySigner, +}; + use super::*; use crate::{ config::Config, @@ -14,7 +19,6 @@ use crate::{ ArenaToken, LiquidExchange, }, }; - /// Represents an [`Arena`] that can be used to run a simulation and execute strategies. pub struct Arena { /// The underlying Anvil execution environment. @@ -36,8 +40,6 @@ pub struct Arena { pub arbitrageur: Box, providers: HashMap, - - nonce: u64, } #[allow(clippy::redundant_closure)] @@ -92,7 +94,12 @@ impl Arena { Uint::from(79228162514264337593543950336_u128), Bytes::default(), ) - .nonce(5) + .nonce( + admin_provider + .get_transaction_count(admin_provider.default_signer_address()) + .await + .unwrap(), + ) .send() .await .map_err(ArenaError::ContractError)? @@ -133,7 +140,6 @@ impl Arena { } self.arbitrageur.init(&signal, admin_provider.clone()).await; - self.nonce = 6; for step in 0..config.steps { let id = fetcher @@ -163,7 +169,12 @@ impl Arena { alloy::primitives::utils::parse_ether(&self.feed.step().to_string()) .map_err(ArenaError::ConversionError)?, ) - .nonce(self.nonce) + .nonce( + admin_provider + .get_transaction_count(admin_provider.default_signer_address()) + .await + .unwrap(), + ) .send() .await .map_err(ArenaError::ContractError)? @@ -171,8 +182,6 @@ impl Arena { .await .map_err(|e| ArenaError::PendingTransactionError(e))?; - self.nonce += 1; - self.arbitrageur .arbitrage(&signal, admin_provider.clone()) .await; @@ -300,7 +309,6 @@ impl ArenaBuilder { feed: self.feed.unwrap(), inspector: self.inspector.unwrap(), arbitrageur: self.arbitrageur.unwrap(), - nonce: 0, providers, } } diff --git a/src/engine/arbitrageur.rs b/src/engine/arbitrageur.rs index a28d517..3a3d1d8 100644 --- a/src/engine/arbitrageur.rs +++ b/src/engine/arbitrageur.rs @@ -7,11 +7,7 @@ use rug::{ops::Pow, Float}; use crate::{ types::{ fetcher::Fetcher, - swap::{ - PoolSwapTest, - PoolSwapTest::TestSettings, - IPoolManager::SwapParams - }, + swap::{IPoolManager::SwapParams, PoolSwapTest, PoolSwapTest::TestSettings}, }, AnvilProvider, Signal, }; @@ -71,8 +67,7 @@ impl Arbitrageur for DefaultArbitrageur { // closed form optimal swap solution, ref: https://arxiv.org/pdf/1911.03380 let fee: u64 = signal.pool.fee.to_string().parse().unwrap(); - let optimal_swap = - Float::with_val(53, 0).max(&(a.clone() - (k / (fee * (a / b))))); + let optimal_swap = Float::with_val(53, 0).max(&(a.clone() - (k / (fee * (a / b))))); let zero_for_one = current_tick > target_tick; @@ -125,7 +120,11 @@ impl DefaultArbitrageur { .poolId; let tick_info = fetcher - .getTickInfo(signal.manager, pool_id, Signed::from_str(&tick.to_string()).unwrap()) + .getTickInfo( + signal.manager, + pool_id, + Signed::from_str(&tick.to_string()).unwrap(), + ) .call() .await .unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 93dcaa0..a9d69c9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,12 +18,10 @@ pub mod engine; /// Contains error types for Arena. pub mod error; -use alloy::primitives::{Signed, Uint}; - use alloy::{ network::{Ethereum, EthereumWallet}, node_bindings::{Anvil, AnvilInstance}, - primitives::{Address, Bytes}, + primitives::{Address, Bytes, Signed, Uint}, providers::{ fillers::{ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller, WalletFiller}, Identity, RootProvider, @@ -217,6 +215,8 @@ impl Signal { #[cfg(test)] mod tests { + use alloy::primitives::{Signed, Uint}; + use super::*; use crate::{ arena::{Arena, ArenaBuilder}, @@ -229,7 +229,6 @@ mod tests { strategy::Strategy, }; - struct StrategyMock; impl Strategy for StrategyMock { @@ -255,13 +254,13 @@ mod tests { let mut arena: Arena<_> = builder .with_strategy(Box::new(StrategyMock {})) - .with_fee(4000) - .with_tick_spacing(2) + .with_fee(Uint::from(4000)) + .with_tick_spacing(Signed::try_from(2).unwrap()) .with_feed(Box::new(OrnsteinUhlenbeck::new(0.1, 0.1, 0.1, 0.1, 0.1))) .with_inspector(Box::new(EmptyInspector {})) .with_arbitrageur(Box::new(EmptyArbitrageur {})) .build(); - arena.run(Config::new(5)).await; + arena.run(Config::new(5)).await.unwrap(); } }