adjustPercentageAmounts adjusts percentage amounts to maintain the correct total
(state *distributionState)
| 232 | |
| 233 | // adjustPercentageAmounts adjusts percentage amounts to maintain the correct total |
| 234 | func adjustPercentageAmounts(state *distributionState) { |
| 235 | if len(state.percentAmounts) == 0 { |
| 236 | return |
| 237 | } |
| 238 | |
| 239 | hundredDec := decimal.NewFromInt(100) |
| 240 | targetTotal := state.totalAmountDec.Mul(state.totalPercentage).Div(hundredDec) |
| 241 | |
| 242 | currentTotal := decimal.Zero |
| 243 | for _, amount := range state.percentAmounts { |
| 244 | currentTotal = currentTotal.Add(amount) |
| 245 | } |
| 246 | |
| 247 | diff := targetTotal.Sub(currentTotal) |
| 248 | if diff.Abs().Cmp(decimal.NewFromInt(0)) <= 0 { |
| 249 | return |
| 250 | } |
| 251 | |
| 252 | largestKey, largestAmount := findLargestDecimal(state.percentAmounts) |
| 253 | if largestKey != "" && largestAmount.Sign() > 0 { |
| 254 | state.percentAmounts[largestKey] = state.percentAmounts[largestKey].Add(diff) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // findLargestDecimal finds the key with the largest decimal value |
| 259 | func findLargestDecimal(amounts map[string]decimal.Decimal) (string, decimal.Decimal) { |
no test coverage detected