Skip to content

Commit

Permalink
Quickstart demo (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus authored May 21, 2021
1 parent 6aedba1 commit 92c963d
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 0 deletions.
Empty file added src/demo_tzbtc/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions src/demo_tzbtc/dipdup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
spec_version: 0.1
package: demo_tzbtc

database:
kind: sqlite
path: demo_tzbtc.sqlite3

contracts:
tzbtc_mainnet:
address: KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn
typename: tzbtc

datasources:
tzkt_staging:
kind: tzkt
url: https://staging.api.tzkt.io

indexes:
tzbtc_holders_mainnet:
kind: operation
datasource: tzkt_staging
contracts:
- tzbtc_mainnet
handlers:
- callback: on_transfer
pattern:
- destination: tzbtc_mainnet
entrypoint: transfer
- callback: on_mint
pattern:
- destination: tzbtc_mainnet
entrypoint: mint
Empty file.
14 changes: 14 additions & 0 deletions src/demo_tzbtc/handlers/on_balance_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from decimal import Decimal
from datetime import datetime

import demo_tzbtc.models as models


async def on_balance_update(address: str, balance_update: Decimal, timestamp: datetime):
holder, _ = await models.Holder.get_or_create(address=address)
holder.balance += balance_update # type: ignore
holder.turnover += abs(balance_update) # type: ignore
holder.tx_count += 1 # type: ignore
holder.last_seen = timestamp # type: ignore
assert holder.balance >= 0, address
await holder.save()
16 changes: 16 additions & 0 deletions src/demo_tzbtc/handlers/on_mint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from decimal import Decimal
from typing import Optional

import demo_tzbtc.models as models
from demo_tzbtc.handlers.on_balance_update import on_balance_update
from demo_tzbtc.types.tzbtc.parameter.mint import MintParameter
from demo_tzbtc.types.tzbtc.storage import TzbtcStorage
from dipdup.models import OperationData, OperationHandlerContext, OriginationContext, TransactionContext


async def on_mint(
ctx: OperationHandlerContext,
mint: TransactionContext[MintParameter, TzbtcStorage],
) -> None:
amount = Decimal(mint.parameter.value) / (10 ** 8)
await on_balance_update(address=mint.parameter.to, balance_update=amount, timestamp=mint.data.timestamp)
15 changes: 15 additions & 0 deletions src/demo_tzbtc/handlers/on_rollback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

from dipdup.utils import reindex

_logger = logging.getLogger(__name__)


async def on_rollback(
from_level: int,
to_level: int,
) -> None:
if from_level - to_level == 1:
return
_logger.warning('Rollback event received, reindexing')
await reindex()
19 changes: 19 additions & 0 deletions src/demo_tzbtc/handlers/on_transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from decimal import Decimal
from typing import Optional

import demo_tzbtc.models as models
from demo_tzbtc.handlers.on_balance_update import on_balance_update
from demo_tzbtc.types.tzbtc.parameter.transfer import TransferParameter
from demo_tzbtc.types.tzbtc.storage import TzbtcStorage
from dipdup.models import OperationData, OperationHandlerContext, OriginationContext, TransactionContext


async def on_transfer(
ctx: OperationHandlerContext,
transfer: TransactionContext[TransferParameter, TzbtcStorage],
) -> None:
if transfer.parameter.from_ == transfer.parameter.to:
return
amount = Decimal(transfer.parameter.value) / (10 ** 8)
await on_balance_update(address=transfer.parameter.from_, balance_update=-amount, timestamp=transfer.data.timestamp)
await on_balance_update(address=transfer.parameter.to, balance_update=amount, timestamp=transfer.data.timestamp)
9 changes: 9 additions & 0 deletions src/demo_tzbtc/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from tortoise import Model, fields


class Holder(Model):
address = fields.CharField(max_length=36, pk=True)
balance = fields.DecimalField(decimal_places=8, max_digits=20, default=0)
turnover = fields.DecimalField(decimal_places=8, max_digits=20, default=0)
tx_count = fields.BigIntField(default=0)
last_seen = fields.DatetimeField(null=True)
Empty file.
Empty file.
Empty file.
11 changes: 11 additions & 0 deletions src/demo_tzbtc/types/tzbtc/parameter/mint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# generated by datamodel-codegen:
# filename: mint.json

from __future__ import annotations

from pydantic import BaseModel


class MintParameter(BaseModel):
to: str
value: str
12 changes: 12 additions & 0 deletions src/demo_tzbtc/types/tzbtc/parameter/transfer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# generated by datamodel-codegen:
# filename: transfer.json

from __future__ import annotations

from pydantic import BaseModel, Field


class TransferParameter(BaseModel):
from_: str = Field(..., alias='from')
to: str
value: str
15 changes: 15 additions & 0 deletions src/demo_tzbtc/types/tzbtc/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# generated by datamodel-codegen:
# filename: storage.json

from __future__ import annotations

from typing import Dict

from pydantic import BaseModel, Field


class TzbtcStorage(BaseModel):
big_map: Dict[str, str]
bool: bool
lambda_: str = Field(..., alias='lambda')
nat: str

0 comments on commit 92c963d

Please # to comment.