Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/c-osmosis/osmosis into fros…
Browse files Browse the repository at this point in the history
…tornge/pools
  • Loading branch information
byeongsu-hong committed Oct 18, 2020
2 parents 00b0ee9 + b616d46 commit 89f042f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.3.1
github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.6.1
github.com/tendermint/tendermint v0.34.0-rc3.0.20200907055413-3359e0bf2f84
github.com/tendermint/tm-db v0.6.2
)
Expand Down
24 changes: 17 additions & 7 deletions x/gamm/keeper/pool/math.go → x/gamm/keeper/pool/math/math.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pool
package math

import (
"fmt"

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

Expand Down Expand Up @@ -177,10 +179,12 @@ func subSign(a, b sdk.Dec) (sdk.Dec, bool) {
}

func pow(base sdk.Dec, exp sdk.Dec) sdk.Dec {

// TODO: MIN_BPOW_BASE, MAX_BPOW_BASE identification required
// require(base >= MIN_BPOW_BASE, "ERR_BPOW_BASE_TOO_LOW");
// require(base <= MAX_BPOW_BASE, "ERR_BPOW_BASE_TOO_HIGH");
if base.LTE(sdk.ZeroDec()) {
panic(fmt.Errorf("base have to be greater than zero"))
}
if base.GTE(sdk.OneDec().MulInt64(2)) {
panic(fmt.Errorf("base have to be lesser than two"))
}

whole := exp.TruncateDec()
remain := exp.Sub(whole)
Expand All @@ -197,10 +201,17 @@ func pow(base sdk.Dec, exp sdk.Dec) sdk.Dec {
}

func powApprox(base sdk.Dec, exp sdk.Dec, precision sdk.Dec) sdk.Dec {
if base.LTE(sdk.ZeroDec()) {
panic(fmt.Errorf("base have to be greater than zero"))
}
if base.GTE(sdk.OneDec().MulInt64(2)) {
panic(fmt.Errorf("base have to be lesser than two"))
}

a := exp
x, xneg := subSign(base, sdk.OneDec())
term := sdk.OneDec()
sum := term
sum := sdk.OneDec()
negative := false

for i := 1; term.GTE(precision); i++ {
Expand All @@ -212,7 +223,6 @@ func powApprox(base sdk.Dec, exp sdk.Dec, precision sdk.Dec) sdk.Dec {
if term.IsZero() {
break
}

if xneg {
negative = !negative
}
Expand Down
45 changes: 45 additions & 0 deletions x/gamm/keeper/pool/math/math_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package math

import (
"testing"

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

"github.com/stretchr/testify/require"
)

func TestPowApprox(t *testing.T) {
base, err := sdk.NewDecFromStr("1.5")
require.NoError(t, err)
exp, err := sdk.NewDecFromStr("0.4")
require.NoError(t, err)
precision, err := sdk.NewDecFromStr("0.00000001")
require.NoError(t, err)

s := powApprox(base, exp, precision)
expectedDec, err := sdk.NewDecFromStr("1.17607902")
require.NoError(t, err)

require.True(
t,
expectedDec.Sub(s).Abs().LTE(precision),
"expected value & actual value's difference should less than precision",
)
}

func TestPow(t *testing.T) {
base, err := sdk.NewDecFromStr("1.4")
require.NoError(t, err)
exp, err := sdk.NewDecFromStr("3.2")
require.NoError(t, err)

s := pow(base, exp)
expectedDec, err := sdk.NewDecFromStr("2.93501087")
require.NoError(t, err)

require.True(
t,
expectedDec.Sub(s).Abs().LTE(powPrecision),
"expected value & actual value's difference should less than precision",
)
}

0 comments on commit 89f042f

Please sign in to comment.