Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: inverse unit tests for CalcInAmtGivenOut and CalcOutAmtGivenIn with zero swap fee #1254

Merged
merged 3 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions x/gamm/pool-models/balancer/amm_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package balancer_test

import (
"fmt"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

"github.com/osmosis-labs/osmosis/v7/x/gamm/pool-models/balancer"
)
Expand Down Expand Up @@ -77,3 +81,108 @@ func (suite *KeeperTestSuite) TestBalancerSpotPrice() {
}
}
}

func TestCalculateAmountOutAndIn_InverseRelationship_ZeroSwapFee(t *testing.T) {
type testcase struct {
denomOut string
initialPoolOut int64
initialWeightOut int64
initialCalcOut int64

denomIn string
initialPoolIn int64
initialWeightIn int64
}

testcases := []testcase{
{
denomOut: "uosmo",
initialPoolOut: 1_000_000_000_000,
initialWeightOut: 100,
initialCalcOut: 100,

denomIn: "ion",
initialPoolIn: 1_000_000_000_000,
initialWeightIn: 100,
},
{
denomOut: "uosmo",
initialPoolOut: 1_000,
initialWeightOut: 100,
initialCalcOut: 100,

denomIn: "ion",
initialPoolIn: 1_000_000,
initialWeightIn: 100,
},
{
denomOut: "uosmo",
initialPoolOut: 1_000,
initialWeightOut: 100,
initialCalcOut: 100,

denomIn: "ion",
initialPoolIn: 1_000_000,
initialWeightIn: 100,
},
{
denomOut: "uosmo",
initialPoolOut: 1_000,
initialWeightOut: 200,
initialCalcOut: 100,

denomIn: "ion",
initialPoolIn: 1_000_000,
initialWeightIn: 50,
},
}

getTestCaseName := func(tc testcase) string {
return fmt.Sprintf("tokenOutInitial: %d, tokenInInitial: %d, initialOut: %d",
tc.initialPoolOut,
tc.initialPoolIn,
tc.initialCalcOut,
)
}

for _, tc := range testcases {
t.Run(getTestCaseName(tc), func(t *testing.T) {
ctx := createTestContext(t)

poolAssetOut := balancer.PoolAsset{
Token: sdk.NewInt64Coin(tc.denomOut, tc.initialPoolOut),
Weight: sdk.NewInt(tc.initialWeightOut),
}

poolAssetIn := balancer.PoolAsset{
Token: sdk.NewInt64Coin(tc.denomIn, tc.initialPoolIn),
Weight: sdk.NewInt(tc.initialWeightIn),
}

pool := createTestPool(t, []balancer.PoolAsset{
poolAssetOut,
poolAssetIn,
},
"0",
"0",
)
require.NotNil(t, pool)

initialOut := sdk.NewInt64Coin(poolAssetOut.Token.Denom, tc.initialCalcOut)
initialOutCoins := sdk.NewCoins(initialOut)

actualTokenIn, err := pool.CalcInAmtGivenOut(ctx, initialOutCoins, poolAssetIn.Token.Denom, sdk.ZeroDec())
require.NoError(t, err)

inverseTokenOut, err := pool.CalcOutAmtGivenIn(ctx, sdk.NewCoins(sdk.NewInt64Coin(poolAssetIn.Token.Denom, actualTokenIn.Amount.TruncateInt64())), poolAssetOut.Token.Denom, sdk.ZeroDec())
require.NoError(t, err)

require.Equal(t, initialOut.Denom, inverseTokenOut.Denom)

expected := initialOut.Amount.ToDec()
actual := inverseTokenOut.Amount.RoundInt().ToDec() // must round to be able to compare with expected.

require.Equal(t, expected, actual)
})
}
}
42 changes: 42 additions & 0 deletions x/gamm/pool-models/balancer/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package balancer_test

import (
"testing"
"time"

"github.com/cosmos/cosmos-sdk/store/rootmulti"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
"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/x/gamm/pool-models/balancer"
"github.com/osmosis-labs/osmosis/v7/x/gamm/types"
)

func createTestPool(t *testing.T, poolAssets []balancer.PoolAsset, swapFee, exitFee string) types.PoolI {
swapFeeDec, err := sdk.NewDecFromStr(swapFee)
require.NoError(t, err)

exitFeeDec, err := sdk.NewDecFromStr(exitFee)
require.NoError(t, err)

pool, err := balancer.NewBalancerPool(1, balancer.PoolParams{
SwapFee: swapFeeDec,
ExitFee: exitFeeDec,
}, poolAssets, "", time.Now())

require.NoError(t, err)

return &pool
}

func createTestContext(t *testing.T) sdk.Context {
db := dbm.NewMemDB()
logger := log.NewNopLogger()

ms := rootmulti.NewStore(db, logger)

return sdk.NewContext(ms, tmtypes.Header{}, false, logger)
}
mattverse marked this conversation as resolved.
Show resolved Hide resolved