Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-zaremba committed Feb 8, 2024
1 parent 9b3db15 commit c987e43
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
1 change: 0 additions & 1 deletion types/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/cometbft/cometbft-db v0.9.1
github.com/cosmos/cosmos-proto v1.0.0-beta.2 // indirect; NOTE: v1.0.0-beta.2+ is not compatible with sdk v0.47
github.com/cosmos/cosmos-sdk v0.47.8
// TODO: check the latest
github.com/cosmos/cosmos-sdk/orm v1.0.0-alpha.12
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
Expand Down
19 changes: 9 additions & 10 deletions types/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
package math

import (
"fmt"

"cosmossdk.io/errors"
"fmt"
"github.com/cockroachdb/apd/v2"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

Expand All @@ -17,6 +15,10 @@ var exactContext = apd.Context{
Traps: apd.DefaultTraps | apd.Inexact | apd.Rounded,
}

var (
errNegativeSub = fmt.Errorf("result negative during non-negative subtraction")
)

// Add adds x and y
func Add(x Dec, y Dec) (Dec, error) {
return x.Add(y)
Expand All @@ -31,7 +33,7 @@ func SubNonNegative(x Dec, y Dec) (Dec, error) {
}

if z.IsNegative() {
return z, fmt.Errorf("result negative during non-negative subtraction")
return z, errNegativeSub
}

return z, nil
Expand Down Expand Up @@ -59,15 +61,12 @@ func SafeAddBalance(x Dec, y Dec) (Dec, error) {
var z Dec

if x.IsNegative() || y.IsNegative() {
return z, errors.Wrap(
sdkerrors.ErrInvalidRequest,
fmt.Sprintf("AddBalance() requires two non-negative Dec parameters, but received %s and %s", x, y))
return z, sdkerrors.ErrInvalidRequest.Wrap(fmt.Sprintf("AddBalance() requires two non-negative Dec parameters, but received %s and %s", x, y))

}

_, err := exactContext.Add(&z.dec, &x.dec, &y.dec)
if err != nil {
if _, err := exactContext.Add(&z.dec, &x.dec, &y.dec); err != nil {
return z, errors.Wrap(err, "decimal subtraction error")
}

return z, nil
}

0 comments on commit c987e43

Please sign in to comment.