Skip to main content

Balancer Analytics with Python

Project description

balancerpy

Python package for Balancer V2 modelling

🔗 SPDX-Anchor: anchorregistry.ai/AR-2026-6pDkj46

Install

To install package:

> git clone https://github.com/defipy-devs/balancerpy
> pip install .

or

> pip install BalancerPy

Balancer

from balancerpy import *

user_nm = 'user_test'

amt_dai = 10000000
denorm_wt_dai = 10

amt_eth = 67738.6361731024
denorm_wt_eth = 40

init_pool_shares = 100

dai = ERC20("DAI", "0x111")
dai.deposit(None, amt_dai)

weth = ERC20("WETH", "0x09")
weth.deposit(None, amt_eth)

bgrp = BalancerVault()
bgrp.add_token(dai, denorm_wt_dai)
bgrp.add_token(weth, denorm_wt_eth)

bfactory = BalancerFactory("WETH pool factory", "0x2")
exchg_data = BalancerExchangeData(vault = bgrp, symbol="LP", address="0x011")
lp = bfactory.deploy(exchg_data)

Join().apply(lp, user_nm, init_pool_shares)
lp.summary()

OUTPUT:

Balancer Exchange: DAI-WETH (LP)
Reserves: DAI = 10000000, WETH = 67738.6361731024
Weights: DAI = 0.2, WETH = 0.8
Pool Shares: 100

Swap (out-given-in)

amt_tkn_in = 10000
tkn_in = dai
tkn_out = weth

res = Swap(Proc.SWAPOUT).apply(lp, tkn_in, tkn_out, user_nm, amt_tkn_in)
lp.summary()

print(f"{amt_tkn_in} {tkn_in.token_name} was swapped into {res['tkn_out_amt']} {tkn_out.token_name}")

OUTPUT:

Balancer Exchange: DAI-WETH (LP)
Reserves: DAI = 10010000, WETH = 67721.75437414162
Weights: DAI = 0.2, WETH = 0.8
Pool Shares: 100

10000 DAI was swapped into 16.881798960778035 WETH

Swap (out-given-in)

amt_tkn_out = 20
tkn_out = weth
tkn_in = dai

res = Swap(Proc.SWAPIN).apply(lp, tkn_in, tkn_out, user_nm, amt_tkn_out)
lp.summary()

print(f"{amt_tkn_out} {tkn_out.token_name} was swapped into {res['tkn_in_amt']} {tkn_in.token_name}")

OUTPUT:

Balancer Exchange: DAI-WETH (LP)
Reserves: DAI = 9998136.750149786, WETH = 67741.75437414162
Weights: DAI = 0.2, WETH = 0.8
Pool Shares: 100

20 WETH was swapped into 11863.249850213939 DAI

Testing

Run the full test suite from the repo root:

> python -m pytest python/test/ -v

Tests cover:

  • Process: join, swap (exact-in/exact-out, price impact), add/remove liquidity (by token and by shares), exit pool
  • Quotes: CWP share-to-amount and amount-to-share conversions with round-trip verification
  • Math: all BalancerMath static methods (spot price, out-given-in, in-given-out, pool/single token conversions)

Requirements

> pip install pytest

License

Licensed under the Apache License, Version 2.0.
See LICENSE and NOTICE for details.
Portions of this project may include code from third-party projects under compatible open-source licenses.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

balancerpy-1.0.6.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

balancerpy-1.0.6-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

Details for the file balancerpy-1.0.6.tar.gz.

File metadata

  • Download URL: balancerpy-1.0.6.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for balancerpy-1.0.6.tar.gz
Algorithm Hash digest
SHA256 4531e45569824e9dd23216c7dfc1832144a004e9e8181148e41ee3cec4c0c499
MD5 8fb5cba9ad96e8a1cc26e7f2d8261a27
BLAKE2b-256 a90022bbe916b1e4339b35e4bfc17904ac87745e842e47477acd1e3bb7b142bc

See more details on using hashes here.

File details

Details for the file balancerpy-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: balancerpy-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for balancerpy-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 45cf1b6d5739da2f90a8ed6d495295f0a628736eb063e1d082735982b1326950
MD5 d98a40cd1042a80d56e9fbad7edcbf14
BLAKE2b-256 7e5a9c262f41c0e575d229ea4b7721947386c5d7d1101ea3f83cf5cdd72dbe79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page