Skip to content

Commit

Permalink
feat: add total delegation balance of a delegator query (#352)
Browse files Browse the repository at this point in the history
* add delegator-sum-delegation-balances query

* change name to total-delegation-balance
  • Loading branch information
beer-1 authored Feb 28, 2025
1 parent f5cd97d commit 8188d3f
Show file tree
Hide file tree
Showing 8 changed files with 954 additions and 82 deletions.
4 changes: 4 additions & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,10 @@ func NewAppKeeper(
Request: &stakingtypes.QueryDelegatorDelegationsRequest{},
Response: &stakingtypes.QueryDelegatorDelegationsResponse{},
}
queryWhitelist.Stargate["/initia.mstaking.v1.Query/DelegatorTotalDelegationBalance"] = movetypes.ProtoSet{
Request: &stakingtypes.QueryDelegatorTotalDelegationBalanceRequest{},
Response: &stakingtypes.QueryDelegatorTotalDelegationBalanceResponse{},
}
queryWhitelist.Stargate["/initia.mstaking.v1.Query/Delegation"] = movetypes.ProtoSet{
Request: &stakingtypes.QueryDelegationRequest{},
Response: &stakingtypes.QueryDelegationResponse{},
Expand Down
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

264 changes: 264 additions & 0 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44253,6 +44253,247 @@ paths:
type: boolean
tags:
- Query
/initia/mstaking/v1/delegators/{delegator_addr}/total_delegation_balance:
get:
summary: >-
DelegatorTotalDelegationBalance queries sum of all the delegations'
balance of a

delegator.
operationId: DelegatorTotalDelegationBalance
responses:
'200':
description: A successful response.
schema:
type: object
properties:
balance:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: >-
Coin defines a token with a denomination and an amount.


NOTE: The amount field is an Int which implements the custom
method

signatures required by gogoproto.
description: >-
balance defines the sum of all the delegations' balance of a
delegator.
description: >-
QueryDelegatorTotalDelegationBalanceResponse is response type for
the

Query/DelegatorTotalDelegationBalance RPC method.
default:
description: An unexpected error response.
schema:
type: object
properties:
error:
type: string
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
type_url:
type: string
description: >-
A URL/resource name that uniquely identifies the type of
the serialized

protocol buffer message. This string must contain at
least

one "/" character. The last segment of the URL's path
must represent

the fully qualified name of the type (as in

`path/google.protobuf.Duration`). The name should be in
a canonical form

(e.g., leading "." is not accepted).


In practice, teams usually precompile into the binary
all types that they

expect it to use in the context of Any. However, for
URLs which use the

scheme `http`, `https`, or no scheme, one can optionally
set up a type

server that maps type URLs to message definitions as
follows:


* If no scheme is provided, `https` is assumed.

* An HTTP GET on the URL must yield a
[google.protobuf.Type][]
value in binary format, or produce an error.
* Applications are allowed to cache lookup results based
on the
URL, or have them precompiled into a binary to avoid any
lookup. Therefore, binary compatibility needs to be preserved
on changes to types. (Use versioned type names to manage
breaking changes.)

Note: this functionality is not currently available in
the official

protobuf release, and it is not used for type URLs
beginning with

type.googleapis.com.


Schemes other than `http`, `https` (or the empty scheme)
might be

used with implementation specific semantics.
value:
type: string
format: byte
description: >-
Must be a valid serialized protocol buffer of the above
specified type.
description: >-
`Any` contains an arbitrary serialized protocol buffer
message along with a

URL that describes the type of the serialized message.


Protobuf library provides support to pack/unpack Any values
in the form

of utility functions or additional generated methods of the
Any type.


Example 1: Pack and unpack a message in C++.

Foo foo = ...;
Any any;
any.PackFrom(foo);
...
if (any.UnpackTo(&foo)) {
...
}

Example 2: Pack and unpack a message in Java.

Foo foo = ...;
Any any = Any.pack(foo);
...
if (any.is(Foo.class)) {
foo = any.unpack(Foo.class);
}
// or ...
if (any.isSameTypeAs(Foo.getDefaultInstance())) {
foo = any.unpack(Foo.getDefaultInstance());
}

Example 3: Pack and unpack a message in Python.

foo = Foo(...)
any = Any()
any.Pack(foo)
...
if any.Is(Foo.DESCRIPTOR):
any.Unpack(foo)
...

Example 4: Pack and unpack a message in Go

foo := &pb.Foo{...}
any, err := anypb.New(foo)
if err != nil {
...
}
...
foo := &pb.Foo{}
if err := any.UnmarshalTo(foo); err != nil {
...
}

The pack methods provided by protobuf library will by
default use

'type.googleapis.com/full.type.name' as the type URL and the
unpack

methods only use the fully qualified type name after the
last '/'

in the type URL, for example "foo.bar.com/x/y.z" will yield
type

name "y.z".


JSON


The JSON representation of an `Any` value uses the regular

representation of the deserialized, embedded message, with
an

additional field `@type` which contains the type URL.
Example:

package google.profile;
message Person {
string first_name = 1;
string last_name = 2;
}

{
"@type": "type.googleapis.com/google.profile.Person",
"firstName": <string>,
"lastName": <string>
}

If the embedded message type is well-known and has a custom
JSON

representation, that representation will be embedded adding
a field

`value` which holds the custom JSON in addition to the
`@type`

field. Example (for message [google.protobuf.Duration][]):

{
"@type": "type.googleapis.com/google.protobuf.Duration",
"value": "1.212s"
}
parameters:
- name: delegator_addr
description: delegator_addr defines the delegator address to query for.
in: path
required: true
type: string
tags:
- Query
/initia/mstaking/v1/delegators/{delegator_addr}/unbonding_delegations:
get:
summary: >-
Expand Down Expand Up @@ -83443,6 +83684,29 @@ definitions:
description: |-
QueryDelegatorDelegationsResponse is response type for the
Query/DelegatorDelegations RPC method.
initia.mstaking.v1.QueryDelegatorTotalDelegationBalanceResponse:
type: object
properties:
balance:
type: array
items:
type: object
properties:
denom:
type: string
amount:
type: string
description: |-
Coin defines a token with a denomination and an amount.

NOTE: The amount field is an Int which implements the custom method
signatures required by gogoproto.
description: >-
balance defines the sum of all the delegations' balance of a
delegator.
description: |-
QueryDelegatorTotalDelegationBalanceResponse is response type for the
Query/DelegatorTotalDelegationBalance RPC method.
initia.mstaking.v1.QueryDelegatorUnbondingDelegationsResponse:
type: object
properties:
Expand Down
25 changes: 25 additions & 0 deletions proto/initia/mstaking/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ syntax = "proto3";
package initia.mstaking.v1;

import "cosmos/base/query/v1beta1/pagination.proto";
import "cosmos/base/v1beta1/coin.proto";
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "initia/mstaking/v1/staking.proto";
Expand Down Expand Up @@ -79,6 +80,12 @@ service Query {
"{validator_addr}";
}

// DelegatorTotalDelegationBalance queries sum of all the delegations' balance of a
// delegator.
rpc DelegatorTotalDelegationBalance(QueryDelegatorTotalDelegationBalanceRequest) returns (QueryDelegatorTotalDelegationBalanceResponse) {
option (google.api.http).get = "/initia/mstaking/v1/delegators/{delegator_addr}/total_delegation_balance";
}

// Pool queries the pool info.
rpc Pool(QueryPoolRequest) returns (QueryPoolResponse) {
option (google.api.http).get = "/initia/mstaking/v1/pool";
Expand Down Expand Up @@ -315,6 +322,24 @@ message QueryDelegatorValidatorResponse {
Validator validator = 1 [(gogoproto.nullable) = false];
}

// QueryDelegatorTotalDelegationBalanceRequest is request type for the
// Query/DelegatorTotalDelegationBalance RPC method.
message QueryDelegatorTotalDelegationBalanceRequest {
// delegator_addr defines the delegator address to query for.
string delegator_addr = 1;
}

// QueryDelegatorTotalDelegationBalanceResponse is response type for the
// Query/DelegatorTotalDelegationBalance RPC method.
message QueryDelegatorTotalDelegationBalanceResponse {
// balance defines the sum of all the delegations' balance of a delegator.
repeated cosmos.base.v1beta1.Coin balance = 1 [
(gogoproto.moretags) = "yaml:\"balance\"",
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins",
(gogoproto.nullable) = false
];
}

// QueryPoolRequest is request type for the Query/Pool RPC method.
message QueryPoolRequest {}

Expand Down
32 changes: 32 additions & 0 deletions x/mstaking/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,38 @@ func (q Querier) DelegatorValidators(ctx context.Context, req *types.QueryDelega
return &types.QueryDelegatorValidatorsResponse{Validators: validators, Pagination: pageRes}, nil
}

func (q Querier) DelegatorTotalDelegationBalance(ctx context.Context, req *types.QueryDelegatorTotalDelegationBalanceRequest) (*types.QueryDelegatorTotalDelegationBalanceResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}

if req.DelegatorAddr == "" {
return nil, status.Error(codes.InvalidArgument, "delegator address cannot be empty")
}

delAddr, err := q.authKeeper.AddressCodec().StringToBytes(req.DelegatorAddr)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

delegations, err := q.GetAllDelegatorDelegations(ctx, delAddr)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

delegationResps, err := delegationsToDelegationResponses(ctx, q.Keeper, delegations)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

var allBalances sdk.Coins
for _, delegationResp := range delegationResps {
allBalances = allBalances.Add(delegationResp.Balance...)
}

return &types.QueryDelegatorTotalDelegationBalanceResponse{Balance: allBalances}, nil
}

// Pool queries the pool info
func (q Querier) Pool(ctx context.Context, _ *types.QueryPoolRequest) (*types.QueryPoolResponse, error) {
bondedPool := q.GetBondedPool(ctx)
Expand Down
Loading

0 comments on commit 8188d3f

Please sign in to comment.