Skip to content

Commit

Permalink
further accelerate, supporting 50 simultaneous positions
Browse files Browse the repository at this point in the history
  • Loading branch information
faddat committed Oct 28, 2024
1 parent 33abf49 commit 9c0ce57
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
30 changes: 19 additions & 11 deletions lib/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9c0ce57

Please sign in to comment.