@@ -408,8 +408,8 @@ def _bulk_insert_credit_history(
408408 cursor = conn .cursor ()
409409
410410 # Column specification for credit history
411- # Include the new message_timestamp field
412- copy_columns = "address, amount, credit_ref, credit_index, message_timestamp, last_update, ratio , tx_hash, expiration_date, token, chain, origin, provider, origin_ref, payment_method"
411+ # Include the new message_timestamp and bonus_amount fields
412+ copy_columns = "address, amount, credit_ref, credit_index, message_timestamp, last_update, price, bonus_amount , tx_hash, expiration_date, token, chain, origin, provider, origin_ref, payment_method"
413413
414414 csv_credit_history = StringIO ("\n " .join (csv_rows ))
415415 cursor .copy_expert (
@@ -453,7 +453,7 @@ def update_credit_balances_distribution(
453453 """
454454 Updates credit balances for distribution messages (aleph_credit_distribution).
455455
456- Distribution messages include all fields like ratio , tx_hash, provider,
456+ Distribution messages include all fields like price , tx_hash, provider,
457457 payment_method, token, chain, and expiration_date.
458458 """
459459
@@ -463,7 +463,7 @@ def update_credit_balances_distribution(
463463 for index , credit_entry in enumerate (credits_list ):
464464 address = credit_entry ["address" ]
465465 amount = abs (int (credit_entry ["amount" ]))
466- ratio = Decimal (credit_entry ["ratio " ])
466+ price = Decimal (credit_entry ["price " ])
467467 tx_hash = credit_entry ["tx_hash" ]
468468 provider = credit_entry ["provider" ]
469469
@@ -472,6 +472,7 @@ def update_credit_balances_distribution(
472472 origin = credit_entry .get ("origin" , "" )
473473 origin_ref = credit_entry .get ("ref" , "" )
474474 payment_method = credit_entry .get ("payment_method" , "" )
475+ bonus_amount = credit_entry .get ("bonus_amount" , "" )
475476
476477 # Convert expiration timestamp to datetime
477478
@@ -482,7 +483,7 @@ def update_credit_balances_distribution(
482483 )
483484
484485 csv_rows .append (
485- f"{ address } ;{ amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;{ ratio } ;{ tx_hash } ;{ expiration_date or '' } ;{ token } ;{ chain } ;{ origin } ;{ provider } ;{ origin_ref } ;{ payment_method } "
486+ f"{ address } ;{ amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;{ price } ; { bonus_amount or '' } ;{ tx_hash } ;{ expiration_date or '' } ;{ token } ;{ chain } ;{ origin } ;{ provider } ;{ origin_ref } ;{ payment_method } "
486487 )
487488
488489 _bulk_insert_credit_history (session , csv_rows )
@@ -500,7 +501,7 @@ def update_credit_balances_expense(
500501 Expense messages have negative amounts and can include:
501502 - execution_id (mapped to origin)
502503 - node_id (mapped to tx_hash)
503- - price (mapped to ratio )
504+ - price (mapped to price )
504505 - time (skipped for now)
505506 - ref (mapped to origin_ref)
506507 """
@@ -516,11 +517,11 @@ def update_credit_balances_expense(
516517 # Map new fields
517518 origin = credit_entry .get ("execution_id" , "" )
518519 tx_hash = credit_entry .get ("node_id" , "" )
519- ratio = credit_entry .get ("price" , "" )
520+ price = credit_entry .get ("price" , "" )
520521 # Skip time field for now
521522
522523 csv_rows .append (
523- f"{ address } ;{ amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;{ ratio } ;{ tx_hash } ;;;;{ origin } ;ALEPH;{ origin_ref } ;credit_expense"
524+ f"{ address } ;{ amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;{ price } ; ;{ tx_hash } ;;;;{ origin } ;ALEPH;{ origin_ref } ;credit_expense"
524525 )
525526
526527 _bulk_insert_credit_history (session , csv_rows )
@@ -562,15 +563,15 @@ def update_credit_balances_transfer(
562563
563564 # Add positive entry for recipient (origin = sender, provider = ALEPH, payment_method = credit_transfer)
564565 csv_rows .append (
565- f"{ recipient_address } ;{ amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;;;{ expiration_date or '' } ;;;{ sender_address } ;ALEPH;;credit_transfer"
566+ f"{ recipient_address } ;{ amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;;;; { expiration_date or '' } ;;;{ sender_address } ;ALEPH;;credit_transfer"
566567 )
567568 index += 1
568569
569570 # Add negative entry for sender (unless sender is in whitelisted addresses)
570571 # (origin = recipient, provider = ALEPH, payment_method = credit_transfer)
571572 if sender_address not in whitelisted_addresses :
572573 csv_rows .append (
573- f"{ sender_address } ;{ - amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;;;;;;{ recipient_address } ;ALEPH;;credit_transfer"
574+ f"{ sender_address } ;{ - amount } ;{ message_hash } ;{ index } ;{ message_timestamp } ;{ last_update } ;;;;;;; { recipient_address } ;ALEPH;;credit_transfer"
574575 )
575576 index += 1
576577
@@ -602,6 +603,13 @@ def get_address_credit_history(
602603 address : str ,
603604 page : int = 1 ,
604605 pagination : int = 0 ,
606+ tx_hash : Optional [str ] = None ,
607+ token : Optional [str ] = None ,
608+ chain : Optional [str ] = None ,
609+ provider : Optional [str ] = None ,
610+ origin : Optional [str ] = None ,
611+ origin_ref : Optional [str ] = None ,
612+ payment_method : Optional [str ] = None ,
605613) -> Sequence [AlephCreditHistoryDb ]:
606614 """
607615 Get paginated credit history entries for a specific address, ordered from newest to oldest.
@@ -611,6 +619,13 @@ def get_address_credit_history(
611619 address: Address to get credit history for
612620 page: Page number (starts at 1)
613621 pagination: Number of entries per page (0 for all entries)
622+ tx_hash: Filter by transaction hash
623+ token: Filter by token
624+ chain: Filter by chain
625+ provider: Filter by provider
626+ origin: Filter by origin
627+ origin_ref: Filter by origin reference
628+ payment_method: Filter by payment method
614629
615630 Returns:
616631 List of credit history entries ordered by message_timestamp desc
@@ -621,6 +636,22 @@ def get_address_credit_history(
621636 .order_by (AlephCreditHistoryDb .message_timestamp .desc ())
622637 )
623638
639+ # Apply filters
640+ if tx_hash is not None :
641+ query = query .where (AlephCreditHistoryDb .tx_hash == tx_hash )
642+ if token is not None :
643+ query = query .where (AlephCreditHistoryDb .token == token )
644+ if chain is not None :
645+ query = query .where (AlephCreditHistoryDb .chain == chain )
646+ if provider is not None :
647+ query = query .where (AlephCreditHistoryDb .provider == provider )
648+ if origin is not None :
649+ query = query .where (AlephCreditHistoryDb .origin == origin )
650+ if origin_ref is not None :
651+ query = query .where (AlephCreditHistoryDb .origin_ref == origin_ref )
652+ if payment_method is not None :
653+ query = query .where (AlephCreditHistoryDb .payment_method == payment_method )
654+
624655 if pagination > 0 :
625656 query = query .offset ((page - 1 ) * pagination ).limit (pagination )
626657
@@ -630,21 +661,51 @@ def get_address_credit_history(
630661def count_address_credit_history (
631662 session : DbSession ,
632663 address : str ,
664+ tx_hash : Optional [str ] = None ,
665+ token : Optional [str ] = None ,
666+ chain : Optional [str ] = None ,
667+ provider : Optional [str ] = None ,
668+ origin : Optional [str ] = None ,
669+ origin_ref : Optional [str ] = None ,
670+ payment_method : Optional [str ] = None ,
633671) -> int :
634672 """
635- Count total credit history entries for a specific address.
673+ Count total credit history entries for a specific address with optional filters .
636674
637675 Args:
638676 session: Database session
639677 address: Address to count credit history for
678+ tx_hash: Filter by transaction hash
679+ token: Filter by token
680+ chain: Filter by chain
681+ provider: Filter by provider
682+ origin: Filter by origin
683+ origin_ref: Filter by origin reference
684+ payment_method: Filter by payment method
640685
641686 Returns:
642- Total number of credit history entries for the address
687+ Total number of credit history entries for the address matching the filters
643688 """
644689 query = select (func .count (AlephCreditHistoryDb .credit_ref )).where (
645690 AlephCreditHistoryDb .address == address
646691 )
647692
693+ # Apply filters
694+ if tx_hash is not None :
695+ query = query .where (AlephCreditHistoryDb .tx_hash == tx_hash )
696+ if token is not None :
697+ query = query .where (AlephCreditHistoryDb .token == token )
698+ if chain is not None :
699+ query = query .where (AlephCreditHistoryDb .chain == chain )
700+ if provider is not None :
701+ query = query .where (AlephCreditHistoryDb .provider == provider )
702+ if origin is not None :
703+ query = query .where (AlephCreditHistoryDb .origin == origin )
704+ if origin_ref is not None :
705+ query = query .where (AlephCreditHistoryDb .origin_ref == origin_ref )
706+ if payment_method is not None :
707+ query = query .where (AlephCreditHistoryDb .payment_method == payment_method )
708+
648709 return session .execute (query ).scalar_one ()
649710
650711
0 commit comments