Skip to content

Commit

Permalink
BigMapIndex handlers get single diff at once, logging improvements (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored Jun 17, 2021
1 parent bf968fe commit 8d9d299
Show file tree
Hide file tree
Showing 61 changed files with 415 additions and 434 deletions.
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.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_cancel_swap(
ctx: OperationHandlerContext,
ctx: HandlerContext,
cancel_swap: Transaction[CancelSwapParameter, HenMinterStorage],
) -> None:
swap = await models.Swap.filter(id=int(cancel_swap.parameter.__root__)).get()
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.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_collect(
ctx: OperationHandlerContext,
ctx: HandlerContext,
collect: Transaction[CollectParameter, HenMinterStorage],
) -> None:
swap = await models.Swap.filter(id=collect.parameter.swap_id).get()
Expand Down
4 changes: 2 additions & 2 deletions src/demo_hic_et_nunc/handlers/on_mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
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.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_mint(
ctx: OperationHandlerContext,
ctx: HandlerContext,
mint_objkt: Transaction[MintOBJKTParameter, HenMinterStorage],
mint: Transaction[MintParameter, HenObjktsStorage],
) -> None:
Expand Down
6 changes: 1 addition & 5 deletions src/demo_hic_et_nunc/handlers/on_rollback.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import logging

from dipdup.context import RollbackHandlerContext

_logger = logging.getLogger(__name__)


async def on_rollback(ctx: RollbackHandlerContext) -> None:
_logger.warning('Datasource `%s` rolled back from level %s to level %s, reindexing', ctx.datasource, ctx.from_level, ctx.to_level)
ctx.logger.warning('Datasource `%s` rolled back from level %s to level %s, reindexing', ctx.datasource, ctx.from_level, ctx.to_level)
await ctx.reindex()
6 changes: 3 additions & 3 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.context import OperationHandlerContext
from dipdup.models import Transaction
from dipdup.context import HandlerContext, RollbackHandlerContext
from dipdup.models import BigMapAction, BigMapData, BigMapDiff, OperationData, Origination, Transaction


async def on_swap(
ctx: OperationHandlerContext,
ctx: HandlerContext,
swap: Transaction[SwapParameter, HenMinterStorage],
) -> None:
holder, _ = await models.Holder.get_or_create(address=swap.data.sender_address)
Expand Down
8 changes: 8 additions & 0 deletions src/demo_quipuswap/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ templates:
- type: transaction
destination: <token_contract>
entrypoint: transfer
- type: transaction
source: <dex_contract>
- callback: on_fa12_tez_to_token
pattern:
- type: transaction
Expand Down Expand Up @@ -75,6 +77,8 @@ templates:
- type: transaction
destination: <token_contract>
entrypoint: transfer
- type: transaction
source: <dex_contract>
- callback: on_fa12_withdraw_profit
pattern:
- type: transaction
Expand Down Expand Up @@ -105,6 +109,8 @@ templates:
- type: transaction
destination: <token_contract>
entrypoint: transfer
- type: transaction
source: <dex_contract>
- callback: on_fa2_tez_to_token
pattern:
- type: transaction
Expand Down Expand Up @@ -134,6 +140,8 @@ templates:
- type: transaction
destination: <token_contract>
entrypoint: transfer
- type: transaction
source: <dex_contract>
- callback: on_fa2_withdraw_profit
pattern:
- type: transaction
Expand Down
12 changes: 6 additions & 6 deletions src/demo_quipuswap/handlers/on_fa12_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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.context import OperationHandlerContext
from dipdup.models import Transaction
from dipdup.context import HandlerContext
from dipdup.models import OperationData, Transaction


async def on_fa12_divest_liquidity(
ctx: OperationHandlerContext,
ctx: HandlerContext,
divest_liquidity: Transaction[DivestLiquidityParameter, QuipuFa12Storage],
transfer: Transaction[TransferParameter, Fa12TokenStorage],
transaction_1: OperationData,
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -24,10 +25,9 @@ async def on_fa12_divest_liquidity(
trader = divest_liquidity.data.sender_address

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)
assert transaction_1.amount is not None
tez_qty = Decimal(transaction_1.amount) / (10 ** 6)
token_qty = Decimal(transfer.parameter.value) / (10 ** decimals)
shares_qty = int(divest_liquidity.parameter.shares)

Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
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.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_fa12_invest_liquidity(
ctx: OperationHandlerContext,
ctx: HandlerContext,
invest_liquidity: Transaction[InvestLiquidityParameter, QuipuFa12Storage],
transfer: Transaction[TransferParameter, Fa12TokenStorage],
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa12_origination.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Origination


async def on_fa12_origination(
ctx: OperationHandlerContext,
ctx: HandlerContext,
quipu_fa12_origination: Origination[QuipuFa12Storage],
) -> None:
if ctx.template_values is None:
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa12_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
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.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_fa12_tez_to_token(
ctx: OperationHandlerContext,
ctx: HandlerContext,
tez_to_token_payment: Transaction[TezToTokenPaymentParameter, QuipuFa12Storage],
transfer: Transaction[TransferParameter, Fa12TokenStorage],
) -> None:
Expand Down
12 changes: 6 additions & 6 deletions src/demo_quipuswap/handlers/on_fa12_token_to_tez.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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.context import OperationHandlerContext
from dipdup.models import Transaction
from dipdup.context import HandlerContext
from dipdup.models import OperationData, Transaction


async def on_fa12_token_to_tez(
ctx: OperationHandlerContext,
ctx: HandlerContext,
token_to_tez_payment: Transaction[TokenToTezPaymentParameter, QuipuFa12Storage],
transfer: Transaction[TransferParameter, Fa12TokenStorage],
transaction_0: OperationData,
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -23,9 +24,8 @@ 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 transaction_0.amount is not None
tez_quantity = Decimal(transaction_0.amount) / (10 ** 6)
assert min_tez_quantity <= tez_quantity, token_to_tez_payment.data.hash

trade = models.Trade(
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa12_transfer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa12.parameter.transfer import TransferParameter
from demo_quipuswap.types.quipu_fa12.storage import QuipuFa12Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_fa12_transfer(
ctx: OperationHandlerContext,
ctx: HandlerContext,
transfer: Transaction[TransferParameter, QuipuFa12Storage],
) -> None:
if ctx.template_values is None:
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,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.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import OperationData, Transaction


async def on_fa12_withdraw_profit(
ctx: OperationHandlerContext,
ctx: HandlerContext,
withdraw_profit: Transaction[WithdrawProfitParameter, QuipuFa12Storage],
transaction_0: Optional[OperationData] = None,
) -> None:
Expand Down
12 changes: 6 additions & 6 deletions src/demo_quipuswap/handlers/on_fa2_divest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
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.context import OperationHandlerContext
from dipdup.models import Transaction
from dipdup.context import HandlerContext
from dipdup.models import OperationData, Transaction


async def on_fa2_divest_liquidity(
ctx: OperationHandlerContext,
ctx: HandlerContext,
divest_liquidity: Transaction[DivestLiquidityParameter, QuipuFa2Storage],
transfer: Transaction[TransferParameter, Fa2TokenStorage],
transaction_1: OperationData,
) -> None:

if ctx.template_values is None:
Expand All @@ -25,10 +26,9 @@ async def on_fa2_divest_liquidity(
trader = divest_liquidity.data.sender_address

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)
assert transaction_1.amount is not None
tez_qty = Decimal(transaction_1.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
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa2_invest_liquidity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from demo_quipuswap.types.fa2_token.storage import Fa2TokenStorage
from demo_quipuswap.types.quipu_fa2.parameter.invest_liquidity import InvestLiquidityParameter
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_fa2_invest_liquidity(
ctx: OperationHandlerContext,
ctx: HandlerContext,
invest_liquidity: Transaction[InvestLiquidityParameter, QuipuFa2Storage],
transfer: Transaction[TransferParameter, Fa2TokenStorage],
) -> None:
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa2_origination.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Origination


async def on_fa2_origination(
ctx: OperationHandlerContext,
ctx: HandlerContext,
quipu_fa2_origination: Origination[QuipuFa2Storage],
) -> None:
if ctx.template_values is None:
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa2_tez_to_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from demo_quipuswap.types.fa2_token.storage import Fa2TokenStorage
from demo_quipuswap.types.quipu_fa2.parameter.tez_to_token_payment import TezToTokenPaymentParameter
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_fa2_tez_to_token(
ctx: OperationHandlerContext,
ctx: HandlerContext,
tez_to_token_payment: Transaction[TezToTokenPaymentParameter, QuipuFa2Storage],
transfer: Transaction[TransferParameter, Fa2TokenStorage],
) -> None:
Expand Down
12 changes: 6 additions & 6 deletions src/demo_quipuswap/handlers/on_fa2_token_to_tez.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
from demo_quipuswap.types.fa2_token.storage import Fa2TokenStorage
from demo_quipuswap.types.quipu_fa2.parameter.token_to_tez_payment import TokenToTezPaymentParameter
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.context import OperationHandlerContext
from dipdup.models import Transaction
from dipdup.context import HandlerContext
from dipdup.models import OperationData, Transaction


async def on_fa2_token_to_tez(
ctx: OperationHandlerContext,
ctx: HandlerContext,
token_to_tez_payment: Transaction[TokenToTezPaymentParameter, QuipuFa2Storage],
transfer: Transaction[TransferParameter, Fa2TokenStorage],
transaction_0: OperationData,
) -> None:
if ctx.template_values is None:
raise Exception('This index must be templated')
Expand All @@ -23,9 +24,8 @@ async def on_fa2_token_to_tez(

min_tez_quantity = Decimal(token_to_tez_payment.parameter.min_out) / (10 ** decimals)
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 transaction_0.amount is not None
tez_quantity = Decimal(transaction_0.amount) / (10 ** 6)
assert min_tez_quantity <= tez_quantity, token_to_tez_payment.data.hash

trade = models.Trade(
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa2_transfer.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa2.parameter.transfer import TransferParameter
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import Transaction


async def on_fa2_transfer(
ctx: OperationHandlerContext,
ctx: HandlerContext,
transfer: Transaction[TransferParameter, QuipuFa2Storage],
) -> None:
if ctx.template_values is None:
Expand Down
4 changes: 2 additions & 2 deletions src/demo_quipuswap/handlers/on_fa2_withdraw_profit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa2.parameter.withdraw_profit import WithdrawProfitParameter
from demo_quipuswap.types.quipu_fa2.storage import QuipuFa2Storage
from dipdup.context import OperationHandlerContext
from dipdup.context import HandlerContext
from dipdup.models import OperationData, Transaction


async def on_fa2_withdraw_profit(
ctx: OperationHandlerContext,
ctx: HandlerContext,
withdraw_profit: Transaction[WithdrawProfitParameter, QuipuFa2Storage],
transaction_0: Optional[OperationData] = None,
) -> None:
Expand Down
6 changes: 1 addition & 5 deletions src/demo_quipuswap/handlers/on_rollback.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import logging

from dipdup.context import RollbackHandlerContext

_logger = logging.getLogger(__name__)


async def on_rollback(ctx: RollbackHandlerContext) -> None:
_logger.warning('Datasource `%s` rolled back from level %s to level %s, reindexing', ctx.datasource, ctx.from_level, ctx.to_level)
ctx.logger.warning('Datasource `%s` rolled back from level %s to level %s, reindexing', ctx.datasource, ctx.from_level, ctx.to_level)
await ctx.reindex()
Loading

0 comments on commit 8d9d299

Please sign in to comment.