MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / applyTransaction

Function applyTransaction

database/balance.go:1518–1540  ·  view source on GitHub ↗

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,
)

Source from the content-addressed store, hash-verified

1516
1517// applyTransaction applies a single transaction to update balance totals
1518func 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
1543func (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) {

Calls 1

NewAPIErrorFunction · 0.92

Tested by 3