StableSwap Analytics with Python
Project description
stableswappy
Python package for Stableswap V1 modelling
🔗 SPDX-Anchor: anchorregistry.ai/AR-2026-PZYDyE1
Install
To install package:
> git clone https://github.com/defipy-devs/stableswappy
> pip install .
or
> pip install StableswapPy
Stableswap
- See test notebook for basic usage
from stableswappy import *
user_nm = 'user_test'
user_nm = 'user_test'
AMPL_COEFF = 2000
amt_dai = 79566307.559825807715868071
decimal_dai = 18
amt_usdc = 81345068.187939
decimal_usdc = 6
amt_usdt = 55663250.772939
decimal_usdt = 6
dai = ERC20("DAI", "0xA0b", decimal_dai)
dai.deposit(None, amt_dai)
usdc = ERC20("USDC", "0xf93", decimal_usdc)
usdc.deposit(None, amt_usdc)
usdt = ERC20("USDT", "0xd7c", decimal_usdt)
usdt.deposit(None, amt_usdt)
sgrp = StableswapVault()
sgrp.add_token(dai)
sgrp.add_token(usdc)
sgrp.add_token(usdt)
sfactory = StableswapFactory("Pool factory", "0x2")
exchg_data = StableswapExchangeData(vault = sgrp, symbol="LP", address="0x011")
lp = sfactory.deploy(exchg_data)
Join().apply(lp, user_nm, AMPL_COEFF)
lp.summary()
OUTPUT:
Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79566307.55982581, USDC = 81345068.187939, USDT = 55663250.772939
Liquidity: 216573027.91811988
Swap
usdc_before = lp.get_reserve(usdc)
usdt_before = lp.get_reserve(usdt)
amt_tkn_in = 10000
tkn_in = usdc
tkn_out = usdt
res = Swap().apply(lp, tkn_in, tkn_out, user_nm, amt_tkn_in)
lp.summary()
print(f"{amt_tkn_in} {tkn_in.token_name} was swapped for {res['tkn_out_amt']} {tkn_out.token_name}")
OUTPUT:
Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79566307.55982581, USDC = 81355068.187939, USDT = 55653253.910191
Liquidity: 216573027.91811988
10000 USDC was swapped for 9996.862748 USDT
Swap
usdc_before = lp.get_reserve(usdc)
dai_before = lp.get_reserve(dai)
amt_tkn_in = 10000
tkn_in = usdc
tkn_out = dai
res = Swap().apply(lp, tkn_in, tkn_out, user_nm, amt_tkn_in)
lp.summary()
print(f"{amt_tkn_in} {tkn_in.token_name} was swapped for {res['tkn_out_amt']} {tkn_out.token_name}")
Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79556308.6645169, USDC = 81365068.187939, USDT = 55653253.910191
Liquidity: 216573027.91811988
10000 USDC was swapped for 9998.895308918858 DAI
Testing
Run the full test suite from the repo root:
> python -m pytest python/test/ -v
Tests cover:
- Process: join, swap (near-peg slippage, fee verification, reserve changes), add/remove liquidity
- Math: D invariant stability, exchange output, virtual price, spot price (
dydx), andcalc_token_amount
Requirements
> pip install pytest gmpy2
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file stableswappy-1.1.0.tar.gz.
File metadata
- Download URL: stableswappy-1.1.0.tar.gz
- Upload date:
- Size: 26.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4735ae2ce5fe4863a973182428e152bc75f0fb0481f59e351300443ae153de2
|
|
| MD5 |
e0180a1556f82d4c16b16aaa156462dd
|
|
| BLAKE2b-256 |
fea669907c839611ae6beff23e1bbba1f91a27b6974fa1fe62aa98c16ea8c0a0
|
File details
Details for the file stableswappy-1.1.0-py3-none-any.whl.
File metadata
- Download URL: stableswappy-1.1.0-py3-none-any.whl
- Upload date:
- Size: 38.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
25047fb5deefa1384f2ecd686fccb9cb26db68e3e5bb25fe2789207cc49b4167
|
|
| MD5 |
7e4bf2f3890fca580a46677c17c23d5d
|
|
| BLAKE2b-256 |
6a96c57fb7db7327988ec8037a03f1f1264ef167da661ce87da7810bb09b040f
|