01 Refactor currency handling to store amounts in GTQ: Removed currency conversion utilities, updated models and API endpoints to directly handle GTQ amounts, and modified transaction processing logic for consistency. Enhanced frontend to reflect these changes, ensuring accurate display and submission of GTQ values across the application.
Refactor GTQ storage migration: Moved the conversion logic for centavo amounts to GTQ into a new migration function, m004_convert_to_gtq_storage, ensuring proper data type changes and updates across relevant tables. This enhances clarity and maintains the integrity of the migration process.
This commit is contained in:
parent
aa71321c84
commit
c83ebf43ab
8 changed files with 157 additions and 162 deletions
|
|
@ -671,7 +671,7 @@ class LamassuTransactionProcessor:
|
|||
# Since crypto_atoms already includes commission, we need to extract the base amount
|
||||
# Formula: crypto_atoms = base_amount * (1 + effective_commission)
|
||||
# Therefore: base_amount = crypto_atoms / (1 + effective_commission)
|
||||
base_crypto_atoms = int(crypto_atoms / (1 + effective_commission))
|
||||
base_crypto_atoms = round(crypto_atoms / (1 + effective_commission))
|
||||
commission_amount_sats = crypto_atoms - base_crypto_atoms
|
||||
else:
|
||||
effective_commission = 0.0
|
||||
|
|
@ -755,9 +755,8 @@ class LamassuTransactionProcessor:
|
|||
client_sats_amount = calc['allocated_sats']
|
||||
proportion = calc['proportion']
|
||||
|
||||
# Calculate equivalent fiat value in centavos for tracking purposes (industry standard)
|
||||
# Store as centavos to maintain precision and avoid floating-point errors
|
||||
client_fiat_amount = round(client_sats_amount * 100 / exchange_rate) if exchange_rate > 0 else 0
|
||||
# Calculate equivalent fiat value in GTQ for tracking purposes
|
||||
client_fiat_amount = round(client_sats_amount / exchange_rate, 2) if exchange_rate > 0 else 0.0
|
||||
|
||||
distributions[client_id] = {
|
||||
"fiat_amount": client_fiat_amount,
|
||||
|
|
@ -765,7 +764,7 @@ class LamassuTransactionProcessor:
|
|||
"exchange_rate": exchange_rate
|
||||
}
|
||||
|
||||
logger.info(f"Client {client_id[:8]}... gets {client_sats_amount} sats (≈{client_fiat_amount/100:.2f} GTQ, {proportion:.2%} share)")
|
||||
logger.info(f"Client {client_id[:8]}... gets {client_sats_amount} sats (≈{client_fiat_amount:.2f} GTQ, {proportion:.2%} share)")
|
||||
|
||||
# Verification: ensure total distribution equals base amount
|
||||
total_distributed = sum(dist["sats_amount"] for dist in distributions.values())
|
||||
|
|
@ -781,9 +780,9 @@ class LamassuTransactionProcessor:
|
|||
current_balance = await get_client_balance_summary(client_id)
|
||||
if current_balance.remaining_balance > 0:
|
||||
final_distributions[client_id] = distribution
|
||||
logger.info(f"Client {client_id[:8]}... final balance check: {current_balance.remaining_balance} centavos - APPROVED for {distribution['sats_amount']} sats")
|
||||
logger.info(f"Client {client_id[:8]}... final balance check: {current_balance.remaining_balance:.2f} GTQ - APPROVED for {distribution['sats_amount']} sats")
|
||||
else:
|
||||
logger.warning(f"Client {client_id[:8]}... final balance check: {current_balance.remaining_balance} centavos - REJECTED (negative balance)")
|
||||
logger.warning(f"Client {client_id[:8]}... final balance check: {current_balance.remaining_balance:.2f} GTQ - REJECTED (negative balance)")
|
||||
|
||||
if len(final_distributions) != len(distributions):
|
||||
logger.warning(f"Rejected {len(distributions) - len(final_distributions)} clients due to negative balances during final check")
|
||||
|
|
@ -830,22 +829,22 @@ class LamassuTransactionProcessor:
|
|||
# Final safety check: Verify client still has positive balance before payment
|
||||
current_balance = await get_client_balance_summary(client_id)
|
||||
if current_balance.remaining_balance <= 0:
|
||||
logger.error(f"CRITICAL: Client {client_id[:8]}... has negative balance ({current_balance.remaining_balance} centavos) - REFUSING payment of {distribution['sats_amount']} sats")
|
||||
logger.error(f"CRITICAL: Client {client_id[:8]}... has negative balance ({current_balance.remaining_balance:.2f} GTQ) - REFUSING payment of {distribution['sats_amount']} sats")
|
||||
continue
|
||||
|
||||
# Verify balance is sufficient for this distribution
|
||||
fiat_equivalent = distribution["fiat_amount"] # Already in centavos
|
||||
fiat_equivalent = distribution["fiat_amount"] # Amount in GTQ
|
||||
if current_balance.remaining_balance < fiat_equivalent:
|
||||
logger.error(f"CRITICAL: Client {client_id[:8]}... insufficient balance ({current_balance.remaining_balance} < {fiat_equivalent} centavos) - REFUSING payment")
|
||||
logger.error(f"CRITICAL: Client {client_id[:8]}... insufficient balance ({current_balance.remaining_balance:.2f} < {fiat_equivalent:.2f} GTQ) - REFUSING payment")
|
||||
continue
|
||||
|
||||
logger.info(f"Client {client_id[:8]}... pre-payment balance check: {current_balance.remaining_balance} centavos - SUFFICIENT for {fiat_equivalent} centavos payment")
|
||||
logger.info(f"Client {client_id[:8]}... pre-payment balance check: {current_balance.remaining_balance:.2f} GTQ - SUFFICIENT for {fiat_equivalent:.2f} GTQ payment")
|
||||
|
||||
# Create DCA payment record
|
||||
payment_data = CreateDcaPaymentData(
|
||||
client_id=client_id,
|
||||
amount_sats=distribution["sats_amount"],
|
||||
amount_fiat=distribution["fiat_amount"], # Still store centavos in DB
|
||||
amount_fiat=distribution["fiat_amount"], # Amount in GTQ
|
||||
exchange_rate=distribution["exchange_rate"],
|
||||
transaction_type="flow",
|
||||
lamassu_transaction_id=transaction_id,
|
||||
|
|
@ -888,12 +887,9 @@ class LamassuTransactionProcessor:
|
|||
return False
|
||||
|
||||
# Create descriptive memo with DCA metrics
|
||||
fiat_amount_centavos = distribution.get("fiat_amount", 0)
|
||||
fiat_amount_gtq = distribution.get("fiat_amount", 0.0)
|
||||
exchange_rate = distribution.get("exchange_rate", 0)
|
||||
|
||||
# Convert centavos to GTQ for display
|
||||
fiat_amount_gtq = fiat_amount_centavos / 100
|
||||
|
||||
# Calculate cost basis (fiat per BTC)
|
||||
if exchange_rate > 0:
|
||||
# exchange_rate is sats per fiat unit, so convert to fiat per BTC
|
||||
|
|
@ -1011,7 +1007,7 @@ class LamassuTransactionProcessor:
|
|||
# Calculate commission metrics
|
||||
if commission_percentage > 0:
|
||||
effective_commission = commission_percentage * (100 - discount) / 100
|
||||
base_crypto_atoms = int(crypto_atoms / (1 + effective_commission))
|
||||
base_crypto_atoms = round(crypto_atoms / (1 + effective_commission))
|
||||
commission_amount_sats = crypto_atoms - base_crypto_atoms
|
||||
else:
|
||||
effective_commission = 0.0
|
||||
|
|
@ -1021,10 +1017,10 @@ class LamassuTransactionProcessor:
|
|||
# Calculate exchange rate
|
||||
exchange_rate = base_crypto_atoms / fiat_amount if fiat_amount > 0 else 0
|
||||
|
||||
# Create transaction data (store fiat_amount in centavos for consistency)
|
||||
# Create transaction data with GTQ amounts
|
||||
transaction_data = CreateLamassuTransactionData(
|
||||
lamassu_transaction_id=transaction["transaction_id"],
|
||||
fiat_amount=int(fiat_amount * 100), # Convert GTQ to centavos
|
||||
fiat_amount=round(fiat_amount, 2), # Store GTQ with 2 decimal places
|
||||
crypto_amount=crypto_atoms,
|
||||
commission_percentage=commission_percentage,
|
||||
discount=discount,
|
||||
|
|
@ -1137,7 +1133,7 @@ class LamassuTransactionProcessor:
|
|||
|
||||
if commission_percentage and commission_percentage > 0:
|
||||
effective_commission = commission_percentage * (100 - discount) / 100
|
||||
base_crypto_atoms = int(crypto_atoms / (1 + effective_commission))
|
||||
base_crypto_atoms = round(crypto_atoms / (1 + effective_commission))
|
||||
commission_amount_sats = crypto_atoms - base_crypto_atoms
|
||||
else:
|
||||
commission_amount_sats = 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue