diff --git a/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py b/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py index 730426cdd..0a25bc59b 100644 --- a/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py @@ -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) diff --git a/src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py b/src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py index 0adb266cf..266499c93 100644 --- a/src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py @@ -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 diff --git a/src/demo_quipuswap/handlers/on_fa20_invest_liquidity.py b/src/demo_quipuswap/handlers/on_fa20_invest_liquidity.py index ef3232f1c..2f82320a3 100644 --- a/src/demo_quipuswap/handlers/on_fa20_invest_liquidity.py +++ b/src/demo_quipuswap/handlers/on_fa20_invest_liquidity.py @@ -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)) diff --git a/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py b/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py index e6aa6d8bd..2eb9a50cf 100644 --- a/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py +++ b/src/demo_quipuswap/handlers/on_fa2_tez_to_token.py @@ -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