From d74374c68dba957559dd58350be7693ee273aa98 Mon Sep 17 00:00:00 2001 From: v1nvn Date: Wed, 16 Oct 2024 08:07:49 +0530 Subject: [PATCH] feat: nft transfer and balance ledgers --- credmark/cmf/model/ledger/__init__.py | 90 ++++++++++++++--------- credmark/cmf/types/ledger.py | 100 ++++++++++++++++++++++++++ 2 files changed, 156 insertions(+), 34 deletions(-) diff --git a/credmark/cmf/model/ledger/__init__.py b/credmark/cmf/model/ledger/__init__.py index 7530190..d014c39 100644 --- a/credmark/cmf/model/ledger/__init__.py +++ b/credmark/cmf/model/ledger/__init__.py @@ -5,6 +5,8 @@ ContractTable, LedgerTable, LogTable, + NFTBalanceTable, + NFTTransferTable, ReceiptTable, TokenBalanceTable, TokenTable, @@ -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 @@ -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 @@ -131,6 +142,7 @@ class Ledger: The query parameters are common to all query methods. """ + # pylint: disable=locally-disabled,invalid-name def __init__(self): @@ -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: """ @@ -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=[], + ) diff --git a/credmark/cmf/types/ledger.py b/credmark/cmf/types/ledger.py index 1eae59f..e0573e4 100644 --- a/credmark/cmf/types/ledger.py +++ b/credmark/cmf/types/ledger.py @@ -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 @@ -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