applyTransaction applies a single transaction to update balance totals
(txn struct {
PreciseAmount string
Source string
Destination string
CreatedAt time.Time
EffectiveDate time.Time
}, balanceID string, creditBalance, debitBalance *big.Int,
)
| 1516 | |
| 1517 | // applyTransaction applies a single transaction to update balance totals |
| 1518 | func applyTransaction(txn struct { |
| 1519 | PreciseAmount string |
| 1520 | Source string |
| 1521 | Destination string |
| 1522 | CreatedAt time.Time |
| 1523 | EffectiveDate time.Time |
| 1524 | }, balanceID string, creditBalance, debitBalance *big.Int, |
| 1525 | ) (*big.Int, *big.Int, error) { |
| 1526 | txAmount, ok := new(big.Int).SetString(txn.PreciseAmount, 10) |
| 1527 | if !ok { |
| 1528 | return nil, nil, apierror.NewAPIError(apierror.ErrInternalServer, "Invalid transaction amount", nil) |
| 1529 | } |
| 1530 | |
| 1531 | // Use the transaction amount to update the appropriate balance |
| 1532 | if txn.Source == balanceID { |
| 1533 | debitBalance = new(big.Int).Add(debitBalance, txAmount) |
| 1534 | } |
| 1535 | if txn.Destination == balanceID { |
| 1536 | creditBalance = new(big.Int).Add(creditBalance, txAmount) |
| 1537 | } |
| 1538 | |
| 1539 | return creditBalance, debitBalance, nil |
| 1540 | } |
| 1541 | |
| 1542 | // calculateBalanceFromTransactions applies transactions to calculate the balance at a specific time |
| 1543 | func (d Datasource) calculateBalanceFromTransactions(ctx context.Context, tx *sql.Tx, balanceID string, startTime time.Time, targetTime time.Time, initialCredit, initialDebit *big.Int) (creditBalance, debitBalance *big.Int, err error) { |