{"id":21221811,"url":"https://github.com/primitivefinance/portfolio_simulations","last_synced_at":"2025-07-10T13:33:02.041Z","repository":{"id":189035461,"uuid":"679915001","full_name":"primitivefinance/portfolio_simulations","owner":"primitivefinance","description":"Moving to the new Arbiter framework to test Portfolio.","archived":false,"fork":false,"pushed_at":"2023-10-01T12:40:45.000Z","size":720,"stargazers_count":10,"open_issues_count":3,"forks_count":0,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-05T14:02:56.044Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/primitivefinance.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-17T23:11:08.000Z","updated_at":"2024-07-09T09:48:13.000Z","dependencies_parsed_at":"2023-08-18T00:55:14.160Z","dependency_job_id":"cc8f28e4-d256-4386-aa36-73fb798d2a71","html_url":"https://github.com/primitivefinance/portfolio_simulations","commit_stats":null,"previous_names":["primitivefinance/portfolio_simulations"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/primitivefinance/portfolio_simulations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fportfolio_simulations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fportfolio_simulations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fportfolio_simulations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fportfolio_simulations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/primitivefinance","download_url":"https://codeload.github.com/primitivefinance/portfolio_simulations/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/primitivefinance%2Fportfolio_simulations/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264585371,"owners_count":23632646,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-20T22:33:57.080Z","updated_at":"2025-07-10T13:32:59.420Z","avatar_url":"https://github.com/primitivefinance.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stable Pool Simulation\n\nThe simulation in this repository is intended to demonstrate a basic simulation created with the [Arbiter](http://https://github.com/primitivefinance/arbiter) framework.\nTo do so, we have initialized a [Portfolio](https://github.com/primitivefinance/portfolio) pool with a single Liquidity Provider (LP) that has parameters chosen to be useful for a pair of two stable tokens.\nWe run a secondary market (`LiquidExchange`) with immense liquidity that changes price based on a stochastic price process.\nA 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. \n\n## Idea\n\n### Smart Contract Testing\n\nWe first want to check and make sure that the `Portfolio` and `NormalStrategy` smart contracts are working as expected.\nNote 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.\nThat is, bugs that may not cause a smart contract to fail, but may cause it to behave in an unexpected way.\nFor example, [Portfolio Issue #436](https://github.com/primitivefinance/portfolio/issues/436) was found as we built and ran this simulation.\n\n### Portfolio Pool Performance\n\nSuppose 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.\nFurthermore, suppose that we want to see how this pool performs over various durations and market conditions.\nBy 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.\n\n## Usage\n\nTo run the simulation, all you need to do is run the following:\n```bash\ngit clone https://github.com/primitivefinance/portfolio_simulations.git\ncd portfolio_simulations\ncargo run\n```\nIf all is working properly, then the simulation will take place.\nBy 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:\n```\n[INFO  arbiter_core::manager] Added environment labeled portfolio\n[INFO  portfolio_simulation::startup] Admin client with address 0xcfaaf7059d10aee23d6e37455df0756603472ca9\n[INFO  arbiter_core::manager] Started environment labeled portfolio\n[INFO  portfolio_simulation::startup] Arbitrageur client with address 0x1f1629cbbdf810ae3ceaa6d38a41debe88aff0a7\n[INFO  portfolio_simulation::startup] WETH contract deployed at 0xff9b0352145603223666c0d1a117925a913d3974\n[INFO  portfolio_simulation::startup] Arbiter Token X contract deployed at 0x4a5939f004c93cdddea56677a735cecddb221a91\n[INFO  portfolio_simulation::startup] Arbiter Token Y contract deployed at 0x8c9b3777b74b59cbbc8baf80b1ad5db8f284fd50\n[INFO  portfolio_simulation::startup] liquid exchange contract deployed at 0x3307bc801ef829893b0a5a24c8d5a08127e07985\n[INFO  portfolio_simulation::startup] Portfolio contract deployed at 0x80676843bbf0827d7fbf8ab0cb2405a0d3d1cfee\n[INFO  portfolio_simulation::startup] normal strategy contract deployed at 0x56575e921da694e6b5fe3cafba407ec5c9273059\n[INFO  portfolio_simulation::startup] Created a pair with pair_id: 1\n[INFO  portfolio_simulation::startup] Created a pool with `pool_id`: 4294967297\n[INFO  portfolio_simulation::startup] Allocated reserves: (4980053002098901240000, 4980053002098894690000)\n[INFO  portfolio_simulation] \n        Step 0\n[INFO  portfolio_simulation::strategies] Updating price of liquid_exchange to: 1.0071773385503369\n[INFO  portfolio_simulation::strategies] Arbitrageur sees prices:\n        Liquid Exchange: 1007177338550336896\n        Portfolio: 999999999999999984\n[INFO  portfolio_simulation::strategies] Upper bound: 1001001001001000984\n[INFO  portfolio_simulation::strategies] Lower bound: 998999999999999984\n[INFO  portfolio_simulation::strategies] Swapping ARBY for ARBX on Portfolio\n[INFO  portfolio_simulation::strategies] Raw reserves: 4980053002098901240000, 4980053002098894690000\n[INFO  portfolio_simulation::strategies] Liquidity: 10000000000000000000000\n[INFO  portfolio_simulation::strategies] Virtual reserve y: 498005300209889469\n[INFO  portfolio_simulation::strategies] S/K: 1007177338550336896\n[INFO  portfolio_simulation::strategies] Inside term: 710170404128665100\n[INFO  portfolio_simulation::strategies] Target virtual reserve: 761200778920208468\n[INFO  portfolio_simulation::strategies] Virtual input: 263195478710318999\n[INFO  portfolio_simulation::strategies] Input ARBY: 2634589376479669659657\n[INFO  portfolio_simulation::strategies] Output ARBX: 2622952719975841770000\n[INFO  portfolio_simulation::strategies] Order: Order { input: 2634589376479669659657, output: 2622952719975841770000, use_max: false, pool_id: 4294967297, sell_asset: false }\n[INFO  portfolio_simulation::strategies] Portfolio swap successful\n[INFO  portfolio_simulation::strategies] Swapping ARBX for ARBY on Liquid Exchange\n[INFO  portfolio_simulation::strategies] LiquidExchange swap successfull; arbitrage successful\n[INFO  portfolio_simulation] Portfolio price after swap is: 1007177338550336861\n[INFO  portfolio_simulation] Reserves after swap are: (2357100282123059470000, 7614642378578564349657)\n...\n```\n\n## Documentation\n\nThe code is documented using Rustdoc.\nTo view the documentation, run the following: \n```bash\ncargo doc --no-deps --open\n```\nAlso added are inline comments to help explain the code and why specific choices were made.\n\n## Visualization\n\nUsing Python, we can visualize the results of the simulation.\nWe can set this up by `cd`ing into the `visualization` directory and running the following:\n```bash\n./py_setup.sh\n```\nThis will get the Python environment set up for you locally and install all the necessary imports.\nThen, you can run the following to generate the plots:\n```bash\npython main.py\n```\n\nCurrently 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.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimitivefinance%2Fportfolio_simulations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprimitivefinance%2Fportfolio_simulations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprimitivefinance%2Fportfolio_simulations/lists"}