Skip to content

Commit

Permalink
quicksave wip! - add protos and msg server + todos
Browse files Browse the repository at this point in the history
  • Loading branch information
Unique-Divine committed Dec 28, 2023
1 parent 31d3bdb commit 1164d6e
Show file tree
Hide file tree
Showing 7 changed files with 1,149 additions and 79 deletions.
67 changes: 67 additions & 0 deletions proto/nibiru/oracle/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ service Msg {
returns (MsgDelegateFeedConsentResponse) {
option (google.api.http).post = "/nibiru/oracle/feeder-delegate";
}

rpc EditOracleParams(MsgEditOracleParams)
returns (MsgEditOracleParamsResponse) {
option (google.api.http).post = "/nibiru/oracle/feeder-delegate";
}
}

// MsgAggregateExchangeRatePrevote represents a message to submit
Expand Down Expand Up @@ -84,3 +89,65 @@ message MsgDelegateFeedConsent {
// MsgDelegateFeedConsentResponse defines the Msg/DelegateFeedConsent response
// type.
message MsgDelegateFeedConsentResponse {}

// MsgEditOracleParams: gRPC tx message for updating the x/oracle module params
// [Admin] Only callable by sudoers.
message MsgEditOracleParams {
string sender = 1;

string vote_period = 2 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = true
];

// vote_threshold: [cosmossdk.io/math.LegacyDec] TODO:
string vote_threshold = 3 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

// reward_band: [cosmossdk.io/math.LegacyDec] TODO:
string reward_band = 4 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

repeated string whitelist = 5 [ (gogoproto.nullable) = true ];

// slash_fraction: [cosmossdk.io/math.LegacyDec] TODO:
string slash_fraction = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

string slash_window = 7 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = true
];

// min_valid_per_window: [cosmossdk.io/math.LegacyDec] TODO:
string min_valid_per_window = 8 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];

string twap_lookback_window = 9 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = true
];

string min_voters = 10 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int",
(gogoproto.nullable) = true
];

// VoteThreshold: [cosmossdk.io/math.LegacyDec] TODO:
string validator_fee_ratio = 11 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = true
];
}

// MsgEditOracleParamsResponse defines the Msg/EditOracleParams response
// type.
message MsgEditOracleParamsResponse {}
12 changes: 12 additions & 0 deletions x/oracle/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,15 @@ func (ms msgServer) DelegateFeedConsent(

return &types.MsgDelegateFeedConsentResponse{}, err
}

// EditOracleParams: gRPC tx msg for editing the oracle module params.
// [Admin] Only callable by sudoers.
func (ms msgServer) EditOracleParams(
goCtx context.Context, msg *types.MsgEditOracleParams,
) (resp *types.MsgEditOracleParamsResponse, err error) {
ctx := sdk.UnwrapSDKContext(goCtx)
sender, _ := sdk.AccAddressFromBech32(msg.Sender)
return resp, ms.Admin.EditOracleParams(
ctx, PartialOracleParams{PbMsg: *msg}, sender,
)
}
63 changes: 26 additions & 37 deletions x/oracle/keeper/sudo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"time"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/NibiruChain/nibiru/x/common/asset"
Expand All @@ -24,16 +22,7 @@ import (
type admin struct{ *Keeper }

type PartialOracleParams struct {
VotePeriod *sdkmath.Int `json:"vote_period,omitempty"`
VoteThreshold *sdk.Dec `json:"vote_threshold,omitempty"`
RewardBand *sdk.Dec `json:"reward_band,omitempty"`
Whitelist []string `json:"whitelist,omitempty"`
SlashFraction *sdk.Dec `json:"slash_fraction,omitempty"`
SlashWindow *sdkmath.Int `json:"slash_window,omitempty"`
MinValidPerWindow *sdk.Dec `json:"min_valid_per_window,omitempty"`
TwapLookbackWindow *sdkmath.Int `json:"twap_lookback_window,omitempty"`
MinVoters *sdkmath.Int `json:"min_voters,omitempty"`
ValidatorFeeRatio *sdk.Dec `json:"validator_fee_ratio,omitempty"`
PbMsg oracletypes.MsgEditOracleParams
}

func (k admin) EditOracleParams(
Expand All @@ -54,55 +43,55 @@ func (k admin) EditOracleParams(
return nil
}

// mergeOracleParams: Takes the givne oracle params and merges them into the
// existing partial params, keeping any existing values that are note set in the
// partial msg
func (msg PartialOracleParams) mergeOracleParams(
// mergeOracleParams: Takes the given oracle params and merges them into the
// existing partial params, keeping any existing values that are not set in the
// partial.
func (partial PartialOracleParams) mergeOracleParams(
oracleParams oracletypes.Params,
) oracletypes.Params {
if msg.VotePeriod != nil {
oracleParams.VotePeriod = msg.VotePeriod.Uint64()
if partial.PbMsg.VotePeriod != nil {
oracleParams.VotePeriod = partial.PbMsg.VotePeriod.Uint64()
}

if msg.VoteThreshold != nil {
oracleParams.VoteThreshold = *msg.VoteThreshold
if partial.PbMsg.VoteThreshold != nil {
oracleParams.VoteThreshold = *partial.PbMsg.VoteThreshold
}

if msg.RewardBand != nil {
oracleParams.RewardBand = *msg.RewardBand
if partial.PbMsg.RewardBand != nil {
oracleParams.RewardBand = *partial.PbMsg.RewardBand
}

if msg.Whitelist != nil {
whitelist := make([]asset.Pair, len(msg.Whitelist))
for i, pair := range msg.Whitelist {
if partial.PbMsg.Whitelist != nil {
whitelist := make([]asset.Pair, len(partial.PbMsg.Whitelist))
for i, pair := range partial.PbMsg.Whitelist {
whitelist[i] = asset.MustNewPair(pair)
}

oracleParams.Whitelist = whitelist
}

if msg.SlashFraction != nil {
oracleParams.SlashFraction = *msg.SlashFraction
if partial.PbMsg.SlashFraction != nil {
oracleParams.SlashFraction = *partial.PbMsg.SlashFraction
}

if msg.SlashWindow != nil {
oracleParams.SlashWindow = msg.SlashWindow.Uint64()
if partial.PbMsg.SlashWindow != nil {
oracleParams.SlashWindow = partial.PbMsg.SlashWindow.Uint64()
}

if msg.MinValidPerWindow != nil {
oracleParams.MinValidPerWindow = *msg.MinValidPerWindow
if partial.PbMsg.MinValidPerWindow != nil {
oracleParams.MinValidPerWindow = *partial.PbMsg.MinValidPerWindow
}

if msg.TwapLookbackWindow != nil {
oracleParams.TwapLookbackWindow = time.Duration(msg.TwapLookbackWindow.Int64())
if partial.PbMsg.TwapLookbackWindow != nil {
oracleParams.TwapLookbackWindow = time.Duration(partial.PbMsg.TwapLookbackWindow.Int64())
}

if msg.MinVoters != nil {
oracleParams.MinVoters = msg.MinVoters.Uint64()
if partial.PbMsg.MinVoters != nil {
oracleParams.MinVoters = partial.PbMsg.MinVoters.Uint64()
}

if msg.ValidatorFeeRatio != nil {
oracleParams.ValidatorFeeRatio = *msg.ValidatorFeeRatio
if partial.PbMsg.ValidatorFeeRatio != nil {
oracleParams.ValidatorFeeRatio = *partial.PbMsg.ValidatorFeeRatio
}

return oracleParams
Expand Down
16 changes: 10 additions & 6 deletions x/oracle/keeper/sudo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/NibiruChain/nibiru/x/common/testutil"
"github.com/NibiruChain/nibiru/x/common/testutil/testapp"
oraclekeeper "github.com/NibiruChain/nibiru/x/oracle/keeper"
oracletypes "github.com/NibiruChain/nibiru/x/oracle/types"
)

func TestSuiteOracleExecutor_RunAll(t *testing.T) {
Expand All @@ -34,7 +35,7 @@ func (s *SuiteOracleSudo) TestEditOracleParams() {
twapLookbackWindow := sdk.NewInt(int64(time.Second * 30))
minVoters := sdk.NewInt(2)
validatorFeeRatio := sdk.MustNewDecFromStr("0.7")
partialParams := oraclekeeper.PartialOracleParams{
partialParams := oracletypes.MsgEditOracleParams{
VotePeriod: &votePeriod,
VoteThreshold: &voteThreshold,
RewardBand: &rewardBand,
Expand All @@ -46,18 +47,21 @@ func (s *SuiteOracleSudo) TestEditOracleParams() {
MinVoters: &minVoters,
ValidatorFeeRatio: &validatorFeeRatio,
}

// TODO: Verify that params before were not equal

invalidSender := testutil.AccAddress()
err := nibiru.OracleKeeper.Admin.EditOracleParams(
ctx, partialParams, invalidSender,
oracleMsgServer := oraclekeeper.NewMsgServerImpl(nibiru.OracleKeeper)
goCtx := sdk.WrapSDKContext(ctx)
partialParams.Sender = invalidSender.String()
_, err := oracleMsgServer.EditOracleParams(
goCtx, &partialParams,
)
s.Error(err)

okSender := testapp.DefaultSudoRoot()
err = nibiru.OracleKeeper.Admin.EditOracleParams(
ctx, partialParams, okSender,
partialParams.Sender = okSender.String()
_, err = oracleMsgServer.EditOracleParams(
goCtx, &partialParams,
)
s.NoError(err)

Expand Down
28 changes: 28 additions & 0 deletions x/oracle/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var (
_ sdk.Msg = &MsgDelegateFeedConsent{}
_ sdk.Msg = &MsgAggregateExchangeRatePrevote{}
_ sdk.Msg = &MsgAggregateExchangeRateVote{}
_ sdk.Msg = &MsgEditOracleParams{}
)

// oracle message types
const (
TypeMsgDelegateFeedConsent = "delegate_feeder"
TypeMsgAggregateExchangeRatePrevote = "aggregate_exchange_rate_prevote"
TypeMsgAggregateExchangeRateVote = "aggregate_exchange_rate_vote"
TypeMsgEditOracleParams = "edit_oracle_params"
)

//-------------------------------------------------
Expand Down Expand Up @@ -149,6 +151,8 @@ func (msg MsgAggregateExchangeRateVote) ValidateBasic() error {
return nil
}

// ------------------------ MsgDelegateFeedConsent ------------------------

// NewMsgDelegateFeedConsent creates a MsgDelegateFeedConsent instance
func NewMsgDelegateFeedConsent(operatorAddress sdk.ValAddress, feederAddress sdk.AccAddress) *MsgDelegateFeedConsent {
return &MsgDelegateFeedConsent{
Expand Down Expand Up @@ -192,3 +196,27 @@ func (msg MsgDelegateFeedConsent) ValidateBasic() error {

return nil
}

// ------------------------ MsgEditOracleParams ------------------------

func (m MsgEditOracleParams) Route() string { return RouterKey }
func (m MsgEditOracleParams) Type() string { return TypeMsgEditOracleParams }

func (m MsgEditOracleParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Sender); err != nil {
return err
}
return nil
}

func (m MsgEditOracleParams) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(&m))
}

func (m MsgEditOracleParams) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(m.Sender)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}
Loading

0 comments on commit 1164d6e

Please sign in to comment.