token_transfer index

This index allows indexing token transfers of contracts compatible with FA1.2 or FA2 standards.

spec_version: 1.2
package: demo_tzbtc_transfers

database:
  kind: sqlite
  path: demo-tzbtc-transfers.sqlite3

contracts:
  tzbtc_mainnet:
    address: KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn
    typename: tzbtc

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

indexes:
  tzbtc_holders_mainnet:
    kind: token_transfer
    datasource: tzkt
    handlers:
      - callback: on_token_transfer
        contract: tzbtc_mainnet

You can also filter transfers by token ID or sender/receiver address.

    handlers:
      - callback: on_token_transfer
        token_id: 1
        to: tzbtc_mainnet

Callback receives TokenTransferData model that optionally contains the transfer sender, receiver, amount, and token metadata.

from decimal import Decimal
from decimal import InvalidOperation

from demo_tzbtc_transfers.handlers.on_balance_update import on_balance_update
from dipdup.context import HandlerContext
from dipdup.models import TokenTransferData


async def on_token_transfer(
    ctx: HandlerContext,
    token_transfer: TokenTransferData,
) -> None:
    from_, to = token_transfer.from_address, token_transfer.to_address
    if not from_ or not to or from_ == to:
        return
    try:
        amount = Decimal(token_transfer.amount or 0) / (10**8)
    except InvalidOperation:
        return
    if not amount:
        return

    await on_balance_update(from_, -amount, token_transfer.timestamp)
    await on_balance_update(to, amount, token_transfer.timestamp)