diff --git a/x/precisebank/keeper/fractional_balance.go b/x/precisebank/keeper/fractional_balance.go index 636e383c..7f2c20c8 100644 --- a/x/precisebank/keeper/fractional_balance.go +++ b/x/precisebank/keeper/fractional_balance.go @@ -48,7 +48,7 @@ func (k *Keeper) SetFractionalBalance( // Ensure the fractional balance is valid before setting it. Use the // NewFractionalAmountFromInt wrapper to use its Validate() method. - if err := types.NewFractionalAmountFromInt(amount).Validate(); err != nil { + if err := types.ValidateFractionalAmount(amount); err != nil { panic(fmt.Errorf("amount is invalid: %w", err)) } diff --git a/x/precisebank/keeper/invariants.go b/x/precisebank/keeper/invariants.go index 66b62b73..1724eeb5 100644 --- a/x/precisebank/keeper/invariants.go +++ b/x/precisebank/keeper/invariants.go @@ -104,7 +104,7 @@ func ValidFractionalAmountsInvariant(k Keeper) sdk.Invariant { ) k.IterateFractionalBalances(ctx, func(addr sdk.AccAddress, amount sdkmath.Int) bool { - if err := types.NewFractionalAmountFromInt(amount).Validate(); err != nil { + if err := types.ValidateFractionalAmount(amount); err != nil { count++ msg += fmt.Sprintf("\t%s has an invalid fractional amount of %s\n", addr, amount) } @@ -133,7 +133,7 @@ func ValidRemainderAmountInvariant(k Keeper) sdk.Invariant { if !remainderAmount.IsZero() { // Only validate if non-zero, as zero is default value - if err := types.NewFractionalAmountFromInt(remainderAmount).Validate(); err != nil { + if err := types.ValidateFractionalAmount(remainderAmount); err != nil { broken = true msg = fmt.Sprintf("remainder amount is invalid: %s", err) } diff --git a/x/precisebank/keeper/remainder_amount.go b/x/precisebank/keeper/remainder_amount.go index 7ec2e2c7..f3dce288 100644 --- a/x/precisebank/keeper/remainder_amount.go +++ b/x/precisebank/keeper/remainder_amount.go @@ -43,7 +43,7 @@ func (k *Keeper) SetRemainderAmount( // Ensure the remainder is valid before setting it. Follows the same // validation as FractionalBalance with the same value range. - if err := types.NewFractionalAmountFromInt(amount).Validate(); err != nil { + if err := types.ValidateFractionalAmount(amount); err != nil { panic(fmt.Errorf("remainder amount is invalid: %w", err)) } diff --git a/x/precisebank/types/fractional_amount.go b/x/precisebank/types/fractional_amount.go deleted file mode 100644 index 59c2d5a3..00000000 --- a/x/precisebank/types/fractional_amount.go +++ /dev/null @@ -1,41 +0,0 @@ -package types - -import ( - fmt "fmt" - - sdkmath "cosmossdk.io/math" -) - -// FractionalAmount represents a fractional amount between the valid range of 1 -// and maxFractionalAmount. This wraps an sdkmath.Int to provide additional -// validation methods so it can be re-used in multiple places. -type FractionalAmount struct { - sdkmath.Int -} - -// NewFractionalAmountFromInt creates a new FractionalAmount from an sdkmath.Int. -func NewFractionalAmountFromInt(i sdkmath.Int) FractionalAmount { - return FractionalAmount{i} -} - -// NewFractionalAmount creates a new FractionalAmount from an int64. -func NewFractionalAmount(i int64) FractionalAmount { - return FractionalAmount{sdkmath.NewInt(i)} -} - -// Validate checks if the FractionalAmount is valid. -func (f FractionalAmount) Validate() error { - if f.IsNil() { - return fmt.Errorf("nil amount") - } - - if !f.IsPositive() { - return fmt.Errorf("non-positive amount %v", f) - } - - if f.GT(maxFractionalAmount) { - return fmt.Errorf("amount %v exceeds max of %v", f, maxFractionalAmount) - } - - return nil -} diff --git a/x/precisebank/types/fractional_balance.go b/x/precisebank/types/fractional_balance.go index 3d39c64a..33d02c0d 100644 --- a/x/precisebank/types/fractional_balance.go +++ b/x/precisebank/types/fractional_balance.go @@ -1,18 +1,15 @@ package types import ( + fmt "fmt" + sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" ) -var ( - // conversionFactor is used to convert the fractional balance to integer - // balances. - conversionFactor = sdkmath.NewInt(1_000_000_000_000) - // maxFractionalAmount is the largest valid value in a FractionalBalance amount. - // This is for direct internal use so that there are no extra allocations. - maxFractionalAmount = conversionFactor.SubRaw(1) -) +// conversionFactor is used to convert the fractional balance to integer +// balances. +var conversionFactor = sdkmath.NewInt(1_000_000_000_000) // ConversionFactor returns a copy of the conversionFactor used to convert the // fractional balance to integer balances. This is also 1 greater than the max @@ -39,5 +36,24 @@ func (fb FractionalBalance) Validate() error { } // Validate the amount with the FractionalAmount wrapper - return NewFractionalAmountFromInt(fb.Amount).Validate() + return ValidateFractionalAmount(fb.Amount) +} + +// ValidateFractionalAmount checks if an sdkmath.Int is a valid fractional +// amount, ensuring it is positive and less than or equal to the maximum +// fractional amount. +func ValidateFractionalAmount(amt sdkmath.Int) error { + if amt.IsNil() { + return fmt.Errorf("nil amount") + } + + if !amt.IsPositive() { + return fmt.Errorf("non-positive amount %v", amt) + } + + if amt.GTE(conversionFactor) { + return fmt.Errorf("amount %v exceeds max of %v", amt, conversionFactor.SubRaw(1)) + } + + return nil } diff --git a/x/precisebank/types/genesis.go b/x/precisebank/types/genesis.go index 9be06c70..38587b25 100644 --- a/x/precisebank/types/genesis.go +++ b/x/precisebank/types/genesis.go @@ -40,7 +40,7 @@ func (gs *GenesisState) Validate() error { } if gs.Remainder.GTE(conversionFactor) { - return fmt.Errorf("remainder %v exceeds max of %v", gs.Remainder, maxFractionalAmount) + return fmt.Errorf("remainder %v exceeds max of %v", gs.Remainder, conversionFactor.SubRaw(1)) } // Determine if sum(fractionalBalances) + remainder = whole integer value