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

allow megavault operator to set default quoting params #2620

Merged
merged 1 commit into from
Nov 27, 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
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
Loading