Skip to content

Commit

Permalink
feat: add extra commission rate info to genoracle cmd (#559)
Browse files Browse the repository at this point in the history
  • Loading branch information
audtlr24 authored Dec 19, 2022
1 parent 8882d1a commit d38bf7b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
44 changes: 35 additions & 9 deletions cmd/panacead/cmd/genoracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ const (
flagOracleRemoteReport = "oracle-remote-report"
flagOraclePublicKeyPath = "oracle-public-key-path"

flagOracleAccount = "oracle-account"
flagOracleEndpoint = "oracle-endpoint"
flagOracleCommRate = "oracle-commission-rate"
flagOracleAccount = "oracle-account"
flagOracleEndpoint = "oracle-endpoint"
flagOracleCommRate = "oracle-commission-rate"
flagOracleCommMaxRate = "oracle-commission-max-rate"
flagOracleCommMaxChangeRate = "oracle-commission-max-change-rate"
)

type OraclePubKeyInfo struct {
Expand Down Expand Up @@ -74,6 +76,10 @@ func AddGenesisOracleCmd(defaultNodeHome string) *cobra.Command {
return fmt.Errorf("failed to set oracle params: %w", err)
}

if err := oracleGenState.Validate(); err != nil {
return err
}

oracleGenStateBz, err := cdc.MarshalJSON(oracleGenState)
if err != nil {
return fmt.Errorf("failed to marshal oracle genesis state: %w", err)
Expand All @@ -99,6 +105,8 @@ func AddGenesisOracleCmd(defaultNodeHome string) *cobra.Command {
cmd.Flags().String(flagOracleAccount, "", "address or keyName")
cmd.Flags().String(flagOracleEndpoint, "", "oracle's endpoint")
cmd.Flags().String(flagOracleCommRate, "", "oracle's commission rate")
cmd.Flags().String(flagOracleCommMaxRate, "", "oracle's commission max rate")
cmd.Flags().String(flagOracleCommMaxChangeRate, "", "oracle's commission max change rate")
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")

flags.AddQueryFlagsToCmd(cmd)
Expand Down Expand Up @@ -166,15 +174,33 @@ func setOracle(cmd *cobra.Command, genState *oracletypes.GenesisState) error {
return fmt.Errorf("inavlid commission rate: %w", err)
}

if commRate.IsNegative() || commRate.GT(sdk.OneDec()) {
return fmt.Errorf("oracle commission rate should be between 0 and 1")
commMaxRateStr, err := cmd.Flags().GetString(flagOracleCommMaxRate)
if err != nil {
return fmt.Errorf("failed to get oracle commission max rate: %w", err)
}

commMaxRate, err := sdk.NewDecFromStr(commMaxRateStr)
if err != nil {
return fmt.Errorf("inavlid commission max rate: %w", err)
}

commMaxChangeRateStr, err := cmd.Flags().GetString(flagOracleCommMaxChangeRate)
if err != nil {
return fmt.Errorf("failed to get oracle commission max change rate: %w", err)
}

commMaxChangeRate, err := sdk.NewDecFromStr(commMaxChangeRateStr)
if err != nil {
return fmt.Errorf("inavlid commission max change rate: %w", err)
}

genState.Oracles = append(genState.Oracles, oracletypes.Oracle{
UniqueId: uniqueID,
OracleAddress: oracleAccAddr.String(),
Endpoint: endpoint,
OracleCommissionRate: commRate,
UniqueId: uniqueID,
OracleAddress: oracleAccAddr.String(),
Endpoint: endpoint,
OracleCommissionRate: commRate,
OracleCommissionMaxRate: commMaxRate,
OracleCommissionMaxChangeRate: commMaxChangeRate,
})

return nil
Expand Down
10 changes: 4 additions & 6 deletions x/oracle/types/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,15 @@ func (m *Oracle) ValidateBasic() error {
if len(m.UniqueId) == 0 {
return fmt.Errorf("uniqueID is empty")
}
if len(m.Endpoint) == 0 {
return fmt.Errorf("endpoint is empty")
}
if m.OracleCommissionRate.LT(sdk.ZeroDec()) || m.OracleCommissionRate.GT(sdk.OneDec()) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "oracleCommissionRate must be between 0 and 1")
}

if m.OracleCommissionMaxRate.LT(sdk.ZeroDec()) || m.OracleCommissionMaxRate.GT(sdk.OneDec()) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "OracleCommissionMaxRate must be between 0 and 1")
}

if m.OracleCommissionRate.LT(sdk.ZeroDec()) || m.OracleCommissionRate.GT(m.OracleCommissionMaxRate) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "oracleCommissionRate must be between 0 and OracleCommissionMaxRate")
}

if m.OracleCommissionMaxChangeRate.LT(sdk.ZeroDec()) || m.OracleCommissionMaxChangeRate.GT(sdk.OneDec()) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "OracleCommissionMaxChangeRate must be between 0 and 1")
}
Expand Down

0 comments on commit d38bf7b

Please sign in to comment.