-
Notifications
You must be signed in to change notification settings - Fork 608
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize gamm Inverse relationship test , add Inverse relationship …
…test for stableswap (#1456) * Generalize inverse testing from balancer package to pool-models * Add stableswap inverse testing * fixed failing lint test * Update x/gamm/pool-models/internal/test_helpers/test_helpers.go Co-authored-by: Roman <[email protected]> * Add test * Address Bez's review * Fix test * Update x/gamm/pool-models/internal/test_helpers/test_helpers.go Co-authored-by: Aleksandr Bezobchuk <[email protected]> Co-authored-by: Xiangan He <[email protected]> Co-authored-by: Xiangan He <[email protected]> Co-authored-by: Roman <[email protected]> Co-authored-by: Aleksandr Bezobchuk <[email protected]>
- Loading branch information
1 parent
c0573d1
commit 2007bab
Showing
9 changed files
with
215 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package test_helpers | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/tendermint/tendermint/libs/log" | ||
tmtypes "github.com/tendermint/tendermint/proto/tendermint/types" | ||
dbm "github.com/tendermint/tm-db" | ||
|
||
"github.com/osmosis-labs/osmosis/v7/osmoutils" | ||
"github.com/osmosis-labs/osmosis/v7/x/gamm/types" | ||
) | ||
|
||
// CfmmCommonTestSuite is the common test suite struct of Constant Function Market Maker, | ||
// that pool-models can inherit from. | ||
type CfmmCommonTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (suite *CfmmCommonTestSuite) CreateTestContext() sdk.Context { | ||
db := dbm.NewMemDB() | ||
logger := log.NewNopLogger() | ||
|
||
ms := rootmulti.NewStore(db, logger) | ||
|
||
return sdk.NewContext(ms, tmtypes.Header{}, false, logger) | ||
} | ||
|
||
func (suite *CfmmCommonTestSuite) TestCalculateAmountOutAndIn_InverseRelationship( | ||
ctx sdk.Context, | ||
pool types.PoolI, | ||
assetInDenom string, | ||
assetOutDenom string, | ||
initialCalcOut int64, | ||
swapFee sdk.Dec, | ||
) { | ||
initialOut := sdk.NewInt64Coin(assetOutDenom, initialCalcOut) | ||
initialOutCoins := sdk.NewCoins(initialOut) | ||
|
||
actualTokenIn, err := pool.CalcInAmtGivenOut(ctx, initialOutCoins, assetInDenom, swapFee) | ||
suite.Require().NoError(err) | ||
|
||
inverseTokenOut, err := pool.CalcOutAmtGivenIn(ctx, sdk.NewCoins(actualTokenIn), assetOutDenom, swapFee) | ||
suite.Require().NoError(err) | ||
|
||
suite.Require().Equal(initialOut.Denom, inverseTokenOut.Denom) | ||
|
||
expected := initialOut.Amount.ToDec() | ||
actual := inverseTokenOut.Amount.ToDec() | ||
|
||
// allow a rounding error of up to 1 for this relation | ||
tol := sdk.NewDec(1) | ||
_, approxEqual, _, _, _ := osmoutils.DecApproxEq(suite.T(), expected, actual, tol) | ||
suite.Require().True(approxEqual) | ||
} | ||
|
||
func TestCfmmCommonTestSuite(t *testing.T) { | ||
t.Parallel() | ||
suite.Run(t, new(CfmmCommonTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package stableswap | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/osmosis-labs/osmosis/v7/x/gamm/types" | ||
) | ||
|
||
func createTestPool(t *testing.T, poolLiquidity sdk.Coins, swapFee, exitFee sdk.Dec) types.PoolI { | ||
pool, err := NewStableswapPool(1, PoolParams{ | ||
SwapFee: swapFee, | ||
ExitFee: exitFee, | ||
}, poolLiquidity, "") | ||
|
||
require.NoError(t, err) | ||
|
||
return &pool | ||
} |