Skip to content

Commit

Permalink
price queries
Browse files Browse the repository at this point in the history
  • Loading branch information
rbajollari committed Jan 9, 2025
1 parent e8f4301 commit eb0c1a7
Show file tree
Hide file tree
Showing 5 changed files with 1,178 additions and 86 deletions.
29 changes: 29 additions & 0 deletions proto/ojo/oracle/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package ojo.oracle.v1;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "ojo/oracle/v1/oracle.proto";
import "ojo/oracle/v1/elys.proto";
import "cosmos/base/v1beta1/coin.proto";

option go_package = "github.com/ojo-network/ojo/x/oracle/types";
Expand Down Expand Up @@ -97,6 +98,18 @@ service Query {
option (google.api.http).get =
"/ojo/oracle/v1/valdiators/validator_reward_set";
}

// Queries a Price by asset.
rpc Price(QueryGetPriceRequest)
returns (QueryGetPriceResponse) {

Check failure on line 104 in proto/ojo/oracle/v1/query.proto

View workflow job for this annotation

GitHub Actions / buf-lint

RPC response type "QueryGetPriceResponse" should be named "PriceResponse" or "QueryPriceResponse".
option (google.api.http).get = "/elys-network/elys/oracle/price/{asset}";
}

// Queries a list of Price items.
rpc PriceAll(QueryAllPriceRequest)
returns (QueryAllPriceResponse) {

Check failure on line 110 in proto/ojo/oracle/v1/query.proto

View workflow job for this annotation

GitHub Actions / buf-lint

RPC response type "QueryAllPriceResponse" should be named "PriceAllResponse" or "QueryPriceAllResponse".
option (google.api.http).get = "/elys-network/elys/oracle/prices";
}
}

// QueryExchangeRates is the request type for the Query/ExchangeRate RPC
Expand Down Expand Up @@ -291,3 +304,19 @@ message QueryValidatorRewardSet {}
message QueryValidatorRewardSetResponse {
ValidatorRewardSet validators = 1 [(gogoproto.nullable) = false];
}

message QueryGetPriceRequest {

Check failure on line 308 in proto/ojo/oracle/v1/query.proto

View workflow job for this annotation

GitHub Actions / buf-lint

Message "QueryGetPriceRequest" should have a non-empty comment for documentation.
string asset = 1;
string source = 2;
uint64 timestamp = 3;
}

message QueryGetPriceResponse {

Check failure on line 314 in proto/ojo/oracle/v1/query.proto

View workflow job for this annotation

GitHub Actions / buf-lint

Message "QueryGetPriceResponse" should have a non-empty comment for documentation.
Price price = 1 [ (gogoproto.nullable) = false ];
}

message QueryAllPriceRequest {}

Check failure on line 318 in proto/ojo/oracle/v1/query.proto

View workflow job for this annotation

GitHub Actions / buf-lint

Message "QueryAllPriceRequest" should have a non-empty comment for documentation.

message QueryAllPriceResponse {

Check failure on line 320 in proto/ojo/oracle/v1/query.proto

View workflow job for this annotation

GitHub Actions / buf-lint

Message "QueryAllPriceResponse" should have a non-empty comment for documentation.
repeated Price price = 1 [ (gogoproto.nullable) = false ];
}
56 changes: 56 additions & 0 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func GetQueryCmd() *cobra.Command {
GetCmdQueryFeederDelegation(),
GetCmdQueryMissCounter(),
GetCmdQuerySlashWindow(),
GetCmdListPrice(),
CmdShowPrice(),
)

return cmd
Expand Down Expand Up @@ -277,3 +279,57 @@ func GetCmdQuerySlashWindow() *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func GetCmdListPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "list-price",
Short: "list all price",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.PriceAll(cmd.Context(), &types.QueryAllPriceRequest{})
return cli.PrintOrErr(res, err, clientCtx)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdShowPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "show-price [asset] [source] [timestamp]",
Short: "shows a price",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)

timestamp, err := cmd.Flags().GetUint64(args[2])
if err != nil {
return err
}

queryClient := types.NewQueryClient(clientCtx)
params := &types.QueryGetPriceRequest{
Asset: args[0],
Source: args[1],
Timestamp: timestamp,
}

res, err := queryClient.Price(cmd.Context(), params)
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
44 changes: 44 additions & 0 deletions x/oracle/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,47 @@ func (q querier) ValidatorRewardSet(
Validators: validatorRewardSet,
}, nil
}

func (q querier) PriceAll(goCtx context.Context, req *types.QueryAllPriceRequest) (*types.QueryAllPriceResponse, error) {

Check failure on line 332 in x/oracle/keeper/grpc_query.go

View workflow job for this annotation

GitHub Actions / lint

The line is 121 characters long, which exceeds the maximum of 120 characters. (lll)
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

ctx := sdk.UnwrapSDKContext(goCtx)

prices := q.GetAllPrice(ctx)

return &types.QueryAllPriceResponse{Price: prices}, nil
}

func (q querier) Price(goCtx context.Context, req *types.QueryGetPriceRequest) (*types.QueryGetPriceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(goCtx)

// if both source and timestamp are defined, use specific value
if req.Source != "" && req.Timestamp != 0 {
val, found := q.GetPrice(ctx, req.Asset, req.Source, req.Timestamp)
if !found {
return nil, status.Error(codes.NotFound, "not found")
}
return &types.QueryGetPriceResponse{Price: val}, nil
}

// if source is specified use latest price from source
if req.Source != "" {
val, found := q.GetLatestPriceFromAssetAndSource(ctx, req.Asset, req.Source)
if !found {
return nil, status.Error(codes.NotFound, "not found")
}
return &types.QueryGetPriceResponse{Price: val}, nil
}

// find from any source if band source does not exist
val, found := q.GetLatestPriceFromAnySource(ctx, req.Asset)
if !found {
return nil, status.Error(codes.NotFound, "not found")
}
return &types.QueryGetPriceResponse{Price: val}, nil
}
Loading

0 comments on commit eb0c1a7

Please sign in to comment.