Balancer: the Python edition
The same Balancer, but coded in Python
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:
balancer_math.py - the file containing math for operations in Balancer AMM - different types of operation related to swaps, join and exit the pool
ActionDecoder - the class that reads the pool's actions and triggers pool mechanisms
system_policies.py - Balancer mechanisms as cadCAD policies
partial_state_update_blocks.py - method that generate state updates and gets simulation steps
Spot Price calculation - about the state update logic for "in pool's" token prices, the price relation between all token pairs.
balancer_constants.py - the file with main constants of Balancer AMM
Basic state update functions are omitted in the docs. If you need more info, check the repo
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, check verification & limitations page.
Last updated