Skip to content

Commit

Permalink
feat(fungible): add query command to get all gas stability pool bal…
Browse files Browse the repository at this point in the history
…ances (#1247)

* remove contract address

* add all stability pool query

* add CLI command

* lint
  • Loading branch information
lumtis authored Oct 16, 2023
1 parent 820465e commit e48b355
Show file tree
Hide file tree
Showing 7 changed files with 741 additions and 74 deletions.
33 changes: 31 additions & 2 deletions docs/openapi/openapi.swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27851,6 +27851,21 @@ paths:
$ref: '#/definitions/googlerpcStatus'
tags:
- Query
/zeta-chain/zetacore/fungible/gas_stability_pool_balance:
get:
summary: Queries all gas stability pool balances.
operationId: Query_GasStabilityPoolBalanceAll
responses:
"200":
description: A successful response.
schema:
$ref: '#/definitions/fungibleQueryAllGasStabilityPoolBalanceResponse'
default:
description: An unexpected error response.
schema:
$ref: '#/definitions/googlerpcStatus'
tags:
- Query
/zeta-chain/zetacore/fungible/gas_stability_pool_balance/{chain_id}:
get:
summary: Queries the balance of a gas stability pool on a given chain.
Expand Down Expand Up @@ -50243,6 +50258,14 @@ definitions:
- VOTE_OPTION_ABSTAIN: VOTE_OPTION_ABSTAIN defines an abstain vote option.
- VOTE_OPTION_NO: VOTE_OPTION_NO defines a no vote option.
- VOTE_OPTION_NO_WITH_VETO: VOTE_OPTION_NO_WITH_VETO defines a no with veto vote option.
QueryAllGasStabilityPoolBalanceResponseBalance:
type: object
properties:
chain_id:
type: string
format: int64
balance:
type: string
bitcoinProof:
type: object
properties:
Expand Down Expand Up @@ -50880,6 +50903,14 @@ definitions:
$ref: '#/definitions/fungibleForeignCoins'
pagination:
$ref: '#/definitions/v1beta1PageResponse'
fungibleQueryAllGasStabilityPoolBalanceResponse:
type: object
properties:
balances:
type: array
items:
type: object
$ref: '#/definitions/QueryAllGasStabilityPoolBalanceResponseBalance'
fungibleQueryGetForeignCoinsResponse:
type: object
properties:
Expand All @@ -50895,8 +50926,6 @@ definitions:
fungibleQueryGetGasStabilityPoolBalanceResponse:
type: object
properties:
contract_address:
type: string
balance:
type: string
fungibleQueryGetSystemContractResponse:
Expand Down
16 changes: 15 additions & 1 deletion proto/fungible/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ service Query {
rpc GasStabilityPoolBalance(QueryGetGasStabilityPoolBalance) returns (QueryGetGasStabilityPoolBalanceResponse) {
option (google.api.http).get = "/zeta-chain/zetacore/fungible/gas_stability_pool_balance/{chain_id}";
}

// Queries all gas stability pool balances.
rpc GasStabilityPoolBalanceAll(QueryAllGasStabilityPoolBalance) returns (QueryAllGasStabilityPoolBalanceResponse) {
option (google.api.http).get = "/zeta-chain/zetacore/fungible/gas_stability_pool_balance";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -86,6 +91,15 @@ message QueryGetGasStabilityPoolBalance {
}

message QueryGetGasStabilityPoolBalanceResponse {
string contract_address = 1;
string balance = 2;
}

message QueryAllGasStabilityPoolBalance {}

message QueryAllGasStabilityPoolBalanceResponse {
message Balance {
int64 chain_id = 1;
string balance = 2;
}
repeated Balance balances = 1 [(gogoproto.nullable) = false];
}
8 changes: 2 additions & 6 deletions x/fungible/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,9 @@ package cli

import (
"fmt"
// "strings"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
// "github.com/cosmos/cosmos-sdk/client/flags"
// sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/spf13/cobra"
"github.com/zeta-chain/zetacore/x/fungible/types"
)

Expand All @@ -30,6 +25,7 @@ func GetQueryCmd(_ string) *cobra.Command {
CmdShowForeignCoins(),
CmdGasStabilityPoolAddress(),
CmdGasStabilityPoolBalance(),
CmdGasStabilityPoolBalances(),
CmdSystemContract(),
)

Expand Down
25 changes: 25 additions & 0 deletions x/fungible/client/cli/query_gas_stability_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,28 @@ func CmdGasStabilityPoolBalance() *cobra.Command {

return cmd
}

func CmdGasStabilityPoolBalances() *cobra.Command {
cmd := &cobra.Command{
Use: "gas-stability-pool-balances",
Short: "query all gas stability pool balances",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

queryClient := types.NewQueryClient(clientCtx)

res, err := queryClient.GasStabilityPoolBalanceAll(context.Background(), &types.QueryAllGasStabilityPoolBalance{})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddPaginationFlagsToCmd(cmd, cmd.Use)
flags.AddQueryFlagsToCmd(cmd)

return cmd
}
37 changes: 37 additions & 0 deletions x/fungible/keeper/grpc_gas_stability_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,40 @@ func (k Keeper) GasStabilityPoolBalance(

return &types.QueryGetGasStabilityPoolBalanceResponse{Balance: balance.String()}, nil
}

func (k Keeper) GasStabilityPoolBalanceAll(
c context.Context,
req *types.QueryAllGasStabilityPoolBalance,
) (*types.QueryAllGasStabilityPoolBalanceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}
ctx := sdk.UnwrapSDKContext(c)

// iterate supported chains
chains := k.observerKeeper.GetParams(ctx).GetSupportedChains()
balances := make([]types.QueryAllGasStabilityPoolBalanceResponse_Balance, 0, len(chains))
for _, chain := range chains {
if chain == nil {
return nil, status.Error(codes.Internal, "invalid chain")
}
chainID := chain.ChainId

balance, err := k.GetGasStabilityPoolBalance(ctx, chainID)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
if balance == nil {
return nil, status.Error(codes.NotFound, "no balance for the gas stability pool")
}

balances = append(balances, types.QueryAllGasStabilityPoolBalanceResponse_Balance{
ChainId: chainID,
Balance: balance.String(),
})
}

return &types.QueryAllGasStabilityPoolBalanceResponse{
Balances: balances,
}, nil
}
Loading

0 comments on commit e48b355

Please sign in to comment.