Abstract Stableswap Test
Abstract Interface
Section titled βAbstract Interfaceβ- π Join: joins X and Y amounts to pool
- π Swap: swaps X for Y (and vice verse) via out-given-in or in-given-out
- π AddLiquidity: adds liquidity using token or share amounts
- π RemoveLiquidity: removes liquidity using token or share amounts
To download notebook to this tutorial, see here
from defipy import *user_nm = 'user_test'
AMPL_COEFF = 2000
amt_dai = 79566307.559825807715868071decimal_dai = 18
amt_usdc = 81345068.187939decimal_usdc = 6
amt_usdt = 55663250.772939decimal_usdt = 6dai = 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)π Join
Section titled βπ Joinβ- Class: π
defipy.process.Join- Purpose: Simplifies initial liquidity addition to Stableswap pools.
- Methods:
apply(pool, user: str, ampl_coeff: float)- Parameters:
pool: Pool instance (e.g., created via Primitive Interface).user: User address.ampl_coeff: Amplification coefficient.
- Parameters:
- Output: Liquidity added to the pool.
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()Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79566307.55982581, USDC = 81345068.187939, USDT = 55663250.772939
Liquidity: 216573027.91811988
π Swap
Section titled βπ Swapβ- Class: π
defipy.process.Swap- Purpose: Facilitates token swaps on Stableswap pools.
- Methods:
apply(pool, token_in: ERC20, token_out: ERC20, user: str, amount_tkn_in: float)- Parameters:
pool: Pool instance to perform the swap on.token_in: ERC20 token to swap in.token_out: ERC20 token to swap out.user: User address (string) executing the swap.amount_tkn_in: Amount oftoken_into swap.
- Parameters:
- Output: Executes the swap from
token_intotoken_outfor the user
usdc_before = lp.get_reserve(usdc)usdt_before = lp.get_reserve(usdt)
amt_tkn_in = 10000tkn_in = usdctkn_out = usdtres = 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 = 79566307.55982581, USDC = 81355068.187939, USDT = 55653253.910191
Liquidity: 216573027.91811988
10000 USDC was swapped for 9996.862748 USDT
usdc_before = lp.get_reserve(usdc)dai_before = lp.get_reserve(dai)
amt_tkn_in = 10000tkn_in = usdctkn_out = daires = 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
π AddLiquidity
Section titled βπ AddLiquidityβ- Class: π
defipy.process.AddLiquidity- Purpose: Adds liquidity to existing Stableswap pools, handling token amounts and liquidity tokens minting.
- Methods:
apply(pool, token_in: ERC20, user: str, amount_in: float)- Parameters:
pool: Pool instance to perform the token addition.token_in: ERC20 token to add.user: User address (string) providing liquidity.amount_in: Amount oftoken_into add.
- Parameters:
- Output: Adds the specified token amounts to the pool and mints liquidity tokens to the user.
usdt_before = lp.get_reserve(usdt)
amt_tkn_in = 10000tkn_in = usdtres = AddLiquidity().apply(lp, tkn_in, user_nm, amt_tkn_in)lp.summary()
print(f"{amt_tkn_in} {tkn_in.token_name} was deposited for {res['liquidity_amt_in']} LP tokens")Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79556308.6645169, USDC = 81365068.187939, USDT = 55663253.910191
Liquidity: 216583028.83723688
10000 USDT was deposited for 10000.919116999057 LP tokens
usdt_before = lp.get_reserve(usdt)amt_tkn_in = 10000tkn_in = dai
res = AddLiquidity().apply(lp, tkn_in, user_nm, amt_tkn_in)lp.summary()
print(f"{amt_tkn_in} {tkn_in.token_name} was deposited for {res['liquidity_amt_in']} LP tokens")Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79566308.6645169, USDC = 81365068.187939, USDT = 55663253.910191
Liquidity: 216593027.8056816
10000 DAI was deposited for 9998.968444705135 LP tokens
π RemoveLiquidity
Section titled βπ RemoveLiquidityβ- Class: π
defipy.process.RemoveLiquidity- Purpose: Removes liquidity from existing Stableswap pools, handling token amounts and liquidity tokens burns.
- Methods:
apply(pool, token_out: ERC20, user: str, amt_lp_out: float)- Parameters:
pool: Pool instance to perform the token removal.token_out: ERC20 token to remove.user: User address (string) providing liquidity.amt_lp_out: Amount of liquidity to remove.
- Parameters:
- Output: Removes the specified token amounts from the pool and burns liquidity tokens from the user.
amt_lp_out = 250000tkn_out = daidai_before = lp.get_reserve(dai)lp_amt_before = lp.total_supply
res = RemoveLiquidity().apply(lp, tkn_out, user_nm, amt_lp_out)lp.summary()
print(f"{amt_lp_out} LP tokens as removed for {res['tkn_out_amt']} {tkn_out.token_name}")Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79316306.72151607, USDC = 81365068.187939, USDT = 55663253.910191
Liquidity: 216343027.8056816
250000 LP tokens as removed for 250001.94300082736 DAI
amt_lp_out = 500000tkn_out = usdtusdt_before = lp.get_reserve(usdt)lp_amt_before = lp.total_supply
res = RemoveLiquidity().apply(lp, tkn_out, user_nm, amt_lp_out)lp.summary()
print(f"{amt_lp_out} LP tokens as removed for {res['tkn_out_amt']} {tkn_out.token_name}")Stableswap Exchange: DAI-USDC-USDT (LP)
Reserves: DAI = 79316306.72151607, USDC = 81365068.187939, USDT = 55163356.268067
Liquidity: 215843027.8056816
500000 LP tokens as removed for 499897.642124 USDT