balanceDistributions ensures the sum of all distributions equals the total amount
(state *distributionState)
| 299 | |
| 300 | // balanceDistributions ensures the sum of all distributions equals the total amount |
| 301 | func 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 |
| 327 | func CalculateDistributionsPrecise(ctx context.Context, totalPreciseAmount *big.Int, distributions []Distribution, precision int64) (map[string]*big.Int, error) { |
no outgoing calls
no test coverage detected