Skip to content

Commit

Permalink
patch g3m swap fee
Browse files Browse the repository at this point in the history
  • Loading branch information
kinrezC committed Oct 10, 2023
1 parent 33abfb1 commit ef33739
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 170 deletions.
332 changes: 166 additions & 166 deletions box-core/src/membrane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,169 +119,169 @@ pub trait Membrane: Sync + Send + Debug {
async fn get_weights(&self) -> anyhow::Result<(f64, f64), anyhow::Error>;
}

#[cfg(test)]
mod tests {

use super::*;
use anyhow::anyhow;
use bindings::counter::{Counter, COUNTER_ABI, COUNTER_BYTECODE};
use ethers::{prelude::*, types::transaction::eip2718::TypedTransaction};
use std::sync::Arc;

/// Mock membrane has two fields:
/// - client - The middleware client.
/// - contract - The Counter smart contract instance. Mutable so it can be overridden after deployed.
/// M type must be restricted to the middleware client type.
#[derive(Debug, Clone)]
pub struct MockMembrane<M>
where
M: Middleware + Clone + Send + Sync + 'static,
{
client: Arc<M>,
contract: Counter<M>,
}

impl<M> MockMembrane<M>
where
M: Middleware + Clone + Send + Sync + 'static,
{
/// Creates a new mock membrane.
pub fn new(client: Arc<M>) -> Self {
let contract = Counter::new(ethers::types::Address::zero(), client.clone());
Self { client, contract }
}

/// Deploys the smart contract to the network.
/// - returns: The address of the deployed smart contract.
/// - errors: If the deployment fails, an error is returned.
/// - notes: This is only used for testing.
pub async fn deploy(mut self) -> Self {
let contract = Counter::deploy(self.client.clone(), ())
.unwrap()
.send()
.await
.unwrap();

// Override contract to use the address.
self.contract = contract;

self
}
}

/// Implements the Membrane trait for the MockMembrane.
/// This is where the middleware client is used to call the smart contract.
/// The middleware methods act as the standardized api.
///
/// Two methods are implemented that call it's Mock "contract" (Counter smart contract):
/// - rebalance() - Calls "increment()": Increments the counter by 1.
/// - get_spot_price() - Calls "number()": Fetches the current count.
#[async_trait]
impl Membrane for MockMembrane<SignerMiddleware<Provider<Http>, LocalWallet>> {
type Client = Arc<SignerMiddleware<Provider<Http>, LocalWallet>>;
async fn rebalance(
&self,
_swap_direction: bool,
_amount: f64,
) -> anyhow::Result<f64, anyhow::Error> {
// Increments the counter by 1.
let res = self.contract.increment().send().await?.await?;

// Returns "1.0" if success, errors otherwise.
let outcome = match res {
Some(_) => Ok(1.0),
None => Err(anyhow!("No result returned from contract call.")),
}?;

Ok(outcome)
}

async fn add_liquidity(
&self,
asset_1: &str,
asset_2: &str,
amount_1: f64,
amount_2: f64,
) -> anyhow::Result<f64, anyhow::Error> {
Ok(0.0)
}

async fn remove_liquidity(
&self,
asset_1: &str,
asset_2: &str,
amount: f64,
) -> anyhow::Result<(f64, f64), anyhow::Error> {
Ok((0.0, 0.0))
}

async fn update_weights(
&self,
asset: &str,
weight: f64,
) -> anyhow::Result<(&str, f64, &str, f64), anyhow::Error> {
Ok(("", 0.0, "", 0.0))
}

async fn get_spot_price(&self) -> anyhow::Result<f64, anyhow::Error> {
let count = self.contract.clone().number().await.unwrap();
let count = count.as_u64() as f64;

Ok(count)
}

async fn get_weights(&self) -> anyhow::Result<(f64, f64), anyhow::Error> {
Ok((0.0, 0.0))
}
}

#[tokio::test]
async fn test_counter() {
// 1. Create an anvil instance.
let anvil = ethers::core::utils::Anvil::new().spawn();

// 2. Get a wallet using the anvil instance developer wallets.
let wallet: LocalWallet = anvil.keys()[0].clone().into();

// 3. Connect to the network
let provider = Provider::<Http>::try_from(anvil.endpoint())
.unwrap()
.interval(std::time::Duration::from_millis(10u64));

// 4. Instantiate the client with the wallet
let client = SignerMiddleware::new(provider, wallet.with_chain_id(anvil.chain_id()));
let client = Arc::new(client);

// 5. Instantiate the membrane implementation with the client.
let mut membrane = MockMembrane::new(client.clone());

// 6. Deploy the smart contract of the membrane implementation using the client.
println!(
"membrane contract address: {:?}",
membrane.contract.clone().address()
);
membrane = membrane.deploy().await;
println!(
"membrane contract address: {:?}",
membrane.contract.clone().address()
);

// 7. Check the initial count is zero.
let count = membrane.get_spot_price().await.unwrap();
assert_eq!(count, 0.0);
println!("count: {}", count);

// 8. Calls rebalance(), which implements the increment() call.
let is_success = membrane.rebalance(true, 0.0).await.unwrap();
assert_eq!(is_success, 1.0); // Returns 1.0 if success, just to make things easy for us.

// 9. Check the count is now one.
let count = membrane.get_spot_price().await.unwrap();
assert_eq!(count, 1.0);
println!("count: {}", count);
}

async fn test_counter_arbiter() {
todo!("Test the arbiter membrane implementation.")
}
}
// #[cfg(test)]
// mod tests {

// use super::*;
// use anyhow::anyhow;
// use bindings::counter::{Counter, COUNTER_ABI, COUNTER_BYTECODE};
// use ethers::{prelude::*, types::transaction::eip2718::TypedTransaction};
// use std::sync::Arc;

// /// Mock membrane has two fields:
// /// - client - The middleware client.
// /// - contract - The Counter smart contract instance. Mutable so it can be overridden after deployed.
// /// M type must be restricted to the middleware client type.
// #[derive(Debug, Clone)]
// pub struct MockMembrane<M>
// where
// M: Middleware + Clone + Send + Sync + 'static,
// {
// client: Arc<M>,
// contract: Counter<M>,
// }

// impl<M> MockMembrane<M>
// where
// M: Middleware + Clone + Send + Sync + 'static,
// {
// /// Creates a new mock membrane.
// pub fn new(client: Arc<M>) -> Self {
// let contract = Counter::new(ethers::types::Address::zero(), client.clone());
// Self { client, contract }
// }

// /// Deploys the smart contract to the network.
// /// - returns: The address of the deployed smart contract.
// /// - errors: If the deployment fails, an error is returned.
// /// - notes: This is only used for testing.
// pub async fn deploy(mut self) -> Self {
// let contract = Counter::deploy(self.client.clone(), ())
// .unwrap()
// .send()
// .await
// .unwrap();

// // Override contract to use the address.
// self.contract = contract;

// self
// }
// }

// /// Implements the Membrane trait for the MockMembrane.
// /// This is where the middleware client is used to call the smart contract.
// /// The middleware methods act as the standardized api.
// ///
// /// Two methods are implemented that call it's Mock "contract" (Counter smart contract):
// /// - rebalance() - Calls "increment()": Increments the counter by 1.
// /// - get_spot_price() - Calls "number()": Fetches the current count.
// #[async_trait]
// impl Membrane for MockMembrane<SignerMiddleware<Provider<Http>, LocalWallet>> {
// type Client = Arc<SignerMiddleware<Provider<Http>, LocalWallet>>;
// async fn rebalance(
// &self,
// _swap_direction: bool,
// _amount: f64,
// ) -> anyhow::Result<f64, anyhow::Error> {
// // Increments the counter by 1.
// let res = self.contract.increment().send().await?.await?;

// // Returns "1.0" if success, errors otherwise.
// let outcome = match res {
// Some(_) => Ok(1.0),
// None => Err(anyhow!("No result returned from contract call.")),
// }?;

// Ok(outcome)
// }

// async fn add_liquidity(
// &self,
// asset_1: &str,
// asset_2: &str,
// amount_1: f64,
// amount_2: f64,
// ) -> anyhow::Result<f64, anyhow::Error> {
// Ok(0.0)
// }

// async fn remove_liquidity(
// &self,
// asset_1: &str,
// asset_2: &str,
// amount: f64,
// ) -> anyhow::Result<(f64, f64), anyhow::Error> {
// Ok((0.0, 0.0))
// }

// async fn update_weights(
// &self,
// asset: &str,
// weight: f64,
// ) -> anyhow::Result<(&str, f64, &str, f64), anyhow::Error> {
// Ok(("", 0.0, "", 0.0))
// }

// async fn get_spot_price(&self) -> anyhow::Result<f64, anyhow::Error> {
// let count = self.contract.clone().number().await.unwrap();
// let count = count.as_u64() as f64;

// Ok(count)
// }

// async fn get_weights(&self) -> anyhow::Result<(f64, f64), anyhow::Error> {
// Ok((0.0, 0.0))
// }
// }

// #[tokio::test]
// async fn test_counter() {
// // 1. Create an anvil instance.
// let anvil = ethers::core::utils::Anvil::new().spawn();

// // 2. Get a wallet using the anvil instance developer wallets.
// let wallet: LocalWallet = anvil.keys()[0].clone().into();

// // 3. Connect to the network
// let provider = Provider::<Http>::try_from(anvil.endpoint())
// .unwrap()
// .interval(std::time::Duration::from_millis(10u64));

// // 4. Instantiate the client with the wallet
// let client = SignerMiddleware::new(provider, wallet.with_chain_id(anvil.chain_id()));
// let client = Arc::new(client);

// // 5. Instantiate the membrane implementation with the client.
// let mut membrane = MockMembrane::new(client.clone());

// // 6. Deploy the smart contract of the membrane implementation using the client.
// println!(
// "membrane contract address: {:?}",
// membrane.contract.clone().address()
// );
// membrane = membrane.deploy().await;
// println!(
// "membrane contract address: {:?}",
// membrane.contract.clone().address()
// );

// // 7. Check the initial count is zero.
// let count = membrane.get_spot_price().await.unwrap();
// assert_eq!(count, 0.0);
// println!("count: {}", count);

// // 8. Calls rebalance(), which implements the increment() call.
// let is_success = membrane.rebalance(true, 0.0).await.unwrap();
// assert_eq!(is_success, 1.0); // Returns 1.0 if success, just to make things easy for us.

// // 9. Check the count is now one.
// let count = membrane.get_spot_price().await.unwrap();
// assert_eq!(count, 1.0);
// println!("count: {}", count);
// }

// async fn test_counter_arbiter() {
// todo!("Test the arbiter membrane implementation.")
// }
// }
17 changes: 13 additions & 4 deletions box-simulation/src/setup/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ pub async fn deploy_contracts(env: &Environment) -> Result<Contracts, anyhow::Er
.await?;

let g3m_args = (
tokens.arbx.address(),
tokens.arby.address(),
U256::from(10u64.pow(18) / 2),
tokens.arbx.address(), //tokena
tokens.arby.address(), //tokenb
U256::from(10u64.pow(18) / 2), //weight
U256::from(100)
);

let lex_args = (
Expand All @@ -65,7 +66,15 @@ pub async fn deploy_contracts(env: &Environment) -> Result<Contracts, anyhow::Er

let contracts = Contracts { tokens, exchanges };

// let g3m_listener = EventLogger::builder().add(g3m.events(), "g3m").run().await?;
let listener = EventLogger::builder()
.add(contracts.exchanges.lex.events(), "lex")
.add(contracts.tokens.arbx.events(), "arbx")
.add(contracts.tokens.arby.events(), "arby")
.run();

// agents
// 1. arbitraguer :check:
// 2. rebalancer

Ok(contracts)
}

0 comments on commit ef33739

Please sign in to comment.