Skip to content

Commit

Permalink
fix: fix division by zero issue in v0.17.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmic-vagabond committed Nov 28, 2023
1 parent 39e3a81 commit 327fa29
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions x/margin/types/calc_funding_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ import (
func CalcFundingRate(longAmount, shortAmount sdk.Int, baseRate, maxRate, minRate sdk.Dec) sdk.Dec {
var ratio sdk.Dec

// Check for division by zero when longAmount > shortAmount
if longAmount.GT(shortAmount) {
if shortAmount.IsZero() {
// Handle the case where shortAmount is zero
return maxRate
}
ratio = sdk.NewDecFromInt(longAmount).Quo(sdk.NewDecFromInt(shortAmount))
return sdk.MinDec(sdk.MaxDec(baseRate.Mul(ratio), minRate), maxRate)
} else if shortAmount.GT(longAmount) {
if longAmount.IsZero() {
// Handle the case where longAmount is zero
return maxRate
}
ratio = sdk.NewDecFromInt(shortAmount).Quo(sdk.NewDecFromInt(longAmount))
return sdk.MinDec(sdk.MaxDec(baseRate.Mul(ratio).Neg(), minRate), maxRate)
} else {
Expand Down

0 comments on commit 327fa29

Please sign in to comment.