From 798ac4fca9563fb3ae4bda2e2b761027541f9e9d Mon Sep 17 00:00:00 2001 From: Chenyao Yu <4844716+chenyaoy@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:54:23 -0400 Subject: [PATCH 1/3] Update tests for market pair name changing --- protocol/testing/e2e/gov/prices_test.go | 14 +++++ .../msg_server_update_market_param_test.go | 60 ++++++++++++++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/protocol/testing/e2e/gov/prices_test.go b/protocol/testing/e2e/gov/prices_test.go index 89f2b996b8..ad341b83b3 100644 --- a/protocol/testing/e2e/gov/prices_test.go +++ b/protocol/testing/e2e/gov/prices_test.go @@ -67,6 +67,20 @@ func TestUpdateMarketParam(t *testing.T) { }, expectedProposalStatus: govtypesv1.ProposalStatus_PROPOSAL_STATUS_FAILED, }, + "Failure: new pair name does not exist in marketmap": { + msg: &pricestypes.MsgUpdateMarketParam{ + Authority: lib.GovModuleAddress.String(), + MarketParam: pricestypes.MarketParam{ + Id: MODIFIED_MARKET_PARAM.Id, + Pair: "nonexistent-pair", + Exponent: MODIFIED_MARKET_PARAM.Exponent, + MinExchanges: MODIFIED_MARKET_PARAM.MinExchanges, + MinPriceChangePpm: MODIFIED_MARKET_PARAM.MinPriceChangePpm, + ExchangeConfigJson: MODIFIED_MARKET_PARAM.ExchangeConfigJson, + }, + }, + expectedProposalStatus: govtypesv1.ProposalStatus_PROPOSAL_STATUS_FAILED, + }, "Failure: exponent is updated": { msg: &pricestypes.MsgUpdateMarketParam{ Authority: lib.GovModuleAddress.String(), diff --git a/protocol/x/prices/keeper/msg_server_update_market_param_test.go b/protocol/x/prices/keeper/msg_server_update_market_param_test.go index b04959a7a6..db22164e88 100644 --- a/protocol/x/prices/keeper/msg_server_update_market_param_test.go +++ b/protocol/x/prices/keeper/msg_server_update_market_param_test.go @@ -1,14 +1,17 @@ package keeper_test import ( + "fmt" "testing" "github.com/dydxprotocol/v4-chain/protocol/lib" + "github.com/dydxprotocol/v4-chain/protocol/lib/slinky" "github.com/dydxprotocol/v4-chain/protocol/testutil/constants" keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" pricestest "github.com/dydxprotocol/v4-chain/protocol/testutil/prices" "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper" + "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" "github.com/stretchr/testify/require" ) @@ -26,7 +29,7 @@ func TestUpdateMarketParam(t *testing.T) { msg *pricestypes.MsgUpdateMarketParam expectedErr string }{ - "Succeeds: update all parameters except exponent": { + "Succeeds: update all parameters except exponent and pair": { msg: &pricestypes.MsgUpdateMarketParam{ Authority: lib.GovModuleAddress.String(), MarketParam: pricestypes.MarketParam{ @@ -39,6 +42,19 @@ func TestUpdateMarketParam(t *testing.T) { }, }, }, + "Succeeds: update pair name": { + msg: &pricestypes.MsgUpdateMarketParam{ + Authority: lib.GovModuleAddress.String(), + MarketParam: pricestypes.MarketParam{ + Id: testMarketParam.Id, + Pair: "NEWMARKET-USD", + Exponent: testMarketParam.Exponent, + MinExchanges: 72, + MinPriceChangePpm: 2_023, + ExchangeConfigJson: `{"exchanges":[{"exchangeName":"XYZ","ticker":"PIKACHU"}]}`, + }, + }, + }, "Succeeds: update min price change ppm only": { msg: &pricestypes.MsgUpdateMarketParam{ Authority: lib.GovModuleAddress.String(), @@ -122,6 +138,20 @@ func TestUpdateMarketParam(t *testing.T) { }, expectedErr: "Market exponent cannot be updated", }, + "Failure: new pair name does not exist in marketmap": { + msg: &pricestypes.MsgUpdateMarketParam{ + Authority: lib.GovModuleAddress.String(), + MarketParam: pricestypes.MarketParam{ + Id: testMarketParam.Id, + Pair: "nonexistent-pair", + Exponent: testMarketParam.Exponent, + MinExchanges: testMarketParam.MinExchanges, + MinPriceChangePpm: testMarketParam.MinPriceChangePpm, + ExchangeConfigJson: "{}", + }, + }, + expectedErr: "NONEXISTENT/PAIR: Ticker not found in market map", + }, "Failure: empty authority": { msg: &pricestypes.MsgUpdateMarketParam{ Authority: "", @@ -144,12 +174,22 @@ func TestUpdateMarketParam(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { - ctx, pricesKeeper, _, _, mockTimeProvider, _, _ := keepertest.PricesKeepers(t) + ctx, pricesKeeper, _, _, mockTimeProvider, _, marketMapKeeper := keepertest.PricesKeepers(t) mockTimeProvider.On("Now").Return(constants.TimeT) msgServer := keeper.NewMsgServerImpl(pricesKeeper) initialMarketParam, err := keepertest.CreateTestMarket(t, ctx, pricesKeeper, testMarketParam, testMarketPrice) require.NoError(t, err) + // Create new pair in marketmap if test is expected to succeed + if (initialMarketParam.Pair != tc.msg.MarketParam.Pair) && tc.expectedErr == "" { + keepertest.CreateMarketsInMarketMapFromParams( + t, + ctx, + marketMapKeeper, + []types.MarketParam{tc.msg.MarketParam}, + ) + } + _, err = msgServer.UpdateMarketParam(ctx, tc.msg) if tc.expectedErr != "" { require.ErrorContains(t, err, tc.expectedErr) @@ -163,6 +203,22 @@ func TestUpdateMarketParam(t *testing.T) { updatedMarketParam, exists := pricesKeeper.GetMarketParam(ctx, tc.msg.MarketParam.Id) require.True(t, exists) require.Equal(t, tc.msg.MarketParam, updatedMarketParam) + + // If pair name changed, verify that old pair is disabled in the marketmap and new pair is enabled + if initialMarketParam.Pair != updatedMarketParam.Pair { + fmt.Println("Inside test") + oldCp, err := slinky.MarketPairToCurrencyPair(initialMarketParam.Pair) + require.NoError(t, err) + oldMarket, err := marketMapKeeper.GetMarket(ctx, oldCp.String()) + require.NoError(t, err) + require.False(t, oldMarket.Ticker.Enabled) + + newCp, err := slinky.MarketPairToCurrencyPair(updatedMarketParam.Pair) + require.NoError(t, err) + market, err := marketMapKeeper.GetMarket(ctx, newCp.String()) + require.NoError(t, err) + require.True(t, market.Ticker.Enabled) + } } }) } From 8627ee6fc5f22bc654fadfaf55b058cb9427ebe8 Mon Sep 17 00:00:00 2001 From: Chenyao Yu <4844716+chenyaoy@users.noreply.github.com> Date: Tue, 17 Sep 2024 14:55:59 -0400 Subject: [PATCH 2/3] remove --- protocol/x/prices/keeper/msg_server_update_market_param_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/protocol/x/prices/keeper/msg_server_update_market_param_test.go b/protocol/x/prices/keeper/msg_server_update_market_param_test.go index db22164e88..c63a6e372e 100644 --- a/protocol/x/prices/keeper/msg_server_update_market_param_test.go +++ b/protocol/x/prices/keeper/msg_server_update_market_param_test.go @@ -1,7 +1,6 @@ package keeper_test import ( - "fmt" "testing" "github.com/dydxprotocol/v4-chain/protocol/lib" @@ -206,7 +205,6 @@ func TestUpdateMarketParam(t *testing.T) { // If pair name changed, verify that old pair is disabled in the marketmap and new pair is enabled if initialMarketParam.Pair != updatedMarketParam.Pair { - fmt.Println("Inside test") oldCp, err := slinky.MarketPairToCurrencyPair(initialMarketParam.Pair) require.NoError(t, err) oldMarket, err := marketMapKeeper.GetMarket(ctx, oldCp.String()) From a7b2ad1880c205dbb6b0cb8d1d5fb8a64b66023b Mon Sep 17 00:00:00 2001 From: Chenyao Yu <4844716+chenyaoy@users.noreply.github.com> Date: Tue, 17 Sep 2024 16:03:21 -0400 Subject: [PATCH 3/3] lint --- .../x/prices/keeper/msg_server_update_market_param_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/protocol/x/prices/keeper/msg_server_update_market_param_test.go b/protocol/x/prices/keeper/msg_server_update_market_param_test.go index c63a6e372e..5e837330e8 100644 --- a/protocol/x/prices/keeper/msg_server_update_market_param_test.go +++ b/protocol/x/prices/keeper/msg_server_update_market_param_test.go @@ -10,7 +10,6 @@ import ( keepertest "github.com/dydxprotocol/v4-chain/protocol/testutil/keeper" pricestest "github.com/dydxprotocol/v4-chain/protocol/testutil/prices" "github.com/dydxprotocol/v4-chain/protocol/x/prices/keeper" - "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types" "github.com/stretchr/testify/require" ) @@ -185,7 +184,7 @@ func TestUpdateMarketParam(t *testing.T) { t, ctx, marketMapKeeper, - []types.MarketParam{tc.msg.MarketParam}, + []pricestypes.MarketParam{tc.msg.MarketParam}, ) }