Skip to content

Commit

Permalink
added rewards query
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Oct 19, 2022
1 parent 26ffd79 commit b34b5f1
Show file tree
Hide file tree
Showing 10 changed files with 983 additions and 89 deletions.
170 changes: 133 additions & 37 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ paths:
will be redirected to the distribution rewards pool
total_tokens:
type: string
total_shares:
type: string
title: 'key: denom value: AllianceAsset'
pagination:
type: object
Expand Down Expand Up @@ -174,15 +172,6 @@ paths:
type: string
format: date-time
title: Last application of `take_rate` on assets
global_reward_indices:
type: array
items:
type: object
properties:
denom:
type: string
index:
type: string
default:
description: An unexpected error response.
schema:
Expand All @@ -203,6 +192,122 @@ paths:
additionalProperties: {}
tags:
- Query
'/terra/alliances/{delegator_addr_1}/{validator_addr}/{denom}':
get:
summary: 'Query for rewards by delegator addr, validator_addr and denom'
operationId: AllianceAllianceAllianceDelegationRewards
responses:
'200':
description: A successful response.
schema:
type: object
properties:
rewards:
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.
default:
description: An unexpected error response.
schema:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
details:
type: array
items:
type: object
properties:
'@type':
type: string
additionalProperties: {}
parameters:
- name: delegator_addr_1
in: path
required: true
type: string
- name: validator_addr
in: path
required: true
type: string
- name: denom
in: path
required: true
type: string
- name: pagination.key
description: |-
key is a value returned in PageResponse.next_key to begin
querying the next page most efficiently. Only one of offset or key
should be set.
in: query
required: false
type: string
format: byte
- name: pagination.offset
description: >-
offset is a numeric offset that can be used when key is unavailable.

It is less efficient than using key. Only one of offset or key
should

be set.
in: query
required: false
type: string
format: uint64
- name: pagination.limit
description: >-
limit is the total number of results to be returned in the result
page.

If left empty it will default to a value to be set by each app.
in: query
required: false
type: string
format: uint64
- name: pagination.count_total
description: >-
count_total is set to true to indicate that the result set should
include

a count of the total number of items available for pagination in
UIs.

count_total is only respected when offset is used. It is ignored
when key

is set.
in: query
required: false
type: boolean
- name: pagination.reverse
description: >-
reverse is set to true if results are to be returned in the
descending order.


Since: cosmos-sdk 0.43
in: query
required: false
type: boolean
tags:
- Query
'/terra/alliances/{delegator_addr}':
get:
summary: Query all paginated alliance delegations for a delegator addr
Expand Down Expand Up @@ -744,8 +849,6 @@ paths:
will be redirected to the distribution rewards pool
total_tokens:
type: string
total_shares:
type: string
title: 'key: denom value: AllianceAsset'
default:
description: An unexpected error response.
Expand Down Expand Up @@ -44914,9 +45017,24 @@ definitions:
will be redirected to the distribution rewards pool
total_tokens:
type: string
total_shares:
type: string
title: 'key: denom value: AllianceAsset'
alliance.alliance.AllianceDelegationRewardsResponse:
type: object
properties:
rewards:
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.
alliance.alliance.Delegation:
type: object
properties:
Expand Down Expand Up @@ -45009,15 +45127,6 @@ definitions:
type: string
format: date-time
title: Last application of `take_rate` on assets
global_reward_indices:
type: array
items:
type: object
properties:
denom:
type: string
index:
type: string
alliance.alliance.QueryAllianceDelegationResponse:
type: object
properties:
Expand Down Expand Up @@ -45102,8 +45211,6 @@ definitions:
will be redirected to the distribution rewards pool
total_tokens:
type: string
total_shares:
type: string
title: 'key: denom value: AllianceAsset'
alliance.alliance.QueryAlliancesDelegationsResponse:
type: object
Expand Down Expand Up @@ -45219,8 +45326,6 @@ definitions:
will be redirected to the distribution rewards pool
total_tokens:
type: string
total_shares:
type: string
title: 'key: denom value: AllianceAsset'
pagination:
type: object
Expand Down Expand Up @@ -45263,15 +45368,6 @@ definitions:
type: string
format: date-time
title: Last application of `take_rate` on assets
global_reward_indices:
type: array
items:
type: object
properties:
denom:
type: string
index:
type: string
alliance.alliance.RewardIndex:
type: object
properties:
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ require (
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0 // indirect
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c // indirect
github.com/gtank/merlin v0.1.1 // indirect
github.com/gtank/ristretto255 v0.1.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,6 @@ github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t
github.com/grpc-ecosystem/grpc-gateway v1.9.5/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0 h1:gmcG1KaJ57LophUzW0Hy8NmPhnMZb4M0+kPpLofRdBo=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0 h1:kr3j8iIMR4ywO/O0rvksXaJvauGGCMg2zAZIiNZ9uIQ=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.12.0/go.mod h1:ummNFgdgLhhX7aIiy35vVmQNS0rWXknfPE0qe6fmFXg=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8ZofjG1Y75iExal34USq5p+wiN1tpie8IrU=
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/gtank/merlin v0.1.1-0.20191105220539-8318aed1a79f/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s=
Expand Down
27 changes: 27 additions & 0 deletions proto/alliance/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "google/api/annotations.proto";
import "cosmos/base/query/v1beta1/pagination.proto";
import "alliance/params.proto";
import "alliance/alliance.proto";
import "cosmos/base/v1beta1/coin.proto";
import "alliance/delegations.proto";

option go_package = "alliance/x/alliance/types";
Expand Down Expand Up @@ -39,6 +40,11 @@ service Query {
rpc AllianceDelegation(QueryAllianceDelegationRequest) returns (QueryAllianceDelegationResponse) {
option (google.api.http).get = "/terra/alliances/{delegator_addr}/{validator_addr}/{denom}";
}

// Query for rewards by delegator addr, validator_addr and denom
rpc AllianceDelegationRewards(AllianceDelegationRewardsRequest) returns (AllianceDelegationRewardsResponse) {
option (google.api.http).get = "/terra/alliances/{delegator_addr}/{validator_addr}/{denom}";
}
}

// Params
Expand Down Expand Up @@ -104,4 +110,25 @@ message QueryAllianceDelegationRequest {

message QueryAllianceDelegationResponse {
DelegationResponse delegation = 1 [(gogoproto.nullable) = false];
}

// AllianceDelegation
message AllianceDelegationRewardsRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

string delegator_addr = 1;
string validator_addr = 2;
string denom = 3;
cosmos.base.query.v1beta1.PageRequest pagination = 4;
}

message AllianceDelegationRewardsResponse {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

repeated cosmos.base.v1beta1.Coin rewards = 1 [
(gogoproto.nullable) = false,
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Coin"
];
}
1 change: 1 addition & 0 deletions x/alliance/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func GetQueryCmd() *cobra.Command {
cmd.AddCommand(CmdQueryAlliancesDelegation())
cmd.AddCommand(CmdQueryAlliancesDelegationByValidator())
cmd.AddCommand(CmdQueryAllianceDelegation())
cmd.AddCommand(CmdQueryRewards())

return cmd
}
42 changes: 42 additions & 0 deletions x/alliance/client/cli/query_rewards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cli

import (
"context"

"alliance/x/alliance/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"
)

func CmdQueryRewards() *cobra.Command {
cmd := &cobra.Command{
Use: "rewards delegator_addr validator_addr denom",
Short: "Query module parameters",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
delegatorAddr := args[0]
validatorAddr := args[1]
denom := args[2]
ctx := client.GetClientContextFromCmd(cmd)
query := types.NewQueryClient(ctx)
params := &types.AllianceDelegationRewardsRequest{
DelegatorAddr: delegatorAddr,
ValidatorAddr: validatorAddr,
Denom: denom,
}

res, err := query.AllianceDelegationRewards(context.Background(), params)
if err != nil {
return err
}

return ctx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}
42 changes: 42 additions & 0 deletions x/alliance/keeper/grpc_query_rewards.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package keeper

import (
"alliance/x/alliance/types"
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

func (k Keeper) AllianceDelegationRewards(context context.Context, request *types.AllianceDelegationRewardsRequest) (*types.AllianceDelegationRewardsResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
delAddr, err := sdk.AccAddressFromBech32(request.DelegatorAddr)
if err != nil {
return nil, err
}
valAddr, err := sdk.ValAddressFromBech32(request.ValidatorAddr)
if err != nil {
return nil, err
}
_, found := k.GetAssetByDenom(ctx, request.Denom)
if !found {
return nil, types.ErrUnknownAsset
}

val, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return nil, stakingtypes.ErrNoValidatorFound
}

_, found = k.GetDelegation(ctx, delAddr, val, request.Denom)
if !found {
return nil, stakingtypes.ErrNoDelegation
}

rewards, err := k.ClaimDelegationRewards(ctx, delAddr, val, request.Denom)
if err != nil {
return nil, err
}
return &types.AllianceDelegationRewardsResponse{
Rewards: rewards,
}, nil
}
Loading

0 comments on commit b34b5f1

Please sign in to comment.