Skip to content

Commit

Permalink
Merge pull request DongCoNY#47 from onomyprotocol/add_query_price
Browse files Browse the repository at this point in the history
Add query price for oracle module
  • Loading branch information
vuong177 authored Oct 18, 2024
2 parents 3192212 + 7c1fc05 commit 2af30cb
Show file tree
Hide file tree
Showing 6 changed files with 616 additions and 29 deletions.
20 changes: 20 additions & 0 deletions proto/reserve/oracle/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ service Query {
option (google.api.http).get =
"/reserve/oracle/band_price_states";
}
// Retrieves the price of base/quote
rpc Price(QueryPriceRequest)
returns (QueryPriceResponse) {
option (google.api.http).get =
"/reserve/oracle/price";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand All @@ -42,3 +48,17 @@ message QueryBandPriceStatesRequest {}
message QueryBandPriceStatesResponse {
repeated BandPriceState price_states = 1;
}

// QueryPriceRequest is the request type for the
// Query/Price RPC method.
message QueryPriceRequest {
string base_denom = 1;

string quote_denom = 2;
}

// QueryPriceResponse is the response type for the
// Query/Price RPC method
message QueryPriceResponse {
string price = 1;
}
36 changes: 35 additions & 1 deletion x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func GetQueryCmd() *cobra.Command {

cmd.AddCommand(
GetBandPriceStates(),
GetPrice(),
)
return cmd
}
Expand Down Expand Up @@ -53,4 +54,37 @@ func GetBandPriceStates() *cobra.Command {

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
}

// GetPrice queries the price based on rate of base/quote
func GetPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "price [base] [quote]",
Short: "Get price based on rate of base/quote",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
baseDenom := args[0]
quoteDenom := args[1]

queryClient := types.NewQueryClient(clientCtx)

var res proto.Message
req := &types.QueryPriceRequest{
BaseDenom: baseDenom,
QuoteDenom: quoteDenom,
}
res, err = queryClient.Price(context.Background(), req)
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
2 changes: 2 additions & 0 deletions x/oracle/keeper/band_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (k *Keeper) GetPrice(ctx context.Context, base, quote string) *math.LegacyD
// query ref by using GetBandPriceState
basePriceState := k.GetBandPriceState(ctx, base)
if basePriceState == nil || basePriceState.Rate.IsZero() {
k.Logger(ctx).Info("Can not get price state of base denom %s: price state is nil or rate is zero", base)
return nil
}

Expand All @@ -289,6 +290,7 @@ func (k *Keeper) GetPrice(ctx context.Context, base, quote string) *math.LegacyD

quotePriceState := k.GetBandPriceState(ctx, quote)
if quotePriceState == nil || quotePriceState.Rate.IsZero() {
k.Logger(ctx).Info("Can not get price state of quote denom %s: price state is nil or rate is zero", quote)
return nil
}

Expand Down
12 changes: 11 additions & 1 deletion x/oracle/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ func (k Keeper) BandPriceStates(c context.Context, _ *types.QueryBandPriceStates
}

return res, nil
}
}

func (k Keeper) Price(c context.Context, q *types.QueryPriceRequest) (*types.QueryPriceResponse, error) {
ctx := sdk.UnwrapSDKContext(c)

price := k.GetPrice(ctx, q.BaseDenom, q.QuoteDenom)
res := &types.QueryPriceResponse{
Price: price.String(),
}
return res, nil
}
Loading

0 comments on commit 2af30cb

Please sign in to comment.