Skip to content

Commit c63d6b3

Browse files
authored
feat: Add node_id execution_id and price in the historical credit exp… (#867)
* feat: Add node_id execution_id and price in the historical credit expense entries * fix: swap expense node_id and execution_id order and lint fixes
1 parent 9e86249 commit c63d6b3

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ classifiers = [
1717
"Programming Language :: Python",
1818
"Programming Language :: Python :: 3 :: Only",
1919
"Programming Language :: Python :: 3.12",
20+
"Programming Language :: Python :: 3.13",
2021
]
2122
dynamic = [ "version" ]
2223
dependencies = [

src/aleph/db/accessors/balances.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,12 @@ def update_credit_balances_expense(
497497
"""
498498
Updates credit balances for expense messages (aleph_credit_expense).
499499
500-
Expense messages have negative amounts and only include origin_ref field.
501-
Other fields like ratio, tx_hash, provider, payment_method, token, chain,
502-
origin, and expiration_date are not present.
500+
Expense messages have negative amounts and can include:
501+
- execution_id (mapped to origin)
502+
- node_id (mapped to tx_hash)
503+
- price (mapped to ratio)
504+
- time (skipped for now)
505+
- ref (mapped to origin_ref)
503506
"""
504507

505508
last_update = utc_now()
@@ -510,8 +513,14 @@ def update_credit_balances_expense(
510513
amount = -abs(int(credit_entry["amount"]))
511514
origin_ref = credit_entry.get("ref", "")
512515

516+
# Map new fields
517+
origin = credit_entry.get("execution_id", "")
518+
tx_hash = credit_entry.get("node_id", "")
519+
ratio = credit_entry.get("price", "")
520+
# Skip time field for now
521+
513522
csv_rows.append(
514-
f"{address};{amount};{message_hash};{index};{message_timestamp};{last_update};;;;;;;ALEPH;{origin_ref};credit_expense"
523+
f"{address};{amount};{message_hash};{index};{message_timestamp};{last_update};{ratio};{tx_hash};;;;{origin};ALEPH;{origin_ref};credit_expense"
515524
)
516525

517526
_bulk_insert_credit_history(session, csv_rows)

tests/db/test_credit_balances.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ def test_update_credit_balances_expense(session_factory: DbSessionFactory):
103103
assert expense_record.token is None
104104
assert expense_record.chain is None
105105
assert expense_record.provider == "ALEPH"
106+
assert expense_record.origin is None
106107
assert expense_record.origin_ref == "expense_ref"
107108
assert expense_record.payment_method == "credit_expense"
108109
assert expense_record.credit_ref == "expense_msg_789"
@@ -111,6 +112,57 @@ def test_update_credit_balances_expense(session_factory: DbSessionFactory):
111112
assert expense_record.message_timestamp == message_timestamp
112113

113114

115+
def test_update_credit_balances_expense_with_new_fields(
116+
session_factory: DbSessionFactory,
117+
):
118+
"""Test direct database insertion for credit expense messages with new fields."""
119+
credits_list = [
120+
{
121+
"address": "0x456",
122+
"amount": 500,
123+
"ref": "expense_ref",
124+
"execution_id": "exec_12345",
125+
"node_id": "node_67890",
126+
"price": "0.001",
127+
"time": 1640995200000, # This will be skipped for now
128+
}
129+
]
130+
131+
message_timestamp = dt.datetime(2023, 1, 2, 12, 0, 0, tzinfo=dt.timezone.utc)
132+
133+
with session_factory() as session:
134+
update_credit_balances_expense(
135+
session=session,
136+
credits_list=credits_list,
137+
message_hash="expense_msg_with_fields_789",
138+
message_timestamp=message_timestamp,
139+
)
140+
session.commit()
141+
142+
# Verify expense record was inserted with new field mappings
143+
expense_record = (
144+
session.query(AlephCreditHistoryDb)
145+
.filter_by(address="0x456", credit_ref="expense_msg_with_fields_789")
146+
.first()
147+
)
148+
149+
assert expense_record is not None
150+
assert expense_record.address == "0x456"
151+
assert expense_record.amount == -500
152+
assert expense_record.ratio == Decimal("0.001") # price mapped to ratio
153+
assert expense_record.tx_hash == "node_67890" # node_id mapped to tx_hash
154+
assert expense_record.token is None
155+
assert expense_record.chain is None
156+
assert expense_record.provider == "ALEPH"
157+
assert expense_record.origin == "exec_12345" # execution_id mapped to origin
158+
assert expense_record.origin_ref == "expense_ref"
159+
assert expense_record.payment_method == "credit_expense"
160+
assert expense_record.credit_ref == "expense_msg_with_fields_789"
161+
assert expense_record.credit_index == 0
162+
assert expense_record.expiration_date is None
163+
assert expense_record.message_timestamp == message_timestamp
164+
165+
114166
def test_update_credit_balances_transfer(session_factory: DbSessionFactory):
115167
"""Test direct database insertion for credit transfer messages."""
116168
credits_list = [

0 commit comments

Comments
 (0)