Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Quipuswap empty pool edge case #42

Merged
merged 2 commits into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ async def on_fa12_divest_liquidity(
token_qty = Decimal(transfer.parameter.value) / (10 ** decimals)
shares_qty = int(divest_liquidity.parameter.shares)

price = (Decimal(storage.storage.tez_pool) / (10 ** 6)) / (Decimal(storage.storage.token_pool) / (10 ** decimals))
tez_pool = Decimal(storage.storage.tez_pool) / (10 ** 6)
token_pool = Decimal(storage.storage.token_pool) / (10 ** decimals)
if tez_pool and token_pool:
price = tez_pool / token_pool
else:
last_trade = await models.Trade.filter(symbol=symbol).order_by('-id').first()
assert last_trade
price = last_trade.price
share_px = (tez_qty + price * token_qty) / shares_qty

position.realized_pl += shares_qty * (share_px - position.avg_share_px)
Expand Down
15 changes: 12 additions & 3 deletions src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,20 @@ async def on_fa20_divest_liquidity(
transaction = next(op for op in ctx.operations if op.amount)

tez_qty = Decimal(transaction.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.__root__[0].txs[0].amount) / (10 ** decimals)
token_qty = sum(Decimal(tx.amount) for tx in transfer.parameter.__root__[0].txs) / (10 ** decimals)
shares_qty = int(divest_liquidity.parameter.shares)

price = (Decimal(storage.storage.tez_pool) / (10 ** 6)) / (Decimal(storage.storage.token_pool) / (10 ** decimals))
share_px = (tez_qty + price * token_qty) / shares_qty
tez_pool = Decimal(storage.storage.tez_pool) / (10 ** 6)
token_pool = Decimal(storage.storage.token_pool) / (10 ** decimals)
if tez_pool and token_pool:
price = tez_pool / token_pool
share_px = (tez_qty + price * token_qty) / shares_qty
else:
last_trade = await models.Trade.filter(symbol=symbol).order_by('-id').first()
assert last_trade
price = last_trade.price
shares_qty = 0
share_px = 0

position.realized_pl += shares_qty * (share_px - position.avg_share_px)
position.shares_qty -= shares_qty # type: ignore
Expand Down
2 changes: 1 addition & 1 deletion src/demo_quipuswap/handlers/on_fa20_invest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ async def on_fa20_invest_liquidity(
position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)

tez_qty = Decimal(invest_liquidity.data.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.__root__[0].txs[0].amount) / (10 ** decimals)
token_qty = sum(Decimal(tx.amount) for tx in transfer.parameter.__root__[0].txs) / (10 ** decimals)
new_shares_qty = int(storage.storage.ledger[trader].balance) + int(storage.storage.ledger[trader].frozen_balance) # type: ignore

price = (Decimal(storage.storage.tez_pool) / (10 ** 6)) / (Decimal(storage.storage.token_pool) / (10 ** decimals))
Expand Down
2 changes: 1 addition & 1 deletion src/demo_quipuswap/handlers/on_fa2_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def on_fa2_tez_to_token(
trader = tez_to_token_payment.data.sender_address

min_token_quantity = Decimal(tez_to_token_payment.parameter.min_out) / (10 ** decimals)
token_quantity = Decimal(transfer.parameter.__root__[0].txs[0].amount) / (10 ** decimals)
token_quantity = sum(Decimal(tx.amount) for tx in transfer.parameter.__root__[0].txs) / (10 ** decimals)
tez_quantity = Decimal(tez_to_token_payment.data.amount) / (10 ** 6)
assert min_token_quantity <= token_quantity, tez_to_token_payment.data.hash

Expand Down