Skip to content

Commit

Permalink
write partner fees in db
Browse files Browse the repository at this point in the history
  • Loading branch information
harisang committed Sep 5, 2024
1 parent 60f4842 commit f56c889
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
9 changes: 8 additions & 1 deletion src/helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,22 +97,29 @@ def write_fees(
token_address: str,
fee_amount: float,
fee_type: str,
recipient: str,
):
"""Function attempts to write price data to the table."""
tx_hash_bytes = bytes.fromhex(tx_hash[2:])
token_address_bytes = bytes.fromhex(token_address[2:])
order_uid_bytes = bytes.fromhex(order_uid[2:])

query = read_sql_file("src/sql/insert_fee.sql")
final_recipient = None
if recipient != "":
final_recipient = bytes.fromhex(recipient[2:])

self.execute_and_commit(
query,
{
"chain_name": self.chain_name,
"chain_name": chain_name,
"auction_id": auction_id,
"block_number": block_number,
"tx_hash": tx_hash_bytes,
"order_uid": order_uid_bytes,
"token_address": token_address_bytes,
"fee_amount": fee_amount,
"fee_type": fee_type,
"recipient": final_recipient,
},
)
47 changes: 40 additions & 7 deletions src/transaction_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,11 @@ def process_single_transaction(

# Compute Fees
if self.process_fees:
protocol_fees, network_fees = self.process_fees_for_transaction(
tx_hash, auction_id, block_number
)
(
protocol_fees,
partner_fees,
network_fees,
) = self.process_fees_for_transaction(tx_hash)

# Compute Prices
if self.process_prices:
Expand All @@ -134,7 +136,12 @@ def process_single_transaction(

if self.process_fees:
self.handle_fees(
protocol_fees, network_fees, auction_id, block_number, tx_hash
protocol_fees,
partner_fees,
network_fees,
auction_id,
block_number,
tx_hash,
)

if self.process_prices and prices:
Expand Down Expand Up @@ -162,14 +169,19 @@ def process_token_imbalances(
return {}

def process_fees_for_transaction(
self, tx_hash: str, auction_id: int, block_number: int
) -> tuple[dict[str, tuple[str, int]], dict[str, tuple[str, int]]]:
self,
tx_hash: str,
) -> tuple[
dict[str, tuple[str, int]],
dict[str, tuple[str, int, str]],
dict[str, tuple[str, int]],
]:
"""Process and return protocol and network fees for a given transaction."""
try:
protocol_fees, partner_fees, network_fees = compute_all_fees_of_batch(
HexBytes(tx_hash)
)
return protocol_fees, network_fees
return protocol_fees, partner_fees, network_fees
except Exception as e:
logger.error(f"Failed to process fees for transaction {tx_hash}: {e}")
return {}, {}
Expand Down Expand Up @@ -226,6 +238,7 @@ def handle_imbalances(
def handle_fees(
self,
protocol_fees: dict[str, tuple[str, int]],
partner_fees: dict[str, tuple[str, int, str]],
network_fees: dict[str, tuple[str, int]],
auction_id: int,
block_number: int,
Expand All @@ -244,6 +257,25 @@ def handle_fees(
token_address=token_address,
fee_amount=float(fee_amount),
fee_type="protocol",
recipient="",
)

# Write partner fees
for order_uid, (
token_address,
fee_amount,
recipient,
) in partner_fees.items():
self.db.write_fees(
chain_name=self.chain_name,
auction_id=auction_id,
block_number=block_number,
tx_hash=tx_hash,
order_uid=order_uid,
token_address=token_address,
fee_amount=float(fee_amount),
fee_type="partner",
recipient=recipient,
)

# Write network fees
Expand All @@ -257,6 +289,7 @@ def handle_fees(
token_address=token_address,
fee_amount=float(fee_amount),
fee_type="network",
recipient="",
)
except Exception as err:
logger.error(
Expand Down

0 comments on commit f56c889

Please sign in to comment.