Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulation records returned as a json object #5

Merged
merged 5 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 90 additions & 24 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ rand_distr = "0.4.3"

funty = "2.0.0"
clap = { version = "4.5.1", features = ["derive"] }
serde = "1.0.198"
serde_json = "1.0.116"
rayon = "1.10.0"
features = "0.10.0"
kdam = { version = "0.5.1", features = ["rayon"] }
4 changes: 2 additions & 2 deletions src/aave/agents/borrow_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Agent for BorrowAgent {
self.address,
self.pool_address,
self.supply_token_address,
U256::from(10_u128.pow(20)),
U256::from(10_u128.pow(18)),
);
self.has_supplied = true;

Expand All @@ -79,7 +79,7 @@ impl Agent for BorrowAgent {

let exp = U256::from(10u128).pow(self.borrow_token_decimals - U256::from(4u128));
// Agent borrows a random fraction of the available to borrow amount
let u = U256::from(rng.gen_range(9000..10000));
let u = U256::from(rng.gen_range(8000..9000));
let available_borrow = exp * available_borrow_base * u / borrow_asset_price;

if available_borrow > U256::ZERO {
Expand Down
11 changes: 11 additions & 0 deletions src/aave/agents/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ mod borrow_agent;
mod liquidation_agent;
mod uniswap_agent;
mod uniswap_noise_agent;

use super::types::UserData;
use alloy_primitives::U256;
pub use borrow_agent::BorrowAgent;
pub use liquidation_agent::LiquidationAgent;
use rand::RngCore;
use serde::{Deserialize, Serialize};
pub use uniswap_agent::UniswapPriceAgent;
pub use uniswap_noise_agent::UniswapNoiseAgent;
use verbs_rs::agent::{AgentSet, AgentVec, SimState, SingletonAgent};
Expand All @@ -21,3 +23,12 @@ pub struct AgentStates {
pub uniswap_price_agent: SingletonAgent<(i128, i128), UniswapPriceAgent>,
pub uniswap_noise_agents: AgentVec<U256, UniswapNoiseAgent>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct SimData {
pub seed: u64,
pub borrow_agents: Vec<Vec<U256>>,
pub liquidation_agents: Vec<Vec<UserData>>,
pub uniswap_price_agent: Vec<(i128, i128)>,
pub uniswap_noise_agents: Vec<Vec<U256>>,
}
21 changes: 18 additions & 3 deletions src/aave/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ mod initialisation;
mod protocol;
pub mod types;

pub use agents::SimData;
use verbs_rs::env::GasPriorityValidator;
use verbs_rs::sim_runner;
use verbs_rs::{agent::RecordedAgentSet, sim_runner};

use self::initialisation::initialise_sim;

Expand All @@ -16,7 +17,7 @@ pub fn aave_sim_from_fork(
n_steps: usize,
params: types::ForkedSimParameters,
alchemy_key: String,
) {
) -> SimData {
println!("Initialising Simulation");

let validator = GasPriorityValidator {};
Expand All @@ -27,11 +28,25 @@ pub fn aave_sim_from_fork(
println!("Running");

sim_runner::run(&mut env, &mut agent_sets, seed, n_steps);
SimData {
seed,
borrow_agents: agent_sets.borrow_agents.take_records(),
liquidation_agents: agent_sets.liquidation_agents.take_records(),
uniswap_price_agent: agent_sets.uniswap_price_agent.take_records(),
uniswap_noise_agents: agent_sets.uniswap_noise_agents.take_records(),
}
}

pub fn aave_sim(seed: u64, n_steps: usize, params: types::SimParameters) {
pub fn aave_sim(seed: u64, n_steps: usize, params: types::SimParameters) -> SimData {
let validator = GasPriorityValidator {};
let (mut env, mut agent_sets, _, _, _) = initialise_sim(params, validator);

sim_runner::run(&mut env, &mut agent_sets, seed, n_steps);
SimData {
seed,
borrow_agents: agent_sets.borrow_agents.take_records(),
liquidation_agents: agent_sets.liquidation_agents.take_records(),
uniswap_price_agent: agent_sets.uniswap_price_agent.take_records(),
uniswap_noise_agents: agent_sets.uniswap_noise_agents.take_records(),
}
}
Loading