UpdateBalances updates the balances for both the source and destination based on the transaction details. It ensures precision is applied and checks for overdraft before updating.
(transaction *Transaction, source, destination *Balance)
| 419 | // UpdateBalances updates the balances for both the source and destination based on the transaction details. |
| 420 | // It ensures precision is applied and checks for overdraft before updating. |
| 421 | func UpdateBalances(transaction *Transaction, source, destination *Balance) error { |
| 422 | // Apply precision to get precise amount |
| 423 | transaction.PreciseAmount = ApplyPrecision(transaction) |
| 424 | err := transaction.validate() |
| 425 | if err != nil { |
| 426 | return err |
| 427 | } |
| 428 | |
| 429 | // Check if source has sufficient funds |
| 430 | err = canProcessTransaction(transaction, source) |
| 431 | if err != nil { |
| 432 | return err |
| 433 | } |
| 434 | |
| 435 | source.InitializeBalanceFields() |
| 436 | destination.InitializeBalanceFields() |
| 437 | |
| 438 | // Update source balance with original precise amount |
| 439 | source.addDebit(transaction.PreciseAmount, transaction.Inflight) |
| 440 | source.computeBalance(transaction.Inflight) |
| 441 | |
| 442 | // Calculate destination amount with rate |
| 443 | destinationAmount := ApplyRate(transaction.PreciseAmount, transaction.Rate) |
| 444 | |
| 445 | // Update destination balance with rate-adjusted amount |
| 446 | destination.addCredit(destinationAmount, transaction.Inflight) |
| 447 | destination.computeBalance(transaction.Inflight) |
| 448 | |
| 449 | return nil |
| 450 | } |
| 451 | |
| 452 | // CheckCondition checks if a balance meets the condition specified by a BalanceMonitor. |
| 453 | // It compares various balance fields (e.g., debit balance, credit balance) against the precise value. |