Skip to content

Commit

Permalink
allow megavault operator to set default quoting params (#2620)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 authored Nov 27, 2024
1 parent 9a66300 commit 6ca9b4e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

errorsmod "cosmossdk.io/errors"

govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/dydxprotocol/v4-chain/protocol/lib"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)
Expand All @@ -15,15 +14,18 @@ func (k msgServer) UpdateDefaultQuotingParams(
goCtx context.Context,
msg *types.MsgUpdateDefaultQuotingParams,
) (*types.MsgUpdateDefaultQuotingParamsResponse, error) {
if !k.HasAuthority(msg.Authority) {
ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)
operator := k.GetOperatorParams(ctx).Operator

// Check if authority is valid (must be a module authority or operator).
if !k.HasAuthority(msg.Authority) && msg.Authority != operator {
return nil, errorsmod.Wrapf(
govtypes.ErrInvalidSigner,
types.ErrInvalidAuthority,
"invalid authority %s",
msg.Authority,
)
}

ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName)
if err := k.SetDefaultQuotingParams(ctx, msg.DefaultQuotingParams); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,39 @@ import (
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"

"github.com/cometbft/cometbft/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/keeper"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
"github.com/stretchr/testify/require"
)

func TestMsgUpdateDefaultQuotingParams(t *testing.T) {
tests := map[string]struct {
// Msg.
msg *types.MsgUpdateDefaultQuotingParams
msg *vaulttypes.MsgUpdateDefaultQuotingParams
// Operator.
operator string
// Expected error
expectedErr string
}{
"Success. Update to default": {
msg: &types.MsgUpdateDefaultQuotingParams{
"Success. Update to default. Gov Authority": {
msg: &vaulttypes.MsgUpdateDefaultQuotingParams{
Authority: lib.GovModuleAddress.String(),
DefaultQuotingParams: types.DefaultQuotingParams(),
DefaultQuotingParams: vaulttypes.DefaultQuotingParams(),
},
operator: constants.AliceAccAddress.String(),
},
"Success. Update to default. Operator Authority": {
msg: &vaulttypes.MsgUpdateDefaultQuotingParams{
Authority: constants.CarlAccAddress.String(),
DefaultQuotingParams: vaulttypes.DefaultQuotingParams(),
},
operator: constants.CarlAccAddress.String(),
},
"Success. Update to non-default": {
msg: &types.MsgUpdateDefaultQuotingParams{
msg: &vaulttypes.MsgUpdateDefaultQuotingParams{
Authority: lib.GovModuleAddress.String(),
DefaultQuotingParams: types.QuotingParams{
DefaultQuotingParams: vaulttypes.QuotingParams{
Layers: 3,
SpreadMinPpm: 234_567,
SpreadBufferPpm: 6_789,
Expand All @@ -40,18 +51,20 @@ func TestMsgUpdateDefaultQuotingParams(t *testing.T) {
ActivationThresholdQuoteQuantums: dtypes.NewInt(2_121_343_787),
},
},
operator: constants.AliceAccAddress.String(),
},
"Failure - Invalid Authority": {
msg: &types.MsgUpdateDefaultQuotingParams{
msg: &vaulttypes.MsgUpdateDefaultQuotingParams{
Authority: constants.AliceAccAddress.String(),
DefaultQuotingParams: types.DefaultQuotingParams(),
DefaultQuotingParams: vaulttypes.DefaultQuotingParams(),
},
operator: constants.BobAccAddress.String(),
expectedErr: "invalid authority",
},
"Failure - Invalid Params": {
msg: &types.MsgUpdateDefaultQuotingParams{
msg: &vaulttypes.MsgUpdateDefaultQuotingParams{
Authority: lib.GovModuleAddress.String(),
DefaultQuotingParams: types.QuotingParams{
DefaultQuotingParams: vaulttypes.QuotingParams{
Layers: 3,
SpreadMinPpm: 4_000,
SpreadBufferPpm: 2_000,
Expand All @@ -61,21 +74,34 @@ func TestMsgUpdateDefaultQuotingParams(t *testing.T) {
ActivationThresholdQuoteQuantums: dtypes.NewInt(1_000_000_000),
},
},
expectedErr: types.ErrInvalidOrderSizePctPpm.Error(),
operator: constants.AliceAccAddress.String(),
expectedErr: vaulttypes.ErrInvalidOrderSizePctPpm.Error(),
},
}

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
tApp := testapp.NewTestAppBuilder(t).WithGenesisDocFn(func() (genesis types.GenesisDoc) {
genesis = testapp.DefaultGenesis()
// Set megavault operator.
testapp.UpdateGenesisDocWithAppStateForModule(
&genesis,
func(genesisState *vaulttypes.GenesisState) {
genesisState.OperatorParams = vaulttypes.OperatorParams{
Operator: tc.operator,
}
},
)
return genesis
}).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper
ms := keeper.NewMsgServerImpl(k)

_, err := ms.UpdateDefaultQuotingParams(ctx, tc.msg)
if tc.expectedErr != "" {
require.ErrorContains(t, err, tc.expectedErr)
require.Equal(t, types.DefaultQuotingParams(), k.GetDefaultQuotingParams(ctx))
require.Equal(t, vaulttypes.DefaultQuotingParams(), k.GetDefaultQuotingParams(ctx))
} else {
require.NoError(t, err)
require.Equal(t, tc.msg.DefaultQuotingParams, k.GetDefaultQuotingParams(ctx))
Expand Down

0 comments on commit 6ca9b4e

Please sign in to comment.