Skip to content

Commit

Permalink
feat: remove admin lock, fix price changer
Browse files Browse the repository at this point in the history
  • Loading branch information
ts0yu committed Jul 21, 2024
1 parent 876b8f8 commit e0f11c3
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 44 deletions.
8 changes: 1 addition & 7 deletions contracts/utils/src/LiquidExchange.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,11 @@ contract LiquidExchange {
price = price_;
}

// Our admin lock
modifier onlyAdmin() {
require(msg.sender == admin, "Only admin can call this function");
_;
}

event PriceChange(uint256 price);
event Swap(address tokenIn, address tokenOut, uint256 amountIn, uint256 amountOut, address to);

// Admin only function to set the price of x in terms of y
function setPrice(uint256 _price) public onlyAdmin {
function setPrice(uint256 _price) public {
price = _price;
emit PriceChange(price);
}
Expand Down
41 changes: 28 additions & 13 deletions src/bindings/liquidexchange.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl Behavior<Message> for Deployer {
.unwrap()
.send(To::All, DeploymentResponse::LiquidExchange(*lex.address()))
.await?;

Ok(ControlFlow::Continue)
}
_ => Ok(ControlFlow::Continue),
Expand Down
41 changes: 26 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ use crate::{
},
deployer::{DeploymentRequest, DeploymentResponse},
pool_admin::PoolParams,
price_changer::PriceUpdate,
price_changer::{PriceChanger, PriceUpdate},
types::process::{OrnsteinUhlenbeck, StochasticProcess},
};
use crate::price_changer::PriceChanger;

pub mod arbitrageur;
pub mod bindings;
Expand All @@ -34,7 +33,6 @@ pub mod pool_admin;
pub mod price_changer;
pub mod types;


#[cfg(test)]
mod tests {
use octane::{agent::Agent, world::World};
Expand Down Expand Up @@ -119,7 +117,7 @@ mod tests {
if let DeploymentResponse::Token(address) = query {
self.tokens.push(address);
}

if self.tokens.len() == 2 {
break;
}
Expand All @@ -138,13 +136,24 @@ mod tests {

println!("Tokens: {:?}", self.tokens);

for i in 0..100 {
messager
.send(
To::Agent("pricechanger".to_string()),
PriceUpdate,
)
.await?;
use tokio::time::{sleep, Duration};

while let Some(event) = stream.next().await {
let query: DeploymentResponse = match serde_json::from_str(&event.data) {
Ok(query) => query,
Err(_) => {
// eprintln!("Failed to deserialize the event datfa into a DeploymentResponse");
continue;
}
};

if let DeploymentResponse::LiquidExchange(address) = query {
sleep(Duration::from_millis(3000)).await;
println!("here");
for i in 0..100 {
messager.send(To::All, PriceUpdate).await?;
}
}
}

self.client = Some(client.clone());
Expand All @@ -155,7 +164,7 @@ mod tests {
}

#[tokio::test]
async fn asdfasdf() {
async fn test_price_changer() {
env_logger::init();

let token_deployer = Agent::builder("tdeployer").with_behavior(TokenDeployer {
Expand All @@ -174,15 +183,17 @@ mod tests {
tokens: vec![],
});

let changer = Agent::builder("pricechanger").with_behavior(PriceChanger::new(OrnsteinUhlenbeck::new(0.0, 0.1, 0.1, 0.1)));
let changer = Agent::builder("pricechanger").with_behavior(PriceChanger::new(
OrnsteinUhlenbeck::new(1.0, 0.15, 0.0, 0.3, 1.0 / 252.0),
));

let mut world = World::new("id");

world.add_agent(changer);
world.add_agent(mock_deployer);
world.add_agent(deployer);
world.add_agent(token_deployer);
world.add_agent(changer);

let _ = world.run().await;
}
}
}
32 changes: 29 additions & 3 deletions src/price_changer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,32 @@ impl PriceChanger {
}
}

use serde::{ser::SerializeStruct, Deserializer, Serializer};

#[derive(Debug, Serialize, Deserialize)]
pub struct PriceUpdate;

// impl Serialize for PriceUpdate {
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: Serializer,
// {
// let mut state = serializer.serialize_struct("PriceUpdate", 1)?;
// state.serialize_field("type", "PriceUpdate")?;
// state.end()
// }
// }

// impl<'de> Deserialize<'de> for PriceUpdate {
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
// where
// D: Deserializer<'de>,
// {
// serde::de::Deserialize::deserialize(deserializer)?;
// Ok(PriceUpdate)
// }
// }

#[async_trait::async_trait]
impl Behavior<Message> for PriceChanger {
async fn startup(
Expand All @@ -41,9 +64,12 @@ impl Behavior<Message> for PriceChanger {

while let Some(event) = stream.next().await {
let query: DeploymentResponse = match serde_json::from_str(&event.data) {
Ok(query) => query,
Ok(query) => {
println!("heresd");
query
}
Err(_) => {
eprintln!("Failed to deserialize the event data into a DeploymentResponse");
// eprintln!("Failed to deserialize the event datfa into a DeploymentResponse");
continue;
}
};
Expand All @@ -58,7 +84,7 @@ impl Behavior<Message> for PriceChanger {
}

async fn process(&mut self, event: Message) -> Result<ControlFlow> {
let _query: PriceUpdate = match serde_json::from_str(&event.data) {
let query: PriceUpdate = match serde_json::from_str(&event.data) {
Ok(query) => query,
Err(_) => {
eprintln!("Failed to deserialize the event data into a PriceUpdate");
Expand Down
14 changes: 8 additions & 6 deletions src/types/process.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use rand::thread_rng;
use rand_distr::{Distribution, Normal};
use serde::{Deserialize, Serialize};

pub trait StochasticProcess {
type Value;
Expand All @@ -8,7 +10,7 @@ pub trait StochasticProcess {
fn step(&mut self) -> Self::Value;
}

#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug)]
pub struct OrnsteinUhlenbeck {
current_value: f64,

Expand All @@ -26,13 +28,13 @@ pub struct OrnsteinUhlenbeck {
}

impl OrnsteinUhlenbeck {
pub fn new(initial_value: f64, theta: f64, mu: f64, sigma: f64) -> Self {
pub fn new(initial_value: f64, theta: f64, mu: f64, sigma: f64, dt: f64) -> Self {
OrnsteinUhlenbeck {
current_value: initial_value,
theta,
mu,
sigma,
dt: 0.0,
dt,
}
}
}
Expand All @@ -45,13 +47,13 @@ impl StochasticProcess for OrnsteinUhlenbeck {
}

fn step(&mut self) -> Self::Value {
let mut rng = rand::thread_rng();
let mut rng = thread_rng();
let normal = Normal::new(0.0, 1.0).unwrap();

let drift = self.theta * (self.mu - self.current_value) * self.dt;
let randomness = self.sigma * (self.dt.sqrt()) * normal.sample(&mut rng);
self.current_value += drift + randomness;
let randomness = self.sigma * self.dt.sqrt() * normal.sample(&mut rng);

self.current_value += drift + randomness;
self.current_value
}
}

0 comments on commit e0f11c3

Please sign in to comment.