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

[1]:
from defipy import *
[2]:
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
[3]:
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)

πŸ“˜ 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.

      • Output: Liquidity added to the pool.

[4]:
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

  • 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 of token_in to swap.

      • Output: Executes the swap from token_in to token_out for the user

[5]:
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}")
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
[6]:
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

πŸ“˜ 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 of token_in to add.

      • Output: Adds the specified token amounts to the pool and mints liquidity tokens to the user.

[7]:
usdt_before = lp.get_reserve(usdt)

amt_tkn_in = 10000
tkn_in = usdt
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 = 79556308.6645169, USDC = 81365068.187939, USDT = 55663253.910191
Liquidity: 216583028.83723688

10000 USDT was deposited for 10000.919116999057 LP tokens
[8]:
usdt_before = lp.get_reserve(usdt)
amt_tkn_in = 10000
tkn_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

  • 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.

      • Output: Removes the specified token amounts from the pool and burns liquidity tokens from the user.

[9]:
amt_lp_out = 250000
tkn_out = dai
dai_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
[10]:
amt_lp_out = 500000
tkn_out = usdt
usdt_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