Skip to content

Commit

Permalink
[stableswap]: Convert all core stableswap arithmetic to use BigDec (#…
Browse files Browse the repository at this point in the history
…2777)

* convert all core stableswap arithmetic to use bigdec

* fix conversion from sdkDec to BigDec

* use correct precision for conversions

* improve readability and update Sdk -> SDK in function names

* fix merge conflicts
  • Loading branch information
AlpinYukseloglu authored Sep 19, 2022
1 parent 90559ad commit 44a8577
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
11 changes: 8 additions & 3 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,22 +498,27 @@ func (d BigDec) MustFloat64() float64 {

// SdkDec returns the Sdk.Dec representation of a BigDec.
// Values in any additional decimal places are truncated.
func (d BigDec) SdkDec() sdk.Dec {
func (d BigDec) SDKDec() sdk.Dec {
precisionDiff := Precision - sdk.Precision
precisionFactor := new(big.Int).Exp(big.NewInt(10), big.NewInt(int64(precisionDiff)), nil)

if precisionDiff < 0 {
panic("invalid decimal precision")
}

truncatedDec := sdk.NewDecFromBigIntWithPrec(new(big.Int).Quo(d.BigInt(), precisionFactor), sdk.Precision)
// Truncate any additional decimal values that exist due to BigDec's additional precision
// This relies on big.Int's Quo function doing floor division
intRepresentation := new(big.Int).Quo(d.BigInt(), precisionFactor)

// convert int representation back to SDK Dec precision
truncatedDec := sdk.NewDecFromBigIntWithPrec(intRepresentation, sdk.Precision)

return truncatedDec
}

// BigDecFromSdkDec returns the BigDec representation of an SdkDec.
// Values in any additional decimal places are truncated.
func BigDecFromSdkDec(d sdk.Dec) BigDec {
func BigDecFromSDKDec(d sdk.Dec) BigDec {
return NewDecFromBigIntWithPrec(d.BigInt(), sdk.Precision)
}

Expand Down
8 changes: 4 additions & 4 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ func (s *decimalTestSuite) TestSdkDec() {
}
for tcIndex, tc := range tests {
if tc.expPanic {
s.Require().Panics(func() { tc.d.SdkDec() })
s.Require().Panics(func() { tc.d.SDKDec() })
} else {
value := tc.d.SdkDec()
value := tc.d.SDKDec()
s.Require().Equal(tc.want, value, "bad SdkDec(), index: %v", tcIndex)
}
}
Expand All @@ -192,9 +192,9 @@ func (s *decimalTestSuite) TestBigDecFromSdkDec() {
}
for tcIndex, tc := range tests {
if tc.expPanic {
s.Require().Panics(func() { BigDecFromSdkDec(tc.d) })
s.Require().Panics(func() { BigDecFromSDKDec(tc.d) })
} else {
value := BigDecFromSdkDec(tc.d)
value := BigDecFromSDKDec(tc.d)
s.Require().Equal(tc.want, value, "bad BigDecFromSdkDec(), index: %v", tcIndex)
}
}
Expand Down
8 changes: 4 additions & 4 deletions x/gamm/pool-models/stableswap/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,9 @@ func (p *Pool) calcOutAmtGivenIn(tokenIn sdk.Coin, tokenOutDenom string, swapFee
}
tokenInSupply, tokenOutSupply := reserves[0], reserves[1]
// We are solving for the amount of token out, hence x = tokenOutSupply, y = tokenInSupply
cfmmOut := solveCfmm(osmomath.BigDecFromSdkDec(tokenOutSupply), osmomath.BigDecFromSdkDec(tokenInSupply), osmomath.BigDecFromSdkDec(tokenIn.Amount.ToDec()))
cfmmOut := solveCfmm(osmomath.BigDecFromSDKDec(tokenOutSupply), osmomath.BigDecFromSDKDec(tokenInSupply), osmomath.BigDecFromSDKDec(tokenIn.Amount.ToDec()))
outAmt := p.getDescaledPoolAmt(tokenOutDenom, cfmmOut)
return outAmt.SdkDec(), nil
return outAmt.SDKDec(), nil
}

// returns inAmt as a decimal
Expand All @@ -374,9 +374,9 @@ func (p *Pool) calcInAmtGivenOut(tokenOut sdk.Coin, tokenInDenom string, swapFee
tokenInSupply, tokenOutSupply := reserves[0], reserves[1]
// We are solving for the amount of token in, cfmm(x,y) = cfmm(x + x_in, y - y_out)
// x = tokenInSupply, y = tokenOutSupply, yIn = -tokenOutAmount
cfmmIn := solveCfmm(osmomath.BigDecFromSdkDec(tokenInSupply), osmomath.BigDecFromSdkDec(tokenOutSupply), osmomath.BigDecFromSdkDec(tokenOut.Amount.ToDec().Neg()))
cfmmIn := solveCfmm(osmomath.BigDecFromSDKDec(tokenInSupply), osmomath.BigDecFromSDKDec(tokenOutSupply), osmomath.BigDecFromSDKDec(tokenOut.Amount.ToDec().Neg()))
inAmt := p.getDescaledPoolAmt(tokenInDenom, cfmmIn.Neg())
return inAmt.SdkDec(), nil
return inAmt.SDKDec(), nil
}

func (p *Pool) calcSingleAssetJoinShares(tokenIn sdk.Coin, swapFee sdk.Dec) (sdk.Int, error) {
Expand Down
4 changes: 2 additions & 2 deletions x/gamm/pool-models/stableswap/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ func (p Pool) SpotPrice(ctx sdk.Context, baseAssetDenom string, quoteAssetDenom
return sdk.Dec{}, err
}

scaledSpotPrice := spotPrice(osmomath.BigDecFromSdkDec(reserves[0]), osmomath.BigDecFromSdkDec(reserves[1]))
scaledSpotPrice := spotPrice(osmomath.BigDecFromSDKDec(reserves[0]), osmomath.BigDecFromSDKDec(reserves[1]))
spotPrice := p.getDescaledPoolAmt(baseAssetDenom, scaledSpotPrice)
spotPriceSdkDec := spotPrice.SdkDec()
spotPriceSdkDec := spotPrice.SDKDec()

return spotPriceSdkDec, nil
}
Expand Down

0 comments on commit 44a8577

Please sign in to comment.