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

Add tests for when market pair name changes #2280

Merged
merged 3 commits into from
Oct 1, 2024
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
14 changes: 14 additions & 0 deletions protocol/testing/e2e/gov/prices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
57 changes: 55 additions & 2 deletions protocol/x/prices/keeper/msg_server_update_market_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"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"
Expand All @@ -26,7 +27,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{
Expand All @@ -39,6 +40,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(),
Expand Down Expand Up @@ -122,6 +136,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: "",
Expand All @@ -144,12 +172,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,
[]pricestypes.MarketParam{tc.msg.MarketParam},
)
}

_, err = msgServer.UpdateMarketParam(ctx, tc.msg)
if tc.expectedErr != "" {
require.ErrorContains(t, err, tc.expectedErr)
Expand All @@ -163,6 +201,21 @@ 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 {
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)
}
}
})
}
Expand Down
Loading