Skip to content

Commit

Permalink
Origination operation type, fix processing multiple indexes (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored May 6, 2021
1 parent d1f7ad0 commit ead7c6b
Show file tree
Hide file tree
Showing 46 changed files with 705 additions and 412 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ indexes:
handlers:
- callback: on_mint
pattern:
- destination: HEN_minter
- type: transaction
destination: HEN_minter
entrypoint: mint_OBJKT
- destination: HEN_objkts
- type: transaction
destination: HEN_objkts
entrypoint: mint
```
Expand Down Expand Up @@ -131,13 +133,13 @@ from demo_hic_et_nunc.types.hen_minter.parameter.mint_objkt import MintOBJKTPara
from demo_hic_et_nunc.types.hen_minter.storage import HenMinterStorage
from demo_hic_et_nunc.types.hen_objkts.parameter.mint import MintParameter
from demo_hic_et_nunc.types.hen_objkts.storage import HenObjktsStorage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import TransactionContext, OperationHandlerContext
async def on_mint(
ctx: OperationHandlerContext,
mint_objkt: OperationContext[MintOBJKTParameter, HenMinterStorage],
mint: OperationContext[MintParameter, HenObjktsStorage],
mint_objkt: TransactionContext[MintOBJKTParameter, HenMinterStorage],
mint: TransactionContext[MintParameter, HenObjktsStorage],
) -> None:
holder, _ = await models.Holder.get_or_create(address=mint.parameter.address)
token = models.Token(
Expand Down Expand Up @@ -201,15 +203,19 @@ templates:
handlers:
- callback: on_fa12_token_to_tez
pattern:
- destination: <dex>
- type: transaction
destination: <dex>
entrypoint: tokenToTezPayment
- destination: <token>
- type: transaction
destination: <token>
entrypoint: transfer
- callback: on_fa20_tez_to_token
pattern:
- destination: <dex>
- type: transaction
destination: <dex>
entrypoint: tezToTokenPayment
- destination: <token>
- type: transaction
destination: <token>
entrypoint: transfer
indexes:
Expand Down
15 changes: 10 additions & 5 deletions src/demo_hic_et_nunc/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,24 @@ indexes:
handlers:
- callback: on_mint
pattern:
- destination: HEN_minter
- type: transaction
destination: HEN_minter
entrypoint: mint_OBJKT
- destination: HEN_objkts
- type: transaction
destination: HEN_objkts
entrypoint: mint
- callback: on_swap
pattern:
- destination: HEN_minter
- type: transaction
destination: HEN_minter
entrypoint: swap
- callback: on_cancel_swap
pattern:
- destination: HEN_minter
- type: transaction
destination: HEN_minter
entrypoint: cancel_swap
- callback: on_collect
pattern:
- destination: HEN_minter
- type: transaction
destination: HEN_minter
entrypoint: collect
4 changes: 2 additions & 2 deletions src/demo_hic_et_nunc/handlers/on_cancel_swap.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.cancel_swap import CancelSwapParameter
from demo_hic_et_nunc.types.hen_minter.storage import HenMinterStorage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_cancel_swap(
ctx: OperationHandlerContext,
cancel_swap: OperationContext[CancelSwapParameter, HenMinterStorage],
cancel_swap: TransactionContext[CancelSwapParameter, HenMinterStorage],
) -> None:
swap = await models.Swap.filter(id=int(cancel_swap.parameter.__root__)).get()
swap.status = models.SwapStatus.CANCELED
Expand Down
4 changes: 2 additions & 2 deletions src/demo_hic_et_nunc/handlers/on_collect.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.collect import CollectParameter
from demo_hic_et_nunc.types.hen_minter.storage import HenMinterStorage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_collect(
ctx: OperationHandlerContext,
collect: OperationContext[CollectParameter, HenMinterStorage],
collect: TransactionContext[CollectParameter, HenMinterStorage],
) -> None:
swap = await models.Swap.filter(id=collect.parameter.swap_id).get()
seller = await swap.creator
Expand Down
6 changes: 3 additions & 3 deletions src/demo_hic_et_nunc/handlers/on_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
from demo_hic_et_nunc.types.hen_minter.storage import HenMinterStorage
from demo_hic_et_nunc.types.hen_objkts.parameter.mint import MintParameter
from demo_hic_et_nunc.types.hen_objkts.storage import HenObjktsStorage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_mint(
ctx: OperationHandlerContext,
mint_objkt: OperationContext[MintOBJKTParameter, HenMinterStorage],
mint: OperationContext[MintParameter, HenObjktsStorage],
mint_objkt: TransactionContext[MintOBJKTParameter, HenMinterStorage],
mint: TransactionContext[MintParameter, HenObjktsStorage],
) -> None:
holder, _ = await models.Holder.get_or_create(address=mint.parameter.address)
token = models.Token(
Expand Down
4 changes: 2 additions & 2 deletions src/demo_hic_et_nunc/handlers/on_swap.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.swap import SwapParameter
from demo_hic_et_nunc.types.hen_minter.storage import HenMinterStorage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_swap(
ctx: OperationHandlerContext,
swap: OperationContext[SwapParameter, HenMinterStorage],
swap: TransactionContext[SwapParameter, HenMinterStorage],
) -> None:
holder, _ = await models.Holder.get_or_create(address=swap.data.sender_address)
swap_model = models.Swap(
Expand Down
54 changes: 36 additions & 18 deletions src/demo_quipuswap/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,31 +39,40 @@ templates:
handlers:
- callback: on_fa12_token_to_tez
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: tokenToTezPayment
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa12_tez_to_token
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: tezToTokenPayment
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa12_invest_liquidity
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: investLiquidity
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa12_divest_liquidity
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: divestLiquidity
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa12_withdraw_profit
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: withdrawProfit

quipuswap_fa2:
Expand All @@ -74,31 +83,40 @@ templates:
handlers:
- callback: on_fa2_token_to_tez
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: tokenToTezPayment
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa2_tez_to_token
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: tezToTokenPayment
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa20_invest_liquidity
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: investLiquidity
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa20_divest_liquidity
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: divestLiquidity
- destination: <token_contract>
- type: transaction
destination: <token_contract>
entrypoint: transfer
- callback: on_fa20_withdraw_profit
pattern:
- destination: <dex_contract>
- type: transaction
destination: <dex_contract>
entrypoint: withdrawProfit

indexes:
Expand Down
7 changes: 4 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from demo_quipuswap.types.fa12_token.storage import Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.divest_liquidity import DivestLiquidityParameter
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_fa12_divest_liquidity(
ctx: OperationHandlerContext,
divest_liquidity: OperationContext[DivestLiquidityParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
divest_liquidity: TransactionContext[DivestLiquidityParameter, QuipuFa12Storage],
transfer: TransactionContext[TransferParameter, Fa12TokenStorage],
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -25,6 +25,7 @@ async def on_fa12_divest_liquidity(
position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)
transaction = next(op for op in ctx.operations if op.amount)

assert transaction.amount is not None
tez_qty = Decimal(transaction.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.value) / (10 ** decimals)
shares_qty = int(divest_liquidity.parameter.shares)
Expand Down
7 changes: 4 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from demo_quipuswap.types.fa12_token.storage import Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.invest_liquidity import InvestLiquidityParameter
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_fa12_invest_liquidity(
ctx: OperationHandlerContext,
invest_liquidity: OperationContext[InvestLiquidityParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
invest_liquidity: TransactionContext[InvestLiquidityParameter, QuipuFa12Storage],
transfer: TransactionContext[TransferParameter, Fa12TokenStorage],
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -24,6 +24,7 @@ async def on_fa12_invest_liquidity(

position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)

assert invest_liquidity.data.amount is not None
tez_qty = Decimal(invest_liquidity.data.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.value) / (10 ** decimals)
new_shares_qty = int(storage.storage.ledger[trader].balance) + int(storage.storage.ledger[trader].frozen_balance) # type: ignore
Expand Down
7 changes: 4 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from demo_quipuswap.types.fa12_token.storage import Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.tez_to_token_payment import TezToTokenPaymentParameter
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_fa12_tez_to_token(
ctx: OperationHandlerContext,
tez_to_token_payment: OperationContext[TezToTokenPaymentParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
tez_to_token_payment: TransactionContext[TezToTokenPaymentParameter, QuipuFa12Storage],
transfer: TransactionContext[TransferParameter, Fa12TokenStorage],
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -22,6 +22,7 @@ async def on_fa12_tez_to_token(

min_token_quantity = Decimal(tez_to_token_payment.parameter.min_out) / (10 ** decimals)
token_quantity = Decimal(transfer.parameter.value) / (10 ** decimals)
assert tez_to_token_payment.data.amount is not None
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
7 changes: 4 additions & 3 deletions src/demo_quipuswap/handlers/on_fa12_token_to_tez.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from demo_quipuswap.types.fa12_token.storage import Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.token_to_tez_payment import TokenToTezPaymentParameter
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_fa12_token_to_tez(
ctx: OperationHandlerContext,
token_to_tez_payment: OperationContext[TokenToTezPaymentParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
token_to_tez_payment: TransactionContext[TokenToTezPaymentParameter, QuipuFa12Storage],
transfer: TransactionContext[TransferParameter, Fa12TokenStorage],
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -23,6 +23,7 @@ async def on_fa12_token_to_tez(
min_tez_quantity = Decimal(token_to_tez_payment.parameter.min_out) / (10 ** 6)
token_quantity = Decimal(token_to_tez_payment.parameter.amount) / (10 ** decimals)
transaction = next(op for op in ctx.operations if op.amount)
assert transaction.amount is not None
tez_quantity = Decimal(transaction.amount) / (10 ** 6)
assert min_tez_quantity <= tez_quantity, token_to_tez_payment.data.hash

Expand Down
5 changes: 3 additions & 2 deletions src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa12.parameter.withdraw_profit import WithdrawProfitParameter
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_fa12_withdraw_profit(
ctx: OperationHandlerContext,
withdraw_profit: OperationContext[WithdrawProfitParameter, QuipuFa12Storage],
withdraw_profit: TransactionContext[WithdrawProfitParameter, QuipuFa12Storage],
) -> None:

if ctx.template_values is None:
Expand All @@ -20,6 +20,7 @@ async def on_fa12_withdraw_profit(
position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)
transaction = next(op for op in ctx.operations if op.amount)

assert transaction.amount is not None
position.realized_pl += Decimal(transaction.amount) / (10 ** 6) # type: ignore

await position.save()
7 changes: 4 additions & 3 deletions src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from demo_quipuswap.types.fa2_token.storage import Fa2TokenStorage
from demo_quipuswap.types.quipu_fa2.parameter.divest_liquidity import DivestLiquidityParameter
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.models import OperationContext, OperationHandlerContext
from dipdup.models import OperationHandlerContext, TransactionContext


async def on_fa20_divest_liquidity(
ctx: OperationHandlerContext,
divest_liquidity: OperationContext[DivestLiquidityParameter, QuipuFa2Storage],
transfer: OperationContext[TransferParameter, Fa2TokenStorage],
divest_liquidity: TransactionContext[DivestLiquidityParameter, QuipuFa2Storage],
transfer: TransactionContext[TransferParameter, Fa2TokenStorage],
) -> None:

if ctx.template_values is None:
Expand All @@ -26,6 +26,7 @@ async def on_fa20_divest_liquidity(
position, _ = await models.Position.get_or_create(trader=trader, symbol=symbol)
transaction = next(op for op in ctx.operations if op.amount)

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

0 comments on commit ead7c6b

Please sign in to comment.