Skip to content

Commit

Permalink
Multiple contracts in operation index, big_map index (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
droserasprout authored Apr 29, 2021
1 parent 00a5eb7 commit d4acf10
Show file tree
Hide file tree
Showing 95 changed files with 1,489 additions and 667 deletions.
34 changes: 19 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ indexes:
hen_mainnet:
kind: operation
datasource: tzkt_mainnet
contract: HEN_minter
contracts:
- HEN_minter
handlers:
- callback: on_mint
pattern:
Expand Down Expand Up @@ -125,19 +126,21 @@ class Token(Model):
Now take a look at `handlers` module generated by `init` command. When operation group matching `pattern` block of corresponding handler at config will arrive callback will be fired. This example will simply save minted Hic Et Nunc tokens and their owners to the database:

```python
from demo_hic_et_nunc.models import Holder, Token
from demo_hic_et_nunc.types.hen_minter.parameter.mint_objkt import MintOBJKT
from demo_hic_et_nunc.types.hen_objkts.parameter.mint import Mint
from dipdup.models import HandlerContext, OperationContext
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.mint_objkt import MintOBJKTParameter
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
async def on_mint(
ctx: HandlerContext,
mint_objkt: OperationContext[MintOBJKT],
mint: OperationContext[Mint],
ctx: OperationHandlerContext,
mint_objkt: OperationContext[MintOBJKTParameter, HenMinterStorage],
mint: OperationContext[MintParameter, HenObjktsStorage],
) -> None:
holder, _ = await Holder.get_or_create(address=mint.parameter.address)
token = Token(
holder, _ = await models.Holder.get_or_create(address=mint.parameter.address)
token = models.Token(
id=mint.parameter.token_id,
creator=holder,
supply=mint.parameter.amount,
Expand Down Expand Up @@ -193,19 +196,20 @@ templates:
trades:
kind: operation
datasource: tzkt_staging
contract: < dex >
contracts:
- <dex>
handlers:
- callback: on_fa12_token_to_tez
pattern:
- destination: < dex >
- destination: <dex>
entrypoint: tokenToTezPayment
- destination: < token >
- destination: <token>
entrypoint: transfer
- callback: on_fa20_tez_to_token
pattern:
- destination: < dex >
- destination: <dex>
entrypoint: tezToTokenPayment
- destination: < token >
- destination: <token>
entrypoint: transfer
indexes:
Expand Down
3 changes: 2 additions & 1 deletion src/demo_hic_et_nunc/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ indexes:
hen_mainnet:
kind: operation
datasource: tzkt_mainnet
contract: HEN_minter
contracts:
- HEN_minter
handlers:
- callback: on_mint
pattern:
Expand Down
8 changes: 4 additions & 4 deletions src/demo_hic_et_nunc/handlers/on_cancel_swap.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.cancel_swap import CancelSwap as CancelSwapParameter
from demo_hic_et_nunc.types.hen_minter.storage import Storage as HenMinterStorage
from dipdup.models import HandlerContext, OperationContext
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


async def on_cancel_swap(
ctx: HandlerContext,
ctx: OperationHandlerContext,
cancel_swap: OperationContext[CancelSwapParameter, HenMinterStorage],
) -> None:
swap = await models.Swap.filter(id=int(cancel_swap.parameter.__root__)).get()
Expand Down
8 changes: 4 additions & 4 deletions src/demo_hic_et_nunc/handlers/on_collect.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.collect import Collect as CollectParameter
from demo_hic_et_nunc.types.hen_minter.storage import Storage as HenMinterStorage
from dipdup.models import HandlerContext, OperationContext
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


async def on_collect(
ctx: HandlerContext,
ctx: OperationHandlerContext,
collect: OperationContext[CollectParameter, HenMinterStorage],
) -> None:
swap = await models.Swap.filter(id=collect.parameter.swap_id).get()
Expand Down
12 changes: 6 additions & 6 deletions src/demo_hic_et_nunc/handlers/on_mint.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.mint_objkt import MintOBJKT as MintOBJKTParameter
from demo_hic_et_nunc.types.hen_minter.storage import Storage as HenMinterStorage
from demo_hic_et_nunc.types.hen_objkts.parameter.mint import Mint as MintParameter
from demo_hic_et_nunc.types.hen_objkts.storage import Storage as HenObjktsStorage
from dipdup.models import HandlerContext, OperationContext
from demo_hic_et_nunc.types.hen_minter.parameter.mint_objkt import MintOBJKTParameter
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


async def on_mint(
ctx: HandlerContext,
ctx: OperationHandlerContext,
mint_objkt: OperationContext[MintOBJKTParameter, HenMinterStorage],
mint: OperationContext[MintParameter, HenObjktsStorage],
) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/demo_hic_et_nunc/handlers/on_swap.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import demo_hic_et_nunc.models as models
from demo_hic_et_nunc.types.hen_minter.parameter.swap import Swap as SwapParameter
from demo_hic_et_nunc.types.hen_minter.storage import Storage as HenMinterStorage
from dipdup.models import HandlerContext, OperationContext
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


async def on_swap(
ctx: HandlerContext,
ctx: OperationHandlerContext,
swap: OperationContext[SwapParameter, HenMinterStorage],
) -> None:
holder, _ = await models.Holder.get_or_create(address=swap.data.sender_address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
from pydantic import BaseModel


class CancelSwap(BaseModel):
class CancelSwapParameter(BaseModel):
__root__: str
2 changes: 1 addition & 1 deletion src/demo_hic_et_nunc/types/hen_minter/parameter/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
from pydantic import BaseModel


class Collect(BaseModel):
class CollectParameter(BaseModel):
objkt_amount: str
swap_id: str
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import BaseModel


class MintOBJKT(BaseModel):
class MintOBJKTParameter(BaseModel):
address: str
amount: str
metadata: str
Expand Down
2 changes: 1 addition & 1 deletion src/demo_hic_et_nunc/types/hen_minter/parameter/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pydantic import BaseModel


class Swap(BaseModel):
class SwapParameter(BaseModel):
objkt_amount: str
objkt_id: str
xtz_per_objkt: str
2 changes: 1 addition & 1 deletion src/demo_hic_et_nunc/types/hen_minter/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Swaps(BaseModel):
xtz_per_objkt: str


class Storage(BaseModel):
class HenMinterStorage(BaseModel):
curate: str
genesis: str
hdao: str
Expand Down
2 changes: 1 addition & 1 deletion src/demo_hic_et_nunc/types/hen_objkts/parameter/mint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from pydantic import BaseModel


class Mint(BaseModel):
class MintParameter(BaseModel):
address: str
amount: str
token_id: str
Expand Down
4 changes: 2 additions & 2 deletions src/demo_hic_et_nunc/types/hen_objkts/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class LedgerItem(BaseModel):


class Key1(BaseModel):
owner: str
operator: str
owner: str
token_id: str


Expand All @@ -34,7 +34,7 @@ class TokenMetadata(BaseModel):
token_info: Dict[str, str]


class Storage(BaseModel):
class HenObjktsStorage(BaseModel):
administrator: str
all_tokens: str
ledger: List[LedgerItem]
Expand Down
6 changes: 4 additions & 2 deletions src/demo_quipuswap/dipdup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ templates:
quipuswap_fa12:
kind: operation
datasource: tzkt_staging_mainnet
contract: <dex_contract>
contracts:
- <dex_contract>
handlers:
- callback: on_fa12_token_to_tez
pattern:
Expand Down Expand Up @@ -68,7 +69,8 @@ templates:
quipuswap_fa2:
kind: operation
datasource: tzkt_staging_mainnet
contract: <dex_contract>
contracts:
- <dex_contract>
handlers:
- callback: on_fa2_token_to_tez
pattern:
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
@@ -1,15 +1,15 @@
from decimal import Decimal

import demo_quipuswap.models as models
from demo_quipuswap.types.fa12_token.parameter.transfer import Transfer as TransferParameter
from demo_quipuswap.types.fa12_token.storage import Storage as Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.divest_liquidity import DivestLiquidity as DivestLiquidityParameter
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFa12Storage
from dipdup.models import HandlerContext, OperationContext
from demo_quipuswap.types.fa12_token.parameter.transfer import TransferParameter
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


async def on_fa12_divest_liquidity(
ctx: HandlerContext,
ctx: OperationHandlerContext,
divest_liquidity: OperationContext[DivestLiquidityParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
) -> None:
Expand Down
14 changes: 7 additions & 7 deletions src/demo_quipuswap/handlers/on_fa12_invest_liquidity.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from decimal import Decimal

import demo_quipuswap.models as models
from demo_quipuswap.types.fa12_token.parameter.transfer import Transfer as TransferParameter
from demo_quipuswap.types.fa12_token.storage import Storage as Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.invest_liquidity import InvestLiquidity as InvestLiquidityParameter
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFa12Storage
from dipdup.models import HandlerContext, OperationContext
from demo_quipuswap.types.fa12_token.parameter.transfer import TransferParameter
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


async def on_fa12_invest_liquidity(
ctx: HandlerContext,
ctx: OperationHandlerContext,
invest_liquidity: OperationContext[InvestLiquidityParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
) -> None:
Expand All @@ -26,7 +26,7 @@ async def on_fa12_invest_liquidity(

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)
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))
value = tez_qty + price * token_qty
Expand Down
12 changes: 6 additions & 6 deletions src/demo_quipuswap/handlers/on_fa12_tez_to_token.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from decimal import Decimal

import demo_quipuswap.models as models
from demo_quipuswap.types.fa12_token.parameter.transfer import Transfer as TransferParameter
from demo_quipuswap.types.fa12_token.storage import Storage as Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.tez_to_token_payment import TezToTokenPayment as TezToTokenPaymentParameter
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFa12Storage
from dipdup.models import HandlerContext, OperationContext
from demo_quipuswap.types.fa12_token.parameter.transfer import TransferParameter
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


async def on_fa12_tez_to_token(
ctx: HandlerContext,
ctx: OperationHandlerContext,
tez_to_token_payment: OperationContext[TezToTokenPaymentParameter, QuipuFa12Storage],
transfer: OperationContext[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
@@ -1,15 +1,15 @@
from decimal import Decimal

import demo_quipuswap.models as models
from demo_quipuswap.types.fa12_token.parameter.transfer import Transfer as TransferParameter
from demo_quipuswap.types.fa12_token.storage import Storage as Fa12TokenStorage
from demo_quipuswap.types.quipu_fa12.parameter.token_to_tez_payment import TokenToTezPayment as TokenToTezPaymentParameter
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFa12Storage
from dipdup.models import HandlerContext, OperationContext
from demo_quipuswap.types.fa12_token.parameter.transfer import TransferParameter
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


async def on_fa12_token_to_tez(
ctx: HandlerContext,
ctx: OperationHandlerContext,
token_to_tez_payment: OperationContext[TokenToTezPaymentParameter, QuipuFa12Storage],
transfer: OperationContext[TransferParameter, Fa12TokenStorage],
) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/demo_quipuswap/handlers/on_fa12_withdraw_profit.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from decimal import Decimal

import demo_quipuswap.models as models
from demo_quipuswap.types.quipu_fa12.parameter.withdraw_profit import WithdrawProfit as WithdrawProfitParameter
from demo_quipuswap.types.quipu_fa12.storage import Storage as QuipuFa12Storage
from dipdup.models import HandlerContext, OperationContext
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


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

Expand Down
12 changes: 6 additions & 6 deletions src/demo_quipuswap/handlers/on_fa20_divest_liquidity.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from decimal import Decimal

import demo_quipuswap.models as models
from demo_quipuswap.types.fa2_token.parameter.transfer import Transfer as TransferParameter
from demo_quipuswap.types.fa2_token.storage import Storage as Fa2TokenStorage
from demo_quipuswap.types.quipu_fa2.parameter.divest_liquidity import DivestLiquidity as DivestLiquidityParameter
from demo_quipuswap.types.quipu_fa2.storage import Storage as QuipuFa2Storage
from dipdup.models import HandlerContext, OperationContext
from demo_quipuswap.types.fa2_token.parameter.transfer import TransferParameter
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


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

0 comments on commit d4acf10

Please sign in to comment.