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

Feat/simple #413

Merged
merged 11 commits into from
Aug 15, 2023
Merged
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ path = "bin/main.rs"

# Dependencies for the release build
[dependencies]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused dependency

# Local dependencies
arbiter-core = { path = "arbiter-core" }

# Command line and config
clap = { version = "4.3.0", features = ["derive"] }
serde = { version = "1.0.163", features =["derive"] }
Expand Down
40 changes: 3 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
[![](https://dcbadge.vercel.app/api/server/primitive?style=flat)](https://discord.gg/primitive)
[![Twitter Badge](https://badgen.net/badge/icon/twitter?icon=twitter&label)](https://twitter.com/primitivefi)


Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sure we can improve on this in the future, but seemed like a step in the right direction. We should probably review more thoroughly before merging.

This library enables user to communicate with a sandboxed revm instance via the implementation of the [ethers-rs](ethers.rs) middleware.

The Ethereum blockchain's execution environment, the Ethereum Virtual machine (EVM), contains a rich collection of decentralized applications. The EVM is stack machine that sequentially executes opcodes sent to it by users and smart contracts. Arbiter is a highly configurable rust interface over [revm](https://github.com/bluealloy/revm) which is a Rust implementation of the EVM stack machine logic. The purpose of Arbiter is to interface with arbitrary agents and contracts and run this all directly on a blazing-fast simulated EVM.

Financial engineers need to study a wide array of complex portfolio management strategies against thousands of market conditions, contract parameters, and agents. To configure such a rich simulation environment on a test network could be possible, but a more efficient choice for getting the most robust, yet quick, simulations would bypass any local networking and use a low level language's implementation of the EVM.
Expand All @@ -20,32 +23,6 @@ Arbiter can be used for:
- investigating risk, capital efficiency, rebalancing strategies, and portfolio replication (or performance). (LPs, funds, quants, traders)
- Engineering and testing new financial products built on top of more primitive financial products (DeFi firms and academics)

## Features:

For our next beta release, we will be focusing on the following features:


## Build From Source

First, clone the repository to your local environment so

```bash
git clone https://github.com/primitivefinance/arbiter.git
cd arbiter
```

Install arbiter on your system:

```bash
cargo install --path . --force
```

With the `arbiter` binary generated, you can run commands such as:

```bash
arbiter simulate uniswap
```

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all no longer relevant / incorrect with the new release so i removed it.

## Generating Docs

To see the documentation for Arbiter, after cloning the repo, you can run:
Expand All @@ -56,17 +33,6 @@ cargo doc --workspace --no-deps --open

This will generate and open the docs in your browser. From there, you can look at the documentation for each crate in the Arbiter workspace.

## Including More Contracts

In the `contracts/` directory you can add additional smart contracts or regenerate Rust bindings. Once that is done, you will want to make sure the bindings are generated in the script:

```bash
./bind.sh
```
You will need to add the relevant directory for your new contracts to the script above and make sure they are also handled by `forge install`. We look forward to improving upon this UX in the future.

At the moment, this only builds the bindings for the contracts in the `lib/arbmod/contracts/` and `lib/portfolio/contracts`. You can of course add an additional directory of contracts in `lib/`. Just be sure to include it when you generate bindings!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also not relevant with the new release

## Contributing

See our [Contributing Guidelines](https://github.com/primitivefinance/arbiter/blob/main/.github/CONTRIBUTING.md)
9 changes: 0 additions & 9 deletions arbiter-core/README.md

This file was deleted.

118 changes: 0 additions & 118 deletions arbiter-core/src/agent.rs

This file was deleted.

12 changes: 1 addition & 11 deletions arbiter-core/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ use revm::{
};
use thiserror::Error;

use crate::{
agent::{Agent, IsAttached, NotAttached},
math::*,
middleware::RevmMiddleware,
};
use crate::{math::*, middleware::RevmMiddleware};

pub(crate) type ToTransact = bool;
pub(crate) type ResultSender = Sender<RevmResult>;
Expand All @@ -39,7 +35,6 @@ pub struct Environment {
pub(crate) state: Arc<AtomicState>,
pub(crate) evm: EVM<CacheDB<EmptyDB>>,
pub(crate) socket: Socket,
pub agents: Vec<Agent<IsAttached<RevmMiddleware>>>,
pub seeded_poisson: SeededPoisson,
pub(crate) handle: Option<JoinHandle<Result<(), EnvironmentError>>>,
pub(crate) pausevar: Arc<(Mutex<()>, Condvar)>,
Expand Down Expand Up @@ -82,17 +77,12 @@ impl Environment {
state: Arc::new(AtomicState::new(State::Initialization)),
evm,
socket,
agents: vec![],
seeded_poisson,
handle: None,
pausevar: Arc::new((Mutex::new(()), Condvar::new())),
}
}

pub fn add_agent(&mut self, agent: Agent<NotAttached>) {
agent.attach_to_environment(self);
}

pub(crate) fn run(&mut self) {
let mut evm = self.evm.clone();
let tx_receiver = self.socket.tx_receiver.clone();
Expand Down
1 change: 0 additions & 1 deletion arbiter-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![warn(missing_docs, unsafe_code)]

pub mod agent;
pub mod bindings; // TODO: Add better documentation here and some kind of overwrite protection.
pub mod environment;
pub mod manager;
Expand Down
21 changes: 1 addition & 20 deletions arbiter-core/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use std::collections::HashMap;
use log::{info, warn};
use thiserror::Error;

use crate::{
agent::{Agent, NotAttached},
environment::{Environment, State},
};
use crate::environment::{Environment, State};

#[derive(Default)]
pub struct Manager {
Expand Down Expand Up @@ -174,22 +171,6 @@ impl Manager {
}),
}
}

pub fn add_agent(
&mut self,
agent: Agent<NotAttached>,
environment_label: String,
) -> Result<(), ManagerError> {
match self.environments.get_mut(&environment_label) {
Some(environment) => {
environment.add_agent(agent);
Ok(())
}
None => Err(ManagerError::EnvironmentDoesNotExist {
label: environment_label,
}),
}
}
}

#[cfg(test)]
Expand Down
28 changes: 16 additions & 12 deletions arbiter-core/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
};

use ethers::{
core::rand::{thread_rng, SeedableRng},
prelude::{
k256::{
ecdsa::SigningKey,
Expand All @@ -28,15 +29,12 @@ use ethers::{
Log,
},
};
use rand::{rngs::StdRng, SeedableRng};
use rand::rngs;
use revm::primitives::{CreateScheme, ExecutionResult, Output, TransactTo, TxEnv, B160, U256};
use serde::{de::DeserializeOwned, Serialize};
use thiserror::Error;

use crate::{
agent::{Agent, NotAttached},
environment::{Environment, EventBroadcaster, ResultReceiver, ResultSender, TxSender},
};
use crate::environment::{Environment, EventBroadcaster, ResultReceiver, ResultSender, TxSender};

#[derive(Debug)]
pub struct RevmMiddleware {
Expand Down Expand Up @@ -84,7 +82,7 @@ impl MiddlewareError for RevmMiddlewareError {
}

impl RevmMiddleware {
pub fn new(agent: &Agent<NotAttached>, environment: &Environment) -> Self {
pub fn new(environment: &Environment, seed_and_label: Option<String>) -> Self {
let tx_sender = environment.socket.tx_sender.clone();
let (result_sender, result_receiver) = crossbeam_channel::unbounded();
let connection = Connection {
Expand All @@ -95,12 +93,18 @@ impl RevmMiddleware {
filter_receivers: Arc::new(tokio::sync::Mutex::new(HashMap::new())),
};
let provider = Provider::new(connection);
let mut hasher = Sha256::new();
hasher.update(agent.name.as_bytes());
let seed = hasher.finalize();
let mut rng = StdRng::from_seed(seed.into());
let wallet = Wallet::new(&mut rng);
Self { provider, wallet }
if let Some(seed) = seed_and_label {
let mut hasher = Sha256::new();
hasher.update(seed.clone());
let hashed = hasher.finalize();
let mut rng: rngs::StdRng = SeedableRng::from_seed(hashed.into());
let wallet = Wallet::new(&mut rng);
Self { provider, wallet }
} else {
let mut rng = thread_rng();
let wallet = Wallet::new(&mut rng);
Self { provider, wallet }
}
}
}

Expand Down
Loading
Loading