Skip to content

Commit

Permalink
feat: versatile nonce management even tho i shouldnt have to kek
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Sep 1, 2024
1 parent 183c007 commit 333e4a7
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 26 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
alloy = { version = "0.3.0", features = ["full", "node-bindings", "json"] }
alloy-chains = "0.1.29"
alloy-transport-http = "0.3.0"
28 changes: 18 additions & 10 deletions src/arena.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<V> {
/// The underlying Anvil execution environment.
Expand All @@ -36,8 +40,6 @@ pub struct Arena<V> {
pub arbitrageur: Box<dyn Arbitrageur>,

providers: HashMap<usize, AnvilProvider>,

nonce: u64,
}

#[allow(clippy::redundant_closure)]
Expand Down Expand Up @@ -92,7 +94,12 @@ impl<V> Arena<V> {
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)?
Expand Down Expand Up @@ -133,7 +140,6 @@ impl<V> Arena<V> {
}

self.arbitrageur.init(&signal, admin_provider.clone()).await;
self.nonce = 6;

for step in 0..config.steps {
let id = fetcher
Expand Down Expand Up @@ -163,16 +169,19 @@ impl<V> Arena<V> {
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)?
.watch()
.await
.map_err(|e| ArenaError::PendingTransactionError(e))?;

self.nonce += 1;

self.arbitrageur
.arbitrage(&signal, admin_provider.clone())
.await;
Expand Down Expand Up @@ -300,7 +309,6 @@ impl<V> ArenaBuilder<V> {
feed: self.feed.unwrap(),
inspector: self.inspector.unwrap(),
arbitrageur: self.arbitrageur.unwrap(),
nonce: 0,
providers,
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/engine/arbitrageur.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down
13 changes: 6 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -217,6 +215,8 @@ impl Signal {

#[cfg(test)]
mod tests {
use alloy::primitives::{Signed, Uint};

use super::*;
use crate::{
arena::{Arena, ArenaBuilder},
Expand All @@ -229,7 +229,6 @@ mod tests {
strategy::Strategy,
};


struct StrategyMock;

impl<T> Strategy<T> for StrategyMock {
Expand All @@ -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();
}
}

0 comments on commit 333e4a7

Please sign in to comment.