Balancer Simulations
  • Getting Started
  • Understanding Balancer AMMs
    • Balancer Basics
    • Use cases
  • Simulation Packages
    • Model Overview
    • Pool Functions
    • Pool Exploration
  • Code & Instructions
    • Dynamic weights changing
    • Arbitrage agent
    • Balancer: the Python edition
      • ActionDecoder
      • pool_state_updates.py
      • partial_state_update_blocks.py
      • Spot Price calculation
      • balancer_constants.py
      • balancer_math.py
      • balancer_pool.py
      • Verification & Limitations
    • Naming Convention
    • Adding On-chain Transaction Data and USD Prices
    • Modeling Time
Powered by GitBook
On this page

Was this helpful?

  1. Code & Instructions

Balancer: the Python edition

The same Balancer, but coded in Python

PreviousArbitrage agentNextActionDecoder

Last updated 4 years ago

Was this helpful?

The model has been built emulating the operations of Balancer's BPool smart contracts. We made an initial 1:1 port to python to test and match we could get similar results than Solidity, and the split the logic (into policies) and state (state variables and state update functions) in order to create a cadCAD compatible model.

This is an initial iteration built to reproduce and test the model's behaviour following existing pools, so there is no agents or behaviours (yet!), but it's modular enough so they could be added later.

The Python edition of Balancer contains several modules:

  • - the file containing math for operations in Balancer AMM - different types of operation related to swaps, join and exit the pool

  • - the class that reads the pool's actions and triggers pool mechanisms

  • - Balancer mechanisms as cadCAD policies

  • - method that generate state updates and gets simulation steps

  • - about the state update logic for "in pool's" token prices, the price relation between all token pairs.

  • - the file with main constants of Balancer AMM

Basic state update functions are omitted in the docs. If you need more info,

The initial BPool port is kept as a reference:

  • balancer_pool.py - the core of Balancer Pool, in a python class.

Some simplifications and omissions were made, like no token binding logic, no need for security limits in the trade (max price, min token amount in...) since in cadCAD we don't have delayed transactions or reordering. To know more about this, .

balancer_math.py
ActionDecoder
system_policies.py
partial_state_update_blocks.py
Spot Price calculation
balancer_constants.py
check the repo
check verification & limitations page