diff --git a/lib/lib.go b/lib/lib.go index 2ab48d2..995b073 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -202,16 +202,17 @@ func CheckBalancesWithinThreshold(balances map[string]sdkmath.Int, threshold flo return false } - // Initialize minBalance and maxBalance to the first balance in the map var minBalance, maxBalance sdkmath.Int - initialized := false + first := true + for _, balance := range balances { - if !initialized { + if first { minBalance = balance maxBalance = balance - initialized = true + first = false continue } + if balance.LT(minBalance) { minBalance = balance } @@ -220,16 +221,23 @@ func CheckBalancesWithinThreshold(balances map[string]sdkmath.Int, threshold flo } } - // Proceed with your calculations - diff := maxBalance.Sub(minBalance) - avg := maxBalance.Add(minBalance).Quo(sdkmath.NewInt(2)) + // Skip check if all balances are below minimum threshold + minThreshold := sdkmath.NewInt(1000000) // 1 token assuming 6 decimals + if maxBalance.LT(minThreshold) { + return true + } - percentageDiff := diff.ToLegacyDec().Quo(avg.ToLegacyDec()) + // Calculate the difference as a percentage of the max balance + if maxBalance.IsZero() { + return minBalance.IsZero() + } - // Correctly convert the threshold to sdkmath.Dec without losing precision - thresholdDec := sdkmath.LegacyMustNewDecFromStr(fmt.Sprintf("%f", threshold)) + diff := maxBalance.Sub(minBalance) + diffFloat := float64(diff.Int64()) + maxFloat := float64(maxBalance.Int64()) - return percentageDiff.LTE(thresholdDec) + percentage := diffFloat / maxFloat + return percentage <= threshold } // Function to extract the expected sequence number from the error message diff --git a/main.go b/main.go index 358df96..612e8fb 100644 --- a/main.go +++ b/main.go @@ -102,19 +102,27 @@ func main() { log.Fatalf("Failed to get balances after adjustment: %v", err) } - if lib.CheckBalancesWithinThreshold(balances, 0.10) { + // Final balance check with more lenient threshold for dust amounts + if lib.CheckBalancesWithinThreshold(balances, 0.15) { // 15% threshold for final check + fmt.Println("Balances successfully adjusted within acceptable range") return } - totalBalance := sdkmath.ZeroInt() + // Only fail if we have significant balances that are still out of range + var maxBalance sdkmath.Int for _, balance := range balances { - totalBalance = totalBalance.Add(balance) + if balance.GT(maxBalance) { + maxBalance = balance + } } - if totalBalance.IsZero() { - fmt.Println("All accounts have zero balance. Proceeding without adjusting balances.") + + minSignificantBalance := sdkmath.NewInt(1000000) // 1 token assuming 6 decimals + if maxBalance.LT(minSignificantBalance) { + fmt.Println("Remaining balance differences are below minimum threshold, proceeding") return } - log.Fatalf("Account balances are still not within 10%% of each other after adjustment") + + log.Fatalf("Account balances are still not within threshold after adjustment") } nodeURL := config.Nodes.RPC[0] // Use the first node