Moving to the new Arbiter framework to test Portfolio.

Overview

Stable Pool Simulation

The simulation in this repository is intended to demonstrate a basic simulation created with the Arbiter framework. To do so, we have initialized a Portfolio pool with a single Liquidity Provider (LP) that has parameters chosen to be useful for a pair of two stable tokens. We run a secondary market (LiquidExchange) with immense liquidity that changes price based on a stochastic price process. A second client is initialized to behave as an arbitrageur between these two exchanges by moving the Portfolio pool price back into alignment with the secondary market.

Idea

Smart Contract Testing

We first want to check and make sure that the Portfolio and NormalStrategy smart contracts are working as expected. Note that by running a simulation using Arbiter, we gain the ability to find other potential bugs that may not be apparent from the smart contract code alone. That is, bugs that may not cause a smart contract to fail, but may cause it to behave in an unexpected way. For example, Portfolio Issue #436 was found as we built and ran this simulation.

Portfolio Pool Performance

Suppose that we want to gauge the performance of a Portfolio pool with some chosen parameters and without any external flow, just the guaranteed arbitrage flow. Furthermore, suppose that we want to see how this pool performs over various durations and market conditions. By varying the parameters of the secondary market, we can simulate a variety of market conditions and by varying the Portfolio pool parameters, we can sweep over a range of configurations and optimize.

Usage

To run the simulation, all you need to do is run the following:

git clone https://github.com/primitivefinance/portfolio_simulations.git
cd portfolio_simulations
cargo run

If all is working properly, then the simulation will take place. By default, we output warn logs to the console so that you can see if there are any important events that happened during the simulation. If you instead put info or debug in the RUST_LOG environment variable, then you will see more output like the following:

[INFO  arbiter_core::manager] Added environment labeled portfolio
[INFO  portfolio_simulation::startup] Admin client with address 0xcfaaf7059d10aee23d6e37455df0756603472ca9
[INFO  arbiter_core::manager] Started environment labeled portfolio
[INFO  portfolio_simulation::startup] Arbitrageur client with address 0x1f1629cbbdf810ae3ceaa6d38a41debe88aff0a7
[INFO  portfolio_simulation::startup] WETH contract deployed at 0xff9b0352145603223666c0d1a117925a913d3974
[INFO  portfolio_simulation::startup] Arbiter Token X contract deployed at 0x4a5939f004c93cdddea56677a735cecddb221a91
[INFO  portfolio_simulation::startup] Arbiter Token Y contract deployed at 0x8c9b3777b74b59cbbc8baf80b1ad5db8f284fd50
[INFO  portfolio_simulation::startup] liquid exchange contract deployed at 0x3307bc801ef829893b0a5a24c8d5a08127e07985
[INFO  portfolio_simulation::startup] Portfolio contract deployed at 0x80676843bbf0827d7fbf8ab0cb2405a0d3d1cfee
[INFO  portfolio_simulation::startup] normal strategy contract deployed at 0x56575e921da694e6b5fe3cafba407ec5c9273059
[INFO  portfolio_simulation::startup] Created a pair with pair_id: 1
[INFO  portfolio_simulation::startup] Created a pool with `pool_id`: 4294967297
[INFO  portfolio_simulation::startup] Allocated reserves: (4980053002098901240000, 4980053002098894690000)
[INFO  portfolio_simulation] 
        Step 0
[INFO  portfolio_simulation::strategies] Updating price of liquid_exchange to: 1.0071773385503369
[INFO  portfolio_simulation::strategies] Arbitrageur sees prices:
        Liquid Exchange: 1007177338550336896
        Portfolio: 999999999999999984
[INFO  portfolio_simulation::strategies] Upper bound: 1001001001001000984
[INFO  portfolio_simulation::strategies] Lower bound: 998999999999999984
[INFO  portfolio_simulation::strategies] Swapping ARBY for ARBX on Portfolio
[INFO  portfolio_simulation::strategies] Raw reserves: 4980053002098901240000, 4980053002098894690000
[INFO  portfolio_simulation::strategies] Liquidity: 10000000000000000000000
[INFO  portfolio_simulation::strategies] Virtual reserve y: 498005300209889469
[INFO  portfolio_simulation::strategies] S/K: 1007177338550336896
[INFO  portfolio_simulation::strategies] Inside term: 710170404128665100
[INFO  portfolio_simulation::strategies] Target virtual reserve: 761200778920208468
[INFO  portfolio_simulation::strategies] Virtual input: 263195478710318999
[INFO  portfolio_simulation::strategies] Input ARBY: 2634589376479669659657
[INFO  portfolio_simulation::strategies] Output ARBX: 2622952719975841770000
[INFO  portfolio_simulation::strategies] Order: Order { input: 2634589376479669659657, output: 2622952719975841770000, use_max: false, pool_id: 4294967297, sell_asset: false }
[INFO  portfolio_simulation::strategies] Portfolio swap successful
[INFO  portfolio_simulation::strategies] Swapping ARBX for ARBY on Liquid Exchange
[INFO  portfolio_simulation::strategies] LiquidExchange swap successfull; arbitrage successful
[INFO  portfolio_simulation] Portfolio price after swap is: 1007177338550336861
[INFO  portfolio_simulation] Reserves after swap are: (2357100282123059470000, 7614642378578564349657)
...

Documentation

The code is documented using Rustdoc. To view the documentation, run the following:

cargo doc --no-deps --open

Also added are inline comments to help explain the code and why specific choices were made.

Visualization

Using Python, we can visualize the results of the simulation. We can set this up by cding into the visualization directory and running the following:

./py_setup.sh

This will get the Python environment set up for you locally and install all the necessary imports. Then, you can run the following to generate the plots:

python main.py

Currently the file path is hardcoded to ../output/portfolio.csv which is where the simulation will output the results if you cargo run from the portfolio_simulations directory.

Comments
  • set up a config.toml

    set up a config.toml

    Having config.rs was a hack for the near term. We should now set up a way to read in a toml file so we can compile the release level binary and run this all much faster (especially when we parallelize).

    opened by Autoparallel 0
  • cleanup and document the repo

    cleanup and document the repo

    Idea

    The repo is mostly well-organized at this point, but I want to go through and make sure everything is as simple as possible. Also, grab a few tasks along the way.


    Tasks:

    • [x] Arbitrageur completes the other swap leg.
    • [x] Add in depth Cargo doc documentation.
    • [x] Add in-line comments where needed.
    • [x] Add explainer in markdown format.
    opened by Autoparallel 0
  • arbitrageur and refactor

    arbitrageur and refactor

    • [x] Added an Arbitrageur into strategies.rs complete with:
      • Functioning arbitrage math and single swap.
      • Loop to verify we get a valid portfolio swap.
    • [x] Reorganized the code to be a bit more clear.
      • Added config.rs
      • Used shared dependencies where I could to make each main.rs, strategies.rs, and startup.rs cleaner.
      • Added a SimulationContracts struct once I knew the final list of contracts we would need.
    • [x] Created a run() function to split off the loop for the simulation.
    opened by Autoparallel 0
  • deploy a Portfolio pool with a single LP

    deploy a Portfolio pool with a single LP

    Get a pool up and running with an amount of liquidity added to it. This could be added by the admin client for sake of ease. Allow for params to be set via constants.

    opened by Autoparallel 0
  • feat: refactor to add in g3m simulation

    feat: refactor to add in g3m simulation

    Idea

    We will now have two strategies to test in tandem -- RMM-01 style and g3m style. I would like to do the following:

    • [ ] set up some command line arguments to allow for running specific sims
    • [ ] try to keep a stable foundation for both sims that is shared between and have minimal changes to allow for just spawning pools with different strategies.
    • [ ] General cleanup and reorganization.
    opened by Autoparallel 0
Owner
Primitive
Primitive
Integra8 rust integration test framework Rust with a focus on productivity, extensibility, and speed.

integra8 Integra8 rust integration test framework Rust with a focus on productivity, extensibility, and speed. | This repo is in a "work in progress"

exceptional 3 Sep 26, 2022
A new blockchain architecture under active development, with a strong focus on scalability, privacy and safety

Project Slingshot Accelerating trajectory into interstellar space. Slingshot is a new blockchain architecture under active development, with a strong

Stellar 378 Dec 18, 2022
A tray application for Windows that gives you push notifications and instant downloads of new posts, messages and stories posted by models you subscribe to on Onlyfans.

OF-notifier A tray application for Windows that gives you push notifications and instant downloads of new posts, messages and stories posted by models

Gentlemen Mercenary 10 Dec 20, 2022
My create new project simply (originaly in bash), in rust !

CNPS-Rust CNPS (Create new project simply) is a powerful tool built in Rust that simplifies the process of creating projects in various programming la

Asteroidus 3 Jul 30, 2023
A procedural macro to generate a new function implementation for your struct.

Impl New ?? A procedural macro to generate a new function implementation for your struct. ?? Add to your project Add this to your Cargo.toml: [depende

Mohammed Alotaibi 4 Sep 8, 2023
An RPC framework developing tutorial

Mini Lust 系列教程 好奇如何从零造出来一个 RPC 框架?本教程将带你一步一步写出来一个 Rust 版 Thrift RPC 框架。 教程说明 从第二章开始每章节都会附带代码。 这个代码是在上一章节的基础上写的,文档里一般会告诉你增加了哪些东西,但是如果你想详细地对比到底哪里变动了,可以自

null 454 Dec 30, 2022
A recommender systems framework for Rust

Quackin Release the quackin! ?? Quackin is a recommender systems framework written in Rust. This is a young project, which means two things: There wil

Christopher Vittal 8 Dec 8, 2021
Safe Rust bindings to the DynamoRIO dynamic binary instrumentation framework.

Introduction The dynamorio-rs crate provides safe Rust bindings to the DynamoRIO dynamic binary instrumentation framework, essentially allowing you to

S.J.R. van Schaik 17 Nov 21, 2022
Incremental Program Analysis Framework

IncA Overview IncA is a program analysis framework. It comes with a DSL for the definition of program analyses and the runtime system evaluates progra

Tamas Szabo 70 Nov 24, 2022
Rust bindings to the dos-like framework

dos-like for Rust   This project provides access to Mattias Gustavsson's dos-like framework, so as to write DOS-like applications in Rust. How to use

Eduardo Pinho 9 Aug 25, 2022
A developer-friendly framework for building user interfaces in Rust

Reading: "Fru" as in "fruit" and "i" as in "I" (I am). What is Frui? Frui is a developer-friendly UI framework that makes building user interfaces eas

Frui Framework 1.1k Jan 8, 2023
A backend framework for building fast and flexible APIs rapidly.

Andromeda Andromeda is a backend framework for Rust, to simplify the development of the kinds of basic API services that we developers have to build s

Framesurge 7 Dec 28, 2022
🧪 The versatile and intuitive memory hacking framework.

?? hax ?? About hax is a Rust crate designed to make memory hacking, game hacking, cheat development, and any other low level memory based development

null 16 Dec 18, 2022
A minimal boilerplate for Astro / Vite with the Nannou creative framework (Rust → WASM). Supports multiple sketches + hot-reload.

Astro x Nannou Starter astro-nannou-demo-1c.mov ?? Try it online! # 0a. Rust language tools open https://www.rust-lang.org/tools/install # 0b. wasm-p

Julian Cataldo 4 Jan 4, 2023
Grebuloff is an experimental addon framework for Final Fantasy XIV.

Grebuloff Grebuloff is an experimental addon framework for Final Fantasy XIV. It introduces a new concept of what plugins can be, focusing on enabling

Ava Chaney 5 Jun 11, 2023
Indeed, an ORM library, not a framework, written in Rust

Ormlib Indeed, an ORM library, not a framework, written in Rust Features The main idea that I put into my ORM library is a minimum of stupid code and

Evgeny Igumnov 5 Sep 4, 2023
Manas project aims to create a modular framework and ecosystem to create robust storage servers adhering to Solid protocol in rust.

मनस् | Manas Solid is a web native protocol to enable interoperable, read-write, collaborative, and decentralized web, truer to web's original vision.

Manomayam 17 Oct 5, 2023
Prototype risk modeling simulation for Portfolio using Arbiter.

proto-sim Prototype simulation using Arbiter as the simulation & agent engine. Build & Run build.sh cargo run Arbiter config The arbiter.toml config

Primitive 13 Aug 14, 2023
ABQ is a universal test runner that runs test suites in parallel. It’s the best tool for splitting test suites into parallel jobs locally or on CI

?? abq.build   ?? @rwx_research   ?? discord   ?? documentation ABQ is a universal test runner that runs test suites in parallel. It’s the best tool f

RWX 13 Apr 7, 2023
SDK for the Portfolio protocol written in rust.

portfolio-rs Minimalist toolkit for building rust applications on top of the portfolio protocol. Installation [Required] Foundry. Source. If not insta

Primitive 5 Aug 14, 2023