Skip to content

Commit

Permalink
check division by zero and handle default values for balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 committed Oct 23, 2024
1 parent b6fedbe commit 0ba222a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/move/keeper/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (k BalancerKeeper) poolBalances(ctx context.Context, metadataLP vmtypes.Acc
})

if err != nil && errors.Is(err, collections.ErrNotFound) {
return nil, nil
return []math.Int{math.ZeroInt(), math.ZeroInt()}, nil

Check warning on line 231 in x/move/keeper/balancer.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/balancer.go#L231

Added line #L231 was not covered by tests
} else if err != nil {
return nil, err
}

Check warning on line 234 in x/move/keeper/balancer.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/balancer.go#L233-L234

Added lines #L233 - L234 were not covered by tests
Expand Down Expand Up @@ -269,7 +269,7 @@ func (k BalancerKeeper) poolWeights(
TypeArgs: []vmtypes.TypeTag{},
})
if err != nil && errors.Is(err, collections.ErrNotFound) {
return nil, nil
return []math.LegacyDec{math.LegacyZeroDec(), math.LegacyZeroDec()}, nil

Check warning on line 272 in x/move/keeper/balancer.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/balancer.go#L272

Added line #L272 was not covered by tests
} else if err != nil {
return nil, err
}

Check warning on line 275 in x/move/keeper/balancer.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/balancer.go#L274-L275

Added lines #L274 - L275 were not covered by tests
Expand Down
7 changes: 7 additions & 0 deletions x/move/keeper/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/pkg/errors"

distrtypes "github.com/initia-labs/initia/x/distribution/types"
"github.com/initia-labs/initia/x/move/types"
Expand Down Expand Up @@ -194,6 +195,9 @@ func (k DexKeeper) GasPrices(
if err != nil {
return true, err
}

Check warning on line 197 in x/move/keeper/dex.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/dex.go#L196-L197

Added lines #L196 - L197 were not covered by tests
if baseSpotPrice.IsZero() {
return true, errors.New("baseSpotPrice is zero")
}

Check warning on line 200 in x/move/keeper/dex.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/dex.go#L199-L200

Added lines #L199 - L200 were not covered by tests

gasPrice := baseGasPrice.Quo(baseSpotPrice)
gasPrices = gasPrices.Add(sdk.NewDecCoinFromDec(denomQuote, gasPrice))
Expand All @@ -220,6 +224,9 @@ func (k DexKeeper) GasPrice(
if err != nil {
return sdk.NewDecCoin(denomQuote, math.ZeroInt()), err

Check warning on line 225 in x/move/keeper/dex.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/dex.go#L225

Added line #L225 was not covered by tests
}
if baseSpotPrice.IsZero() {
return sdk.NewDecCoin(denomQuote, math.ZeroInt()), errors.New("baseSpotPrice is zero")

Check warning on line 228 in x/move/keeper/dex.go

View check run for this annotation

Codecov / codecov/patch

x/move/keeper/dex.go#L228

Added line #L228 was not covered by tests
}

return sdk.NewDecCoinFromDec(denomQuote, baseGasPrice.Quo(baseSpotPrice)), nil
}
Expand Down
4 changes: 4 additions & 0 deletions x/move/types/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ func GetBaseSpotPrice(
balanceBase, balanceQuote math.Int,
weightBase, weightQuote math.LegacyDec,
) math.LegacyDec {
if balanceBase.IsZero() || balanceQuote.IsZero() {
return math.LegacyZeroDec()
}

Check warning on line 436 in x/move/types/connector.go

View check run for this annotation

Codecov / codecov/patch

x/move/types/connector.go#L435-L436

Added lines #L435 - L436 were not covered by tests

numerator := weightQuote.MulInt(balanceBase)
denominator := weightBase.MulInt(balanceQuote)

Expand Down

0 comments on commit 0ba222a

Please sign in to comment.