CalculateDistributionsPrecise calculates distributions using big.Int for precision
(ctx context.Context, totalPreciseAmount *big.Int, distributions []Distribution, precision int64)
| 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) { |
| 328 | _, span := tracer.Start(ctx, "CalculateDistributionsPrecise") |
| 329 | defer span.End() |
| 330 | |
| 331 | span.AddEvent("Starting precise distribution calculation", trace.WithAttributes( |
| 332 | attribute.String("total_precise_amount", totalPreciseAmount.String()), |
| 333 | attribute.Int("distribution.count", len(distributions)), |
| 334 | )) |
| 335 | |
| 336 | if totalPreciseAmount.Cmp(big.NewInt(0)) == 0 { |
| 337 | return handleZeroAmount(distributions), nil |
| 338 | } |
| 339 | |
| 340 | totalAmountDec := decimal.NewFromBigInt(totalPreciseAmount, 0) |
| 341 | |
| 342 | if result := handleSmallAmount(totalAmountDec, totalPreciseAmount, distributions); result != nil { |
| 343 | return result, nil |
| 344 | } |
| 345 | |
| 346 | state := &distributionState{ |
| 347 | totalAmountDec: totalAmountDec, |
| 348 | amountLeftDec: totalAmountDec, |
| 349 | precisionDec: decimal.NewFromInt(precision), |
| 350 | fixedAmounts: make(map[string]decimal.Decimal), |
| 351 | percentAmounts: make(map[string]decimal.Decimal), |
| 352 | result: make(map[string]*big.Int), |
| 353 | } |
| 354 | |
| 355 | if err := processFixedDistributions(distributions, state); err != nil { |
| 356 | span.RecordError(err) |
| 357 | return nil, err |
| 358 | } |
| 359 | |
| 360 | if err := processPercentageDistributions(distributions, state); err != nil { |
| 361 | span.RecordError(err) |
| 362 | return nil, err |
| 363 | } |
| 364 | |
| 365 | if err := validateDistributions(state); err != nil { |
| 366 | span.RecordError(err) |
| 367 | return nil, err |
| 368 | } |
| 369 | |
| 370 | adjustPercentageAmounts(state) |
| 371 | combineDistributions(state) |
| 372 | |
| 373 | if err := processLeftDistribution(distributions, state); err != nil { |
| 374 | span.RecordError(err) |
| 375 | return nil, err |
| 376 | } |
| 377 | |
| 378 | balanceDistributions(state) |
| 379 | |
| 380 | span.AddEvent("Precise distribution calculation completed") |
| 381 | return state.result, nil |
| 382 | } |
| 383 | |
| 384 | // handleZeroAmount returns zero amounts for all distributions when total is zero |