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

Function balanceDistributions

model/transaction.go:301–324  ·  view source on GitHub ↗

balanceDistributions ensures the sum of all distributions equals the total amount

(state *distributionState)

Source from the content-addressed store, hash-verified

299
300// balanceDistributions ensures the sum of all distributions equals the total amount
301func balanceDistributions(state *distributionState) {
302 sumDec := decimal.Zero
303 for _, amount := range state.result {
304 sumDec = sumDec.Add(decimal.NewFromBigInt(amount, 0))
305 }
306
307 if sumDec.Cmp(state.totalAmountDec) == 0 {
308 return
309 }
310
311 var largestKey string
312 largestAmount := big.NewInt(0)
313 for id, amount := range state.result {
314 if amount.Cmp(largestAmount) > 0 {
315 largestAmount = amount
316 largestKey = id
317 }
318 }
319
320 if largestKey != "" {
321 diff := state.totalAmountDec.Sub(sumDec)
322 state.result[largestKey] = new(big.Int).Add(state.result[largestKey], diff.BigInt())
323 }
324}
325
326// CalculateDistributionsPrecise calculates distributions using big.Int for precision
327func CalculateDistributionsPrecise(ctx context.Context, totalPreciseAmount *big.Int, distributions []Distribution, precision int64) (map[string]*big.Int, error) {

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected