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

Function processPercentageDistributions

model/transaction.go:195–222  ·  view source on GitHub ↗

processPercentageDistributions handles the second pass: percentage distributions

(distributions []Distribution, state *distributionState)

Source from the content-addressed store, hash-verified

193
194// processPercentageDistributions handles the second pass: percentage distributions
195func processPercentageDistributions(distributions []Distribution, state *distributionState) error {
196 hundredDec := decimal.NewFromInt(100)
197 minValueDec := decimal.NewFromInt(1)
198
199 for _, dist := range distributions {
200 if dist.isLeftDistribution() || !dist.isPercentageDistribution() {
201 continue
202 }
203
204 percentageStr := dist.Distribution[:len(dist.Distribution)-1]
205 percentage, err := strconv.ParseFloat(percentageStr, 64)
206 if err != nil {
207 return errors.New("invalid percentage format")
208 }
209
210 percentageDec := decimal.NewFromFloat(percentage)
211 state.totalPercentage = state.totalPercentage.Add(percentageDec)
212
213 rawAmountDec := state.totalAmountDec.Mul(percentageDec).Div(hundredDec)
214
215 if rawAmountDec.Sign() > 0 && rawAmountDec.Cmp(minValueDec) < 0 {
216 state.percentAmounts[dist.Identifier] = decimal.Zero
217 } else {
218 state.percentAmounts[dist.Identifier] = rawAmountDec
219 }
220 }
221 return nil
222}
223
224// validateDistributions checks that total distributions don't exceed limits
225func validateDistributions(state *distributionState) error {

Callers 1

Calls 2

isLeftDistributionMethod · 0.80

Tested by

no test coverage detected