Abstract Interface (V3)

  • Join(): joins X and Y amounts to pool

  • Swap(): swaps X for Y (and vice verse)

  • AddLiquidity(): double-sided deposit; adds liquidity using only X or Y amounts

  • RemoveLiquidity(): double-sided withdrawal; removes liquidity using only X or Y amounts

  • SwapDeposit(): single-sided deposit; deposit exact x or y by coming to pool with just one token from trading pair to make a deposit

  • WithdrawSwap(): single-sided withdrawal; withdraw exact x or y by leaving pool with desired token from trading pair

  • LPQuote(): Quote liquidity pool, via either: (a) token price; (b) LP token amount to token amount; or (c) token amount to LP token amount

[1]:
from defipy import *
[2]:
user_nm = 'user0'
eth_amount = 1000
tkn_amount = 100000

fee = UniV3Utils.FeeAmount.MEDIUM
tick_spacing = UniV3Utils.TICK_SPACINGS[fee]
lwr_tick = UniV3Utils.getMinTick(tick_spacing)
upr_tick = UniV3Utils.getMaxTick(tick_spacing)

Join()

  • joins tkn0 and tkn1 amounts to pool

[3]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Swap()

  • tkn0 for tkn1

[4]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

Swap().apply(lp, tkn, user_nm, 1000)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 990.1284196560293, TKN = 101000.0
Gross Liquidity: 10000.0

  • tkn1 for tkn0

[5]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

Swap().apply(lp, eth, user_nm, 10)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1010.0, TKN = 99012.84196560294
Gross Liquidity: 10000.0

AddLiquidity()

  • add tkn1 and determine tkn0

[6]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

AddLiquidity().apply(lp, tkn, user_nm, 1000, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1010.0, TKN = 101000.0
Gross Liquidity: 10100.0

  • add tkn0 and determine tkn1

[7]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

AddLiquidity().apply(lp, eth, user_nm, 100, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1100.0, TKN = 110000.0
Gross Liquidity: 11000.0

RemoveLiquidity()

  • remove tkn1 and determine tkn0

[8]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

RemoveLiquidity().apply(lp, tkn, user_nm, 1000, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 990.0, TKN = 99000.0
Gross Liquidity: 9900.0

  • remove tkn0 and determine tkn1

[9]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

RemoveLiquidity().apply(lp, eth, user_nm, 10, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 990.0, TKN = 99000.0
Gross Liquidity: 9900.0

SwapDeposit()

  • swap exact tkn1 for tkn0

  • deposit desired token -> perform 50% swap -> perform 50/50 deposit

[10]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

SwapDeposit().apply(lp, tkn, user_nm, 100, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100100.00000001659
Gross Liquidity: 10004.991244978852

  • swap exact tkn0 for tkn1

  • deposit desired token -> perform 50% swap -> perform 50/50 deposit

[11]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

SwapDeposit().apply(lp, eth, user_nm, 10, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1009.9999999867499, TKN = 100000.0
Gross Liquidity: 10049.801066245287

WithdrawSwap()

  • withdraw exact tkn1

  • withdraw LP based upon expected amount of eth

  • perform 50/50 withdraw -> swap remaining 50% -> return desired token

[12]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

WithdrawSwap().apply(lp, eth, user_nm, 1, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 999.0000000000001, TKN = 100000.0
Gross Liquidity: 9994.991239989282

  • withdraw exact tkn0

  • withdraw LP based upon expected amount of tkn

[13]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()

expected_amount_out = WithdrawSwap().apply(lp, tkn, user_nm, 100,lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 99900.00000000001
Gross Liquidity: 9994.991239989282

LPQuote()

  • get LP quotes

[14]:
eth = ERC20("ETH", "0x09")
tkn = ERC20("TKN", "0x111")

exchg_data = UniswapExchangeData(tkn0 = eth, tkn1 = tkn, symbol="LP",
                                   address="0x011", version = 'V3',
                                   tick_spacing = tick_spacing,
                                   fee = fee)

factory = UniswapFactory("ETH pool factory", "0x2")
lp = factory.deploy(exchg_data)

Join().apply(lp, user_nm, eth_amount, tkn_amount, lwr_tick, upr_tick)
lp.summary()
Exchange ETH-TKN (LP)
Real Reserves:   ETH = 1000.0, TKN = 100000.0
Gross Liquidity: 10000.0

Retrieve LP prices

[15]:
p_eth = LPQuote().get_price(lp, eth)
p_tkn = LPQuote().get_price(lp, tkn)
print(f'The price of {eth.token_name} in {tkn.token_name} is {p_eth}')
print(f'The price of {tkn.token_name} in {eth.token_name} is {p_tkn}')
The price of ETH in TKN is 100.0
The price of TKN in ETH is 0.01

Retrieve token settlement amount given opposing token amount

[16]:
amt_eth = LPQuote().get_amount(lp, eth, 1, lwr_tick, upr_tick)
amt_tkn = LPQuote().get_amount(lp, tkn, 1, lwr_tick, upr_tick)
print(f'1 {eth.token_name} token is worth {amt_tkn} {tkn.token_name}')
print(f'1 {tkn.token_name} token is worth {amt_eth} {eth.token_name}')
1 ETH token is worth 0.009969900600093062 TKN
1 TKN token is worth 99.60069810398764 ETH

Retrieve rebased token settlement amount given amount of LP token

[17]:
amt_eth = LPQuote(False).get_amount_from_lp(lp, eth, 1, lwr_tick, upr_tick)
amt_tkn = LPQuote().get_amount_from_lp(lp, eth, 1, lwr_tick, upr_tick)
print(f'1 LP token is worth {amt_eth} {eth.token_name} after swap fees')
print(f'1 LP token is worth {amt_tkn} {tkn.token_name} after swap fees')
1 LP token is worth 1.9969005990709153e-19 ETH after swap fees
1 LP token is worth 1.9905136039497506e-17 TKN after swap fees

Retrieve LP token settlement amount given amount of asset token

[18]:
amt_eth_lp = LPQuote(False).get_lp_from_amount(lp, eth, 1, lwr_tick, upr_tick)
amt_tkn_lp = LPQuote(False).get_lp_from_amount(lp, tkn, 1, lwr_tick, upr_tick)
print(f'1 {eth.token_name} token is worth {amt_eth_lp} LP tokens')
print(f'1 {tkn.token_name} token is worth {amt_tkn_lp} LP tokens')
1 ETH token is worth 5.008760010717809e-18 LP tokens
1 TKN token is worth 5.007523748208768e-20 LP tokens