Skip to content

Commit

Permalink
feat: passing test suite, failing in anvil
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu authored Sep 8, 2024
1 parent bdacbb3 commit 444095b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 27 deletions.
2 changes: 1 addition & 1 deletion contracts/utils/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
src = "src"
out = "out"
libs = ["lib"]

evm-version = "cancun"
# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
101 changes: 88 additions & 13 deletions src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ impl<V> Arena<V> {

let engine = Engine {
pool: self.pool.clone().into(),
provider: admin_provider.clone(),
liquidity_manager: *lp_manager.address(),
};

Expand Down Expand Up @@ -144,12 +143,86 @@ impl<V> Arena<V> {
slot.sqrtPriceX96,
);

strategy.init(
self.providers[&(idx + 1)].clone(),
signal.clone(),
&mut self.inspector,
engine.clone(),
);
let strategy_provider = self.providers[&(idx + 1)].clone();

currency_0
.mint(strategy_provider.default_signer_address(), U256::MAX)
.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))?;

println!("this 1");

currency_1
.mint(strategy_provider.default_signer_address(), U256::MAX)
.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))?;

println!("this 2");

currency_0
.approve(*lp_manager.address(), U256::MAX)
.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))?;

println!("this 3");

currency_1
.approve(*lp_manager.address(), U256::MAX)
.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))?;

println!("this 4");

strategy
.init(
self.providers[&(idx + 1)].clone(),
signal.clone(),
&mut self.inspector,
engine.clone(),
)
.await;

println!("this 5");
}

self.arbitrageur.init(&signal, admin_provider.clone()).await;
Expand Down Expand Up @@ -200,12 +273,14 @@ impl<V> Arena<V> {
.await;

for (idx, strategy) in self.strategies.iter_mut().enumerate() {
strategy.process(
self.providers[&(idx + 1)].clone(),
signal.clone(),
&mut self.inspector,
engine.clone(),
);
strategy
.process(
self.providers[&(idx + 1)].clone(),
signal.clone(),
&mut self.inspector,
engine.clone(),
)
.await;
}

self.feed.step();
Expand Down
13 changes: 7 additions & 6 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl PoolParameters {
#[derive(Debug, Clone)]
pub struct Engine {
pub pool: PoolParameters,
pub(crate) provider: AnvilProvider,
pub(crate) liquidity_manager: Address,
}

Expand All @@ -66,24 +65,26 @@ impl Engine {
key: PoolKey,
params: ModifyLiquidityParams,
hook_data: Bytes,
provider: AnvilProvider,
) -> Result<(), ArenaError> {
let lp_manager =
PoolModifyLiquidityTest::new(self.liquidity_manager, self.provider.clone());
let lp_manager = PoolModifyLiquidityTest::new(self.liquidity_manager, provider.clone());

lp_manager
.modifyLiquidity_0(key.into(), params, hook_data, false, false)
.modifyLiquidity_0(key.into(), params, hook_data, true, false)
.nonce(
self.provider
.get_transaction_count(self.provider.default_signer_address())
provider
.get_transaction_count(provider.default_signer_address())
.await
.unwrap(),
)
.value(U256::from(0))
.send()
.await
.map_err(ArenaError::ContractError)?
.watch()
.await
.map_err(|e| ArenaError::PendingTransactionError(e))?;

Ok(())
}
}
15 changes: 8 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ impl Signal {

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

use super::*;
Expand All @@ -256,28 +256,29 @@ mod tests {
strategy::Strategy,
types::modify_liquidity::IPoolManager::ModifyLiquidityParams,
};
use alloy::primitives::FixedBytes;

struct StrategyMock;

#[async_trait]
impl<T> Strategy<T> for StrategyMock {
async fn init(
&self,
_provider: AnvilProvider,
provider: AnvilProvider,
signal: Signal,
_inspector: &mut Box<dyn Inspector<T>>,
engine: Engine,
) {
let params = ModifyLiquidityParams {
tickLower: Signed::try_from(-2).unwrap(),
tickUpper: Signed::try_from(2).unwrap(),
liquidityDelta: Signed::try_from(1000).unwrap(),
tickLower: Signed::try_from(-20).unwrap(),
tickUpper: Signed::try_from(20).unwrap(),
liquidityDelta: Signed::try_from(100000000000_u128).unwrap(),
salt: FixedBytes::ZERO,
};

println!("{:#?}", signal);

engine
.modify_liquidity(signal.pool, params, Bytes::new())
.modify_liquidity(signal.pool, params, Bytes::new(), provider.clone())
.await
.unwrap();
}
Expand Down

0 comments on commit 444095b

Please sign in to comment.