Skip to content

Commit

Permalink
add querry collateral for vaults
Browse files Browse the repository at this point in the history
  • Loading branch information
DongLieu committed Nov 12, 2024
1 parent 4770d12 commit 4c505c9
Show file tree
Hide file tree
Showing 6 changed files with 1,217 additions and 115 deletions.
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
4 changes: 2 additions & 2 deletions x/oracle/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func GetBandOracleRequest() *cobra.Command {

func GetQueryOracleScriptIdByDenomCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "oracle-scriptId-by-denom [denom]",
Short: "Get oracle-scriptId by denom",
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)
Expand Down
45 changes: 44 additions & 1 deletion x/vaults/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func CmdQueryAllCollateral() *cobra.Command {
func CmdQueryCollateralsByDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "collateral-by-denom [denom]",
Short: "show all collateral",
Short: "show all collateral by denom",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
Expand All @@ -75,6 +75,49 @@ func CmdQueryCollateralsByDenom() *cobra.Command {
return cmd
}

func CmdQueryCollateralsByMintDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "collateral-by-mint-denom [mint-denom]",
Short: "show all collateral by mint denom",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueryCollateralsByMintDenom(context.Background(), &types.QueryCollateralsByMintDenomRequest{MintDenom: args[0]})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
func CmdQueryCollateralsByDenomMintDenom() *cobra.Command {
cmd := &cobra.Command{
Use: "collateral-by-denom [denom] [mint-denom]",
Short: "show collateral by denom and mint denom",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.QueryCollateralsByDenomMintDenom(context.Background(), &types.QueryCollateralsByDenomMintDenomRequest{Denom: args[0], MintDenom: args[1]})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdQueryAllVaults() *cobra.Command {
cmd := &cobra.Command{
Use: "all-vaults",
Expand Down
33 changes: 33 additions & 0 deletions x/vaults/keeper/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,39 @@ func (q queryServer) QueryCollateralsByDenom(ctx context.Context, req *types.Que
}, err
}

func (q queryServer) QueryCollateralsByMintDenom(ctx context.Context, req *types.QueryCollateralsByMintDenomRequest) (*types.QueryCollateralsByMintDenomResponse, error) {
allCollateral := []*types.VaultMamager{}

err := q.keeper.VaultsManager.Walk(ctx, nil, func(key string, value types.VaultMamager) (stop bool, err error) {
if value.Params.MintDenom == req.MintDenom {
allCollateral = append(allCollateral, &value)
}

return false, nil
})

return &types.QueryCollateralsByMintDenomResponse{
AllVaultMamagerByMintDenom: allCollateral,
}, err
}

func (q queryServer) QueryCollateralsByDenomMintDenom(ctx context.Context, req *types.QueryCollateralsByDenomMintDenomRequest) (*types.QueryCollateralsByDenomMintDenomResponse, error) {
var collateral *types.VaultMamager

err := q.keeper.VaultsManager.Walk(ctx, nil, func(key string, value types.VaultMamager) (stop bool, err error) {
if value.Denom == req.Denom && value.Params.MintDenom == req.MintDenom {
collateral = &value
return true, nil
}

return false, nil
})

return &types.QueryCollateralsByDenomMintDenomResponse{
VaultMamager: collateral,
}, err
}

func (q queryServer) QueryAllVaults(ctx context.Context, req *types.QueryAllVaultsRequest) (*types.QueryAllVaultsResponse, error) {
allVaults := []*types.Vault{}

Expand Down
Loading

0 comments on commit 4c505c9

Please sign in to comment.