Skip to content

Commit

Permalink
Add AbsMut
Browse files Browse the repository at this point in the history
  • Loading branch information
ValarDragon committed Apr 10, 2024
1 parent 717447d commit 0ddea0b
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ func (d BigDec) LT(d2 BigDec) bool { return (d.i).Cmp(d2.i) < 0 } /
func (d BigDec) LTE(d2 BigDec) bool { return (d.i).Cmp(d2.i) <= 0 } // less than or equal
func (d BigDec) Neg() BigDec { return BigDec{new(big.Int).Neg(d.i)} } // reverse the decimal sign
// nolint: stylecheck
func (d BigDec) Abs() BigDec { return BigDec{new(big.Int).Abs(d.i)} } // absolute value
func (d BigDec) Abs() BigDec { return BigDec{new(big.Int).Abs(d.i)} } // absolute value
func (d BigDec) AbsMut() BigDec { d.i.Abs(d.i); return d } // absolute value

// BigInt returns a copy of the underlying big.Int.
func (d BigDec) BigInt() *big.Int {
Expand Down
2 changes: 1 addition & 1 deletion osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func (s *decimalTestSuite) TestApproxRoot() {
for i, tc := range testCases {
res, err := tc.input.ApproxRoot(tc.root)
s.Require().NoError(err)
s.Require().True(tc.expected.Sub(res).Abs().LTE(osmomath.SmallestBigDec()), "unexpected result for test case %d, input: %v", i, tc.input)
s.Require().True(tc.expected.Sub(res).AbsMut().LTE(osmomath.SmallestBigDec()), "unexpected result for test case %d, input: %v", i, tc.input)
}
}

Expand Down
1 change: 1 addition & 0 deletions x/concentrated-liquidity/math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func CalcAmount0Delta(liq, sqrtPriceA, sqrtPriceB osmomath.BigDec, roundUp bool)
// CalcAmount1Delta = liq * (sqrtPriceB - sqrtPriceA)
func CalcAmount1Delta(liq, sqrtPriceA, sqrtPriceB osmomath.BigDec, roundUp bool) osmomath.BigDec {
// make sqrtPriceA the smaller value amongst sqrtPriceA and sqrtPriceB
// TODO: Remove this GT check and just do .AbsMut
if sqrtPriceA.GT(sqrtPriceB) {
sqrtPriceA, sqrtPriceB = sqrtPriceB, sqrtPriceA
}
Expand Down

0 comments on commit 0ddea0b

Please sign in to comment.