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

feat: add extra commission rate info to genoracle cmd #559

Merged
merged 3 commits into from
Dec 19, 2022
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
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