Skip to content

Commit

Permalink
test(spot): exit stable pool cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yang committed Jun 28, 2023
1 parent 9288a75 commit 703e1c0
Showing 1 changed file with 57 additions and 60 deletions.
117 changes: 57 additions & 60 deletions x/spot/client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
sdktestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"

testutilcli "github.com/NibiruChain/nibiru/x/common/testutil/cli"
Expand Down Expand Up @@ -424,16 +423,15 @@ func (s *IntegrationTestSuite) TestNewExitPoolCmd() {
}

func (s *IntegrationTestSuite) TestNewExitStablePoolCmd() {
s.T().Skip("this test looks like it has a bug https://github.com/NibiruChain/nibiru/issues/869")
val := s.network.Validators[0]

// create a new pool
out, err := ExecMsgCreatePool(
s.T(),
val.ClientCtx,
/*owner-*/ val.Address,
/*tokenWeights=*/ fmt.Sprintf("1%s,1%s", "coin-3", "coin-4"),
/*tokenWeights=*/ fmt.Sprintf("100%s,100%s", "coin-3", "coin-4"),
/*tokenWeights=*/ fmt.Sprintf("1%s,1%s", "coin-3", "coin-5"),
/*tokenWeights=*/ fmt.Sprintf("100%s,100%s", "coin-3", "coin-5"),
/*swapFee=*/ "0.01",
/*exitFee=*/ "0.01",
/*poolType=*/ "stableswap",
Expand All @@ -451,54 +449,49 @@ func (s *IntegrationTestSuite) TestNewExitStablePoolCmd() {
s.Require().NoError(err, out.String())

testCases := []struct {
name string
poolId uint64
poolSharesOut string
expectErr bool
respType proto.Message
expectedCode uint32
expectedunibi sdkmath.Int
expectedOtherToken sdkmath.Int
name string
poolId uint64
poolSharesOut string
expectErr bool
expectedCode uint32
expectedCoin3 sdkmath.Int
expectedCoin5 sdkmath.Int
}{
{
name: "exit pool from invalid pool",
poolId: 100,
poolSharesOut: "100nibiru/pool/100",
expectErr: false,
respType: &sdk.TxResponse{},
expectedCode: 1, // spot.types.ErrNonExistingPool
expectedunibi: sdk.NewInt(-10),
expectedOtherToken: sdk.NewInt(0),
},
{
name: "exit pool for too many shares",
poolId: poolID,
poolSharesOut: fmt.Sprintf("1001000000000000000000nibiru/pool/%d", poolID),
expectErr: false,
respType: &sdk.TxResponse{},
expectedCode: 1,
expectedunibi: sdk.NewInt(-10),
expectedOtherToken: sdk.NewInt(0),
},
{
name: "exit pool for zero shares",
poolId: poolID,
poolSharesOut: fmt.Sprintf("0nibiru/pool/%d", poolID),
expectErr: false,
respType: &sdk.TxResponse{},
expectedCode: 1,
expectedunibi: sdk.NewInt(-10),
expectedOtherToken: sdk.NewInt(0),
name: "exit pool from invalid pool",
poolId: 100,
poolSharesOut: "100nibiru/pool/100",
expectErr: false,
expectedCode: 1, // spot.types.ErrNonExistingPool
expectedCoin3: sdk.ZeroInt(),
expectedCoin5: sdk.ZeroInt(),
},
{
name: "exit pool for too many shares",
poolId: poolID,
poolSharesOut: fmt.Sprintf("1001000000000000000000nibiru/pool/%d", poolID),
expectErr: false,
expectedCode: 1,
expectedCoin3: sdk.ZeroInt(),
expectedCoin5: sdk.ZeroInt(),
},
{
name: "exit pool for zero shares",
poolId: poolID,
poolSharesOut: fmt.Sprintf("0nibiru/pool/%d", poolID),
expectErr: false,
expectedCode: 1,
expectedCoin3: sdk.ZeroInt(),
expectedCoin5: sdk.ZeroInt(),
},
{ // Looks with a bug
name: "exit pool with sufficient balance",
poolId: poolID,
poolSharesOut: fmt.Sprintf("100000000000000000000nibiru/pool/%d", poolID),
expectErr: false,
respType: &sdk.TxResponse{},
expectedCode: 0,
expectedunibi: sdk.NewInt(100 - 10 - 1), // Received unibi minus 10unibi tx fee minus 1 exit pool fee
expectedOtherToken: sdk.NewInt(100 - 1), // Received uusdc minus 1 exit pool fee
name: "exit pool with sufficient balance",
poolId: poolID,
poolSharesOut: fmt.Sprintf("100000000000000000000nibiru/pool/%d", poolID),
expectErr: false,
expectedCode: 0,
expectedCoin3: sdk.NewInt(99), // Received coin-3 minus 1 exit pool fee
expectedCoin5: sdk.NewInt(99), // Received coin-5 minus 1 exit pool fee
},
}

Expand All @@ -511,32 +504,36 @@ func (s *IntegrationTestSuite) TestNewExitStablePoolCmd() {
resp, err := sdktestutil.QueryBalancesExec(ctx, val.Address)
s.Require().NoError(err)
var originalBalance banktypes.QueryAllBalancesResponse
s.Require().NoError(ctx.Codec.UnmarshalJSON(resp.Bytes(), &originalBalance))
ctx.Codec.MustUnmarshalJSON(resp.Bytes(), &originalBalance)

out, err := ExecMsgExitPool(ctx, tc.poolId, val.Address, tc.poolSharesOut)

if tc.expectErr {
s.Require().Error(err)
} else {
s.Require().NoError(err, out.String())
s.Require().NoError(ctx.Codec.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
s.Require().NoError(s.network.WaitForNextBlock())

resp := &sdk.TxResponse{}
ctx.Codec.MustUnmarshalJSON(out.Bytes(), resp)
resp, err = testutilcli.QueryTx(ctx, resp.TxHash)
s.Require().NoError(err)

txResp := tc.respType.(*sdk.TxResponse)
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
s.Require().Equal(tc.expectedCode, resp.Code, out.String())

// Ensure balance is ok
resp, err := sdktestutil.QueryBalancesExec(ctx, val.Address)
out, err := sdktestutil.QueryBalancesExec(ctx, val.Address)
s.Require().NoError(err)
var finalBalance banktypes.QueryAllBalancesResponse
s.Require().NoError(ctx.Codec.UnmarshalJSON(resp.Bytes(), &finalBalance))
ctx.Codec.MustUnmarshalJSON(out.Bytes(), &finalBalance)

s.Require().Equal(
originalBalance.Balances.AmountOf("uusdc").Add(tc.expectedOtherToken),
finalBalance.Balances.AmountOf("uusdc"),
s.Assert().Equal(
originalBalance.Balances.AmountOf("coin-3").Add(tc.expectedCoin3),
finalBalance.Balances.AmountOf("coin-3"),
)
s.Require().Equal(
originalBalance.Balances.AmountOf("unibi").Add(tc.expectedunibi),
finalBalance.Balances.AmountOf("unibi"),
s.Assert().Equal(
originalBalance.Balances.AmountOf("coin-5").Add(tc.expectedCoin5),
finalBalance.Balances.AmountOf("coin-5"),
)
}
})
Expand Down

0 comments on commit 703e1c0

Please sign in to comment.