Skip to content

Commit

Permalink
Merge pull request #101 from onomyprotocol/dong/add-query-oracle
Browse files Browse the repository at this point in the history
Oracle,Vaults: add query cli oraclescript by symbol,..
  • Loading branch information
DongLieu authored Nov 12, 2024
2 parents 51af20c + 4c505c9 commit e1415a8
Show file tree
Hide file tree
Showing 11 changed files with 1,884 additions and 157 deletions.
17 changes: 16 additions & 1 deletion proto/reserve/oracle/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ service Query {
returns (QueryBandOracleRequestResponse) {
option (google.api.http).get =
"/reserve/oracle/band_oracle_request";
}
}

rpc QueryOracleScriptIdByDenom(QueryOracleScriptIdByDenomRequest)
returns (QueryOracleScriptIdByDenomResponse) {
option (google.api.http).get =
"/reserve/oracle/band_price_states";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -111,4 +117,13 @@ message QueryBandOracleRequestRequest {
// Query/QueryBandOracleRequest RPC method.
message QueryBandOracleRequestResponse {
BandOracleRequest band_oracle_request = 1;
}

message QueryOracleScriptIdByDenomRequest {
string denom = 1;
}


message QueryOracleScriptIdByDenomResponse {
repeated int64 oracle_script_ids = 1;
}
27 changes: 26 additions & 1 deletion proto/reserve/vaults/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ service Query {
option (google.api.http).get = "/reserve/vaults/params";
}

rpc QueryCollateralsByDenom(QueryCollateralsByDenomRequest) returns (QueryCollateralsByDenomResponse){
rpc QueryCollateralsByDenom(QueryCollateralsByDenomRequest) returns (QueryCollateralsByDenomResponse){
option (google.api.http).get = "/reserve/vaults/params";
}

rpc QueryCollateralsByMintDenom(QueryCollateralsByMintDenomRequest) returns (QueryCollateralsByMintDenomResponse){
option (google.api.http).get = "/reserve/vaults/params";
}

rpc QueryCollateralsByDenomMintDenom(QueryCollateralsByDenomMintDenomRequest) returns (QueryCollateralsByDenomMintDenomResponse){
option (google.api.http).get = "/reserve/vaults/params";
}

Expand Down Expand Up @@ -76,6 +84,23 @@ message QueryCollateralsByDenomResponse{
repeated VaultMamager all_vault_mamager_by_denom = 1;
}

message QueryCollateralsByMintDenomRequest {
string mint_denom = 1;
}

message QueryCollateralsByMintDenomResponse{
repeated VaultMamager all_vault_mamager_by_mint_denom = 1;
}

message QueryCollateralsByDenomMintDenomRequest {
string denom = 1;
string mint_denom = 2;
}

message QueryCollateralsByDenomMintDenomResponse{
VaultMamager vault_mamager = 1;
}

message QueryVaultByOwnerRequest {
string address = 1;
}
Expand Down
27 changes: 26 additions & 1 deletion x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cli

import (
"fmt"
"context"
"fmt"
"strconv"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -31,6 +31,7 @@ func GetQueryCmd() *cobra.Command {
GetBandParams(),
GetBandOracleRequestParams(),
GetBandOracleRequest(),
GetQueryOracleScriptIdByDenomCmd(),
)
return cmd
}
Expand Down Expand Up @@ -183,3 +184,27 @@ func GetBandOracleRequest() *cobra.Command {
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func GetQueryOracleScriptIdByDenomCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "oracle-scriptId-by-symbol [symbol]",
Short: "Get oracle-scriptId by symbol",
Args: cobra.ExactArgs(1),
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.QueryOracleScriptIdByDenom(context.Background(), &types.QueryOracleScriptIdByDenomRequest{Denom: args[0]})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
return cmd
}
18 changes: 18 additions & 0 deletions x/oracle/keeper/band_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,24 @@ func (k Keeper) GetAllBandOracleRequests(ctx context.Context) []*types.BandOracl
return bandOracleRequests
}

func (k Keeper) IteratorOracleRequests(ctx context.Context, fn func(bandOracleRequest types.BandOracleRequest) bool) error {
kvStore := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
bandOracleRequestStore := prefix.NewStore(kvStore, types.BandOracleRequestIDKey)

iterator := bandOracleRequestStore.Iterator(nil, nil)
defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var bandOracleRequest types.BandOracleRequest
k.cdc.MustUnmarshal(iterator.Value(), &bandOracleRequest)
if fn(bandOracleRequest) {
return nil
}
}

return nil
}

// GetBandPriceState reads the stored band ibc price state.
func (k *Keeper) GetBandPriceState(ctx context.Context, symbol string) *types.BandPriceState {
var priceState types.BandPriceState
Expand Down
21 changes: 21 additions & 0 deletions x/oracle/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"slices"
"strconv"

errors "cosmossdk.io/errors"
Expand Down Expand Up @@ -69,3 +70,23 @@ func (k Keeper) BandOracleRequest(c context.Context, q *types.QueryBandOracleReq
}
return res, nil
}

func (k Keeper) QueryOracleScriptIdByDenom(c context.Context, q *types.QueryOracleScriptIdByDenomRequest) (*types.QueryOracleScriptIdByDenomResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
allIds := []int64{}

err := k.IteratorOracleRequests(ctx, func(bandOracleRequest types.BandOracleRequest) bool {
if slices.Contains(bandOracleRequest.Symbols, q.Denom) {
allIds = append(allIds, bandOracleRequest.OracleScriptId)
}
return false
})
if err != nil {
return nil, err
}
res := &types.QueryOracleScriptIdByDenomResponse{

OracleScriptIds: allIds,
}
return res, nil
}
Loading

0 comments on commit e1415a8

Please sign in to comment.