Skip to content

Commit

Permalink
feat: nft transfer and balance ledgers
Browse files Browse the repository at this point in the history
  • Loading branch information
v1nvn committed Oct 16, 2024
1 parent b0f4d5a commit d74374c
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 34 deletions.
90 changes: 56 additions & 34 deletions credmark/cmf/model/ledger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
ContractTable,
LedgerTable,
LogTable,
NFTBalanceTable,
NFTTransferTable,
ReceiptTable,
TokenBalanceTable,
TokenTable,
Expand Down Expand Up @@ -61,12 +63,11 @@

def ledger_table_doc(func):
def wrapper(self, *args, **kwargs):
func.__doc__ = QUERY_METHOD_DOC_STRING.replace(
'{TABLE}', func.__name__)
func.__doc__ = QUERY_METHOD_DOC_STRING.replace("{TABLE}", func.__name__)
result = func(self, *args, **kwargs)
result.__doc__ = QUERY_METHOD_DOC_STRING.replace(
'{TABLE}', func.__name__)
result.__doc__ = QUERY_METHOD_DOC_STRING.replace("{TABLE}", func.__name__)
return result

return wrapper


Expand Down Expand Up @@ -110,11 +111,21 @@ def __init__(self, **kwargs):
super().__init__(**kwargs)


class LedgerQueryNFTTransfer(NFTTransferTable, LedgerQuery):
def __init__(self, **kwargs):
super().__init__(**kwargs)


class LedgerQueryTokenBalance(TokenBalanceTable, LedgerQuery):
def __init__(self, **kwargs):
super().__init__(**kwargs)


class LedgerQueryNFTBalance(NFTBalanceTable, LedgerQuery):
def __init__(self, **kwargs):
super().__init__(**kwargs)


class LedgerQueryTable(LedgerQuery, LedgerTable):
pass

Expand All @@ -131,6 +142,7 @@ class Ledger:
The query parameters are common to all query methods.
"""

# pylint: disable=locally-disabled,invalid-name

def __init__(self):
Expand All @@ -140,8 +152,7 @@ def tables(self) -> List[str]:
"""
Returns a list of tables in the ledger database.
"""
return [prop for prop in dir(self) if
isinstance(getattr(self, prop), LedgerQuery)]
return [prop for prop in dir(self) if isinstance(getattr(self, prop), LedgerQuery)]

def table(self, table_name: str) -> LedgerQueryTable:
"""
Expand All @@ -153,70 +164,81 @@ def table(self, table_name: str) -> LedgerQueryTable:
@ledger_table_doc
def Transaction(self):
return LedgerQueryTransaction(
cwgo_query_table='ledger.transaction_data',
table_key='transactions',
more_cols=[])
cwgo_query_table="ledger.transaction_data", table_key="transactions", more_cols=[]
)

@property
@ledger_table_doc
def Trace(self):
return LedgerQueryTrace(
cwgo_query_table='ledger.trace_data',
table_key='traces',
more_cols=[])
cwgo_query_table="ledger.trace_data", table_key="traces", more_cols=[]
)

@property
@ledger_table_doc
def Block(self):
return LedgerQueryBlock(
cwgo_query_table='ledger.block_data',
table_key='blocks',
more_cols=[])
cwgo_query_table="ledger.block_data", table_key="blocks", more_cols=[]
)

@property
@ledger_table_doc
def Contract(self):
return LedgerQueryContract(
cwgo_query_table='ledger.contract_data',
table_key='contracts',
more_cols=[])
cwgo_query_table="ledger.contract_data", table_key="contracts", more_cols=[]
)

@property
@ledger_table_doc
def Log(self):
return LedgerQueryLog(
cwgo_query_table='ledger.log_data',
table_key='logs',
more_cols=[])
return LedgerQueryLog(cwgo_query_table="ledger.log_data", table_key="logs", more_cols=[])

@property
@ledger_table_doc
def Receipt(self):
return LedgerQueryReceipt(
cwgo_query_table='ledger.receipt_data',
table_key='receipts',
more_cols=[])
cwgo_query_table="ledger.receipt_data", table_key="receipts", more_cols=[]
)

@property
@ledger_table_doc
def Token(self):
return LedgerQueryToken(
cwgo_query_table='ledger.erc20_token_data',
table_key='tokens',
more_cols=[])
cwgo_query_table="ledger.erc20_token_data", table_key="tokens", more_cols=[]
)

@property
@ledger_table_doc
def TokenTransfer(self):
return LedgerQueryTokenTransfer(
cwgo_query_table='ledger.erc20_token_transfer_data',
table_key='token_transfers',
more_cols=[])
cwgo_query_table="ledger.erc20_token_transfer_data",
table_key="token_transfers",
more_cols=[],
)

@property
@ledger_table_doc
def NFTTransfer(self):
return LedgerQueryNFTTransfer(
cwgo_query_table="ledger.erc721_token_transfer_data",
table_key="nft_transfers",
more_cols=[],
)

@property
@ledger_table_doc
def TokenBalance(self):
return LedgerQueryTokenBalance(
cwgo_query_table='ledger.token_balance',
table_key='token_double_entry_book',
more_cols=[])
cwgo_query_table="ledger.token_balance",
table_key="token_double_entry_book",
more_cols=[],
)

@property
@ledger_table_doc
def NFTBalance(self):
return LedgerQueryNFTBalance(
cwgo_query_table="ledger.nft_balance",
table_key="nft_double_entry_book",
more_cols=[],
)
100 changes: 100 additions & 0 deletions credmark/cmf/types/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,58 @@ def bigint_cols(self) -> List[ColumnField]:
return [self.RAW_AMOUNT]


class NFTBalanceTable(LedgerTable):
"""
NFT balance ledger data table
Column names
"""

BLOCK_TIMESTAMP = ColumnField("block_timestamp")
""""""
BLOCK_HASH = ColumnField("block_hash")
""""""
BLOCK_NUMBER = ColumnField("block_number")
""""""
TOKEN_ADDRESS = ColumnField("token_address")
""""""
TOKEN_ID = ColumnField("token_id")
""""""
TRANSACTION_HASH = ColumnField("transaction_hash")
""""""
LOG_INDEX = ColumnField("log_index")
""""""
ADDRESS = ColumnField("address")
""""""
COUNTERPARTY_ADDRESS = ColumnField("counterparty_address")
""""""
RAW_AMOUNT = ColumnField("raw_amount")
"""Unscaled raw amount"""
AMOUNT = ColumnField("amount")
"""Raw Amount scaled by token decimals"""

TYPE_MAPPER = {
"block_timestamp": "str",
"block_hash": "str",
"block_number": "int",
"token_address": "str",
"token_id": "str",
"transaction_hash": "str",
"log_index": "int",
"address": "str",
"from_address": "str",
"to_address": "str",
"raw_amount": "int",
"amount": "int",
}

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)

@property
def bigint_cols(self) -> List[ColumnField]:
return [self.RAW_AMOUNT]


class TokenTransferTable(LedgerTable):
"""
Token transfers ledger data table
Expand Down Expand Up @@ -788,6 +840,54 @@ def bigint_cols(self) -> List[ColumnField]:
return [self.RAW_AMOUNT]


class NFTTransferTable(LedgerTable):
"""
NFT transfers ledger data table
Column names
"""

TOKEN_ADDRESS = ColumnField("token_address")
""""""
TOKEN_ID = ColumnField("token_address")
""""""
FROM_ADDRESS = ColumnField("from_address")
""""""
TO_ADDRESS = ColumnField("to_address")
""""""
RAW_AMOUNT = ColumnField("raw_amount")
""""""
TRANSACTION_HASH = ColumnField("transaction_hash")
""""""
LOG_INDEX = ColumnField("log_index")
""""""
BLOCK_HASH = ColumnField("block_hash")
""""""
BLOCK_NUMBER = ColumnField("block_number")
""""""
BLOCK_TIMESTAMP = ColumnField("block_timestamp")
""""""

TYPE_MAPPER = {
"token_address": "str",
"token_id": "str",
"from_address": "str",
"to_address": "str",
"raw_amount": "int",
"transaction_hash": "str",
"log_index": "int",
"block_hash": "str",
"block_number": "int",
"block_timestamp": "str",
}

def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)

@property
def bigint_cols(self) -> List[ColumnField]:
return [self.RAW_AMOUNT]


class TraceTable(LedgerTable):
"""
Trace ledger data table
Expand Down

0 comments on commit d74374c

Please sign in to comment.