Skip to content

Commit

Permalink
Merge pull request #197 from primitivefinance/colin/remove-admin
Browse files Browse the repository at this point in the history
removed admin submodule
  • Loading branch information
0xJepsen authored Apr 12, 2023
2 parents 425e975 + 81e1c68 commit 342fef0
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 63 deletions.
2 changes: 1 addition & 1 deletion crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ async fn main() -> Result<()> {
let user_name = "arbitrageur";
let user_address =
B160::from_str("0x0000000000000000000000000000000000000002").unwrap();
manager.create_user(user_address, user_name);
manager.create_user(user_address, user_name).unwrap();

println!("Arbitraguer created at: {}", user_address);

Expand Down
50 changes: 0 additions & 50 deletions crates/simulate/src/agent/admin.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/simulate/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use revm::primitives::{Address, ExecutionResult, Log, Output, TransactTo, TxEnv,

use crate::environment::{IsDeployed, NotDeployed, SimulationContract, SimulationEnvironment};

pub mod admin;
pub mod user;
/// Describes the gas settings for a transaction.
pub struct TransactSettings {
Expand Down
2 changes: 1 addition & 1 deletion crates/simulate/src/agent/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl User {
address,
account: Account::from(AccountInfo::default()),
transact_settings: TransactSettings {
gas_limit: u64::MAX,
gas_limit: u64::MAX, // TODO: Users should have a gas limit.
gas_price: U256::ZERO, // TODO: Users should have an associated gas price.
},
event_receiver,
Expand Down
4 changes: 2 additions & 2 deletions crates/simulate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ mod tests {
// Create a user to mint tokens to.
let user_name = "alice";
let user_address = B160::from_str("0x0000000000000000000000000000000000000002").unwrap();
manager.create_user(user_address, user_name); // TODO: This should probably be done by the manager itself. THough it will be something to consider when we have more agents.
manager.create_user(user_address, user_name).unwrap(); // TODO: This should probably be done by the manager itself. THough it will be something to consider when we have more agents.

// Allocating new tokens to user by calling Arbiter Token's ERC20 'mint' instance.
let mint_amount = U256::from(1000);
Expand Down Expand Up @@ -254,7 +254,7 @@ mod tests {
let mut manager = SimulationManager::default();
let user_name = "alice";
let user_address = B160::from_str("0x0000000000000000000000000000000000000002").unwrap();
manager.create_user(user_address, user_name);
manager.create_user(user_address, user_name).unwrap();

// Get bytecode and abi for the writer contract.
let writer = SimulationContract::new(
Expand Down
34 changes: 26 additions & 8 deletions crates/simulate/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crossbeam_channel::unbounded;
use revm::primitives::{AccountInfo, ExecutionResult, Log, Output, B160};

use crate::{
agent::{admin::Admin, user::User, Agent},
agent::{user::User, Agent},
environment::SimulationEnvironment,
};

Expand All @@ -31,15 +31,16 @@ impl<'a> Default for SimulationManager<'a> {
}

impl<'a> SimulationManager<'a> {
/// Constructor function to instantiate a
/// Constructor function to instantiate a manager that has a default admin user and a simulation environment.
/// The admin will always be given the 0x0...1 address.
pub fn new() -> Self {
let (event_sender_admin, event_receiver_admin) = unbounded::<Vec<Log>>();
let mut simulation_manager = Self {
environment: SimulationEnvironment::new(),
agents: HashMap::new(),
};
let admin = Box::new(Admin::new(event_receiver_admin));
simulation_manager.add_agent("admin", admin);
let admin = Box::new(User::new(event_receiver_admin, B160::from_low_u64_be(1)));
simulation_manager.add_agent("admin", admin).unwrap();
simulation_manager
.environment
.add_sender(event_sender_admin);
Expand All @@ -52,22 +53,32 @@ impl<'a> SimulationManager<'a> {
}

/// Add an [`Agent`] to the current simulation.
pub fn add_agent(&mut self, name: &'a str, agent: Box<dyn Agent>) {
pub fn add_agent(&mut self, name: &'a str, agent: Box<dyn Agent>) -> Result<(), String> {
if self
.agents
.values()
.into_iter()
.any(|agent_in_db| agent_in_db.address() == agent.address())
{
return Err("Agent already exists in the simulation environment.".to_string());
};
self.agents.insert(name, agent);
Ok(())
}

// TODO: maybe should make the name optional here, but I struggled with this.
/// Allow the manager to create a dummy user account.
pub fn create_user(&mut self, address: B160, name: &'a str) {
pub fn create_user(&mut self, address: B160, name: &'a str) -> Result<(), String> {
self.environment
.evm
.db()
.unwrap()
.insert_account_info(address, AccountInfo::default());
let (event_sender_user, event_receiver_user) = unbounded::<Vec<Log>>();
let user = Box::new(User::new(event_receiver_user, address));
self.add_agent(name, user);
self.environment.add_sender(event_sender_user)
self.add_agent(name, user)?;
self.environment.add_sender(event_sender_user);
Ok(())
}

/// Takes an `ExecutionResult` and returns the raw bytes of the output that can then be decoded.
Expand Down Expand Up @@ -95,3 +106,10 @@ impl<'a> SimulationManager<'a> {
}
}
}

#[test]
fn test_agent_address_collision() {
let mut manager = SimulationManager::default();
let result = manager.create_user(B160::from_low_u64_be(1), "alice");
assert!(result.is_err());
}

0 comments on commit 342fef0

Please # to comment.