Skip to content

Commit

Permalink
Merge pull request #181 from terra-money/fix/query/special/chars
Browse files Browse the repository at this point in the history
fix: query special characters
  • Loading branch information
emidev98 authored May 4, 2023
2 parents c97b7c9 + a7fddc5 commit 3742bab
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 95 deletions.
6 changes: 3 additions & 3 deletions docs/proto/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -958,16 +958,16 @@ Params
| ----------- | ------------ | ------------- | ------------| ------- | -------- |
| `Params` | [QueryParamsRequest](#alliance.alliance.QueryParamsRequest) | [QueryParamsResponse](#alliance.alliance.QueryParamsResponse) | | GET|/terra/alliances/params|
| `Alliances` | [QueryAlliancesRequest](#alliance.alliance.QueryAlliancesRequest) | [QueryAlliancesResponse](#alliance.alliance.QueryAlliancesResponse) | Query paginated alliances | GET|/terra/alliances|
| `IBCAlliance` | [QueryIBCAllianceRequest](#alliance.alliance.QueryIBCAllianceRequest) | [QueryAllianceResponse](#alliance.alliance.QueryAllianceResponse) | Query a specific alliance by ibc hash | GET|/terra/alliances/ibc/{hash}|
| `IBCAlliance` | [QueryIBCAllianceRequest](#alliance.alliance.QueryIBCAllianceRequest) | [QueryAllianceResponse](#alliance.alliance.QueryAllianceResponse) | Query a specific alliance by ibc hash @deprecated: this endpoint will be replaced for by the encoded version of the denom e.g.: GET:/terra/alliances/ibc%2Falliance | GET|/terra/alliances/ibc/{hash}|
| `AllAlliancesDelegations` | [QueryAllAlliancesDelegationsRequest](#alliance.alliance.QueryAllAlliancesDelegationsRequest) | [QueryAlliancesDelegationsResponse](#alliance.alliance.QueryAlliancesDelegationsResponse) | Query all paginated alliance delegations | GET|/terra/alliances/delegations|
| `AllianceValidator` | [QueryAllianceValidatorRequest](#alliance.alliance.QueryAllianceValidatorRequest) | [QueryAllianceValidatorResponse](#alliance.alliance.QueryAllianceValidatorResponse) | Query alliance validator | GET|/terra/alliances/validators/{validator_addr}|
| `AllAllianceValidators` | [QueryAllAllianceValidatorsRequest](#alliance.alliance.QueryAllAllianceValidatorsRequest) | [QueryAllianceValidatorsResponse](#alliance.alliance.QueryAllianceValidatorsResponse) | Query all paginated alliance validators | GET|/terra/alliances/validators|
| `AlliancesDelegation` | [QueryAlliancesDelegationsRequest](#alliance.alliance.QueryAlliancesDelegationsRequest) | [QueryAlliancesDelegationsResponse](#alliance.alliance.QueryAlliancesDelegationsResponse) | Query all paginated alliance delegations for a delegator addr | GET|/terra/alliances/delegations/{delegator_addr}|
| `AlliancesDelegationByValidator` | [QueryAlliancesDelegationByValidatorRequest](#alliance.alliance.QueryAlliancesDelegationByValidatorRequest) | [QueryAlliancesDelegationsResponse](#alliance.alliance.QueryAlliancesDelegationsResponse) | Query all paginated alliance delegations for a delegator addr and validator_addr | GET|/terra/alliances/delegations/{delegator_addr}/{validator_addr}|
| `AllianceDelegation` | [QueryAllianceDelegationRequest](#alliance.alliance.QueryAllianceDelegationRequest) | [QueryAllianceDelegationResponse](#alliance.alliance.QueryAllianceDelegationResponse) | Query a delegation to an alliance by delegator addr, validator_addr and denom | GET|/terra/alliances/delegations/{delegator_addr}/{validator_addr}/{denom}|
| `IBCAllianceDelegation` | [QueryIBCAllianceDelegationRequest](#alliance.alliance.QueryIBCAllianceDelegationRequest) | [QueryAllianceDelegationResponse](#alliance.alliance.QueryAllianceDelegationResponse) | Query a delegation to an alliance by delegator addr, validator_addr and denom | GET|/terra/alliances/delegations/{delegator_addr}/{validator_addr}/ibc/{hash}|
| `IBCAllianceDelegation` | [QueryIBCAllianceDelegationRequest](#alliance.alliance.QueryIBCAllianceDelegationRequest) | [QueryAllianceDelegationResponse](#alliance.alliance.QueryAllianceDelegationResponse) | Query a delegation to an alliance by delegator addr, validator_addr and denom @deprecated: this endpoint will be replaced for by the encoded version of the denom e.g.: GET:/terra/alliances/terradr1231/terravaloper41234/ibc%2Falliance | GET|/terra/alliances/delegations/{delegator_addr}/{validator_addr}/ibc/{hash}|
| `AllianceDelegationRewards` | [QueryAllianceDelegationRewardsRequest](#alliance.alliance.QueryAllianceDelegationRewardsRequest) | [QueryAllianceDelegationRewardsResponse](#alliance.alliance.QueryAllianceDelegationRewardsResponse) | Query for rewards by delegator addr, validator_addr and denom | GET|/terra/alliances/rewards/{delegator_addr}/{validator_addr}/{denom}|
| `IBCAllianceDelegationRewards` | [QueryIBCAllianceDelegationRewardsRequest](#alliance.alliance.QueryIBCAllianceDelegationRewardsRequest) | [QueryAllianceDelegationRewardsResponse](#alliance.alliance.QueryAllianceDelegationRewardsResponse) | Query for rewards by delegator addr, validator_addr and denom | GET|/terra/alliances/rewards/{delegator_addr}/{validator_addr}/ibc/{hash}|
| `IBCAllianceDelegationRewards` | [QueryIBCAllianceDelegationRewardsRequest](#alliance.alliance.QueryIBCAllianceDelegationRewardsRequest) | [QueryAllianceDelegationRewardsResponse](#alliance.alliance.QueryAllianceDelegationRewardsResponse) | Query for rewards by delegator addr, validator_addr and denom @deprecated: this endpoint will be replaced for by the encoded version of the denom e.g.: GET:/terra/alliances/terradr1231/terravaloper41234/ibc%2Falliance | GET|/terra/alliances/rewards/{delegator_addr}/{validator_addr}/ibc/{hash}|
| `Alliance` | [QueryAllianceRequest](#alliance.alliance.QueryAllianceRequest) | [QueryAllianceResponse](#alliance.alliance.QueryAllianceResponse) | Query a specific alliance by denom | GET|/terra/alliances/{denom}|

<!-- end services -->
Expand Down
24 changes: 24 additions & 0 deletions proto/alliance/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ service Query {
}

// Query a specific alliance by ibc hash
// @deprecated: this endpoint will be replaced for by the encoded version
// of the denom e.g.: GET:/terra/alliances/ibc%2Falliance
rpc IBCAlliance(QueryIBCAllianceRequest) returns (QueryAllianceResponse) {
// Deprecated. Please use the default endpoint
// with the encoded denom e.g from ibc/alliance to ibc%2Falliance
option deprecated = true;
option (google.api.http).get = "/terra/alliances/ibc/{hash}";
}

Expand Down Expand Up @@ -57,7 +62,12 @@ service Query {
}

// Query a delegation to an alliance by delegator addr, validator_addr and denom
// @deprecated: this endpoint will be replaced for by the encoded version
// of the denom e.g.: GET:/terra/alliances/terradr1231/terravaloper41234/ibc%2Falliance
rpc IBCAllianceDelegation(QueryIBCAllianceDelegationRequest) returns (QueryAllianceDelegationResponse) {
// Deprecated. Please use the default endpoint
// with the encoded denom e.g from ibc/alliance to ibc%2Falliance
option deprecated = true;
option (google.api.http).get = "/terra/alliances/delegations/{delegator_addr}/{validator_addr}/ibc/{hash}";
}

Expand All @@ -66,7 +76,12 @@ service Query {
option (google.api.http).get = "/terra/alliances/rewards/{delegator_addr}/{validator_addr}/{denom}";
}
// Query for rewards by delegator addr, validator_addr and denom
// @deprecated: this endpoint will be replaced for by the encoded version
// of the denom e.g.: GET:/terra/alliances/terradr1231/terravaloper41234/ibc%2Falliance
rpc IBCAllianceDelegationRewards(QueryIBCAllianceDelegationRewardsRequest) returns (QueryAllianceDelegationRewardsResponse) {
// Deprecated. Please use the default endpoint
// with the encoded denom e.g from ibc/alliance to ibc%2Falliance
option deprecated = true;
option (google.api.http).get = "/terra/alliances/rewards/{delegator_addr}/{validator_addr}/ibc/{hash}";
}

Expand Down Expand Up @@ -103,6 +118,9 @@ message QueryAllianceResponse {
}

message QueryIBCAllianceRequest {
// Deprecated. Please use the default endpoint
// with the encoded denom e.g from
option deprecated = true;
string hash = 1;
}

Expand Down Expand Up @@ -173,6 +191,9 @@ message QueryAllianceDelegationRequest {
}

message QueryIBCAllianceDelegationRequest {
// Deprecated. Please use the default endpoint
// with the encoded denom e.g from
option deprecated = true;
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand All @@ -198,6 +219,9 @@ message QueryAllianceDelegationRewardsRequest {
}

message QueryIBCAllianceDelegationRewardsRequest {
// Deprecated. Please use the default endpoint
// with the encoded denom e.g from
option deprecated = true;
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

Expand Down
32 changes: 23 additions & 9 deletions x/alliance/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper
import (
"context"
"fmt"
"net/url"

"github.com/terra-money/alliance/x/alliance/types"

Expand Down Expand Up @@ -196,6 +197,11 @@ func (k QueryServer) Alliances(c context.Context, req *types.QueryAlliancesReque
}

func (k QueryServer) Alliance(c context.Context, req *types.QueryAllianceRequest) (*types.QueryAllianceResponse, error) {
decodedDenom, err := url.QueryUnescape(req.Denom)
if err == nil {
req.Denom = decodedDenom
}

var asset types.AllianceAsset

// Get context with the information about the environment
Expand All @@ -214,24 +220,28 @@ func (k QueryServer) Alliance(c context.Context, req *types.QueryAllianceRequest
}, nil
}

func (k QueryServer) IBCAlliance(c context.Context, request *types.QueryIBCAllianceRequest) (*types.QueryAllianceResponse, error) {
func (k QueryServer) IBCAlliance(c context.Context, request *types.QueryIBCAllianceRequest) (*types.QueryAllianceResponse, error) { //nolint:staticcheck // SA1019: types.QueryIBCAllianceRequest is deprecated
req := types.QueryAllianceRequest{
Denom: "ibc/" + request.Hash,
}
return k.Alliance(c, &req)
}

func (k QueryServer) AllianceDelegationRewards(context context.Context, request *types.QueryAllianceDelegationRewardsRequest) (*types.QueryAllianceDelegationRewardsResponse, error) {
func (k QueryServer) AllianceDelegationRewards(context context.Context, req *types.QueryAllianceDelegationRewardsRequest) (*types.QueryAllianceDelegationRewardsResponse, error) {
ctx := sdk.UnwrapSDKContext(context)
delAddr, err := sdk.AccAddressFromBech32(request.DelegatorAddr)
decodedDenom, err := url.QueryUnescape(req.Denom)
if err == nil {
req.Denom = decodedDenom
}
delAddr, err := sdk.AccAddressFromBech32(req.DelegatorAddr)
if err != nil {
return nil, err
}
valAddr, err := sdk.ValAddressFromBech32(request.ValidatorAddr)
valAddr, err := sdk.ValAddressFromBech32(req.ValidatorAddr)
if err != nil {
return nil, err
}
_, found := k.GetAssetByDenom(ctx, request.Denom)
_, found := k.GetAssetByDenom(ctx, req.Denom)
if !found {
return nil, types.ErrUnknownAsset
}
Expand All @@ -241,12 +251,12 @@ func (k QueryServer) AllianceDelegationRewards(context context.Context, request
return nil, err
}

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

rewards, err := k.ClaimDelegationRewards(ctx, delAddr, val, request.Denom)
rewards, err := k.ClaimDelegationRewards(ctx, delAddr, val, req.Denom)
if err != nil {
return nil, err
}
Expand All @@ -255,7 +265,7 @@ func (k QueryServer) AllianceDelegationRewards(context context.Context, request
}, nil
}

func (k QueryServer) IBCAllianceDelegationRewards(context context.Context, request *types.QueryIBCAllianceDelegationRewardsRequest) (*types.QueryAllianceDelegationRewardsResponse, error) {
func (k QueryServer) IBCAllianceDelegationRewards(context context.Context, request *types.QueryIBCAllianceDelegationRewardsRequest) (*types.QueryAllianceDelegationRewardsResponse, error) { //nolint:staticcheck // SA1019: types.QueryIBCAllianceDelegationRewardsRequest is deprecated
req := types.QueryAllianceDelegationRewardsRequest{
DelegatorAddr: request.DelegatorAddr,
ValidatorAddr: request.ValidatorAddr,
Expand Down Expand Up @@ -392,6 +402,10 @@ func (k QueryServer) AlliancesDelegationByValidator(c context.Context, req *type

func (k QueryServer) AllianceDelegation(c context.Context, req *types.QueryAllianceDelegationRequest) (*types.QueryAllianceDelegationResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
decodedDenom, err := url.QueryUnescape(req.Denom)
if err == nil {
req.Denom = decodedDenom
}

delAddr, err := sdk.AccAddressFromBech32(req.DelegatorAddr)
if err != nil {
Expand Down Expand Up @@ -433,7 +447,7 @@ func (k QueryServer) AllianceDelegation(c context.Context, req *types.QueryAllia
}, nil
}

func (k QueryServer) IBCAllianceDelegation(c context.Context, request *types.QueryIBCAllianceDelegationRequest) (*types.QueryAllianceDelegationResponse, error) {
func (k QueryServer) IBCAllianceDelegation(c context.Context, request *types.QueryIBCAllianceDelegationRequest) (*types.QueryAllianceDelegationResponse, error) { //nolint:staticcheck // SA1019: types.QueryIBCAllianceDelegationRequest is deprecated
req := types.QueryAllianceDelegationRequest{
DelegatorAddr: request.DelegatorAddr,
ValidatorAddr: request.ValidatorAddr,
Expand Down
19 changes: 18 additions & 1 deletion x/alliance/keeper/tests/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ func TestQueryAnUniqueIBCAlliance(t *testing.T) {
queryServer := keeper.NewQueryServerImpl(app.AllianceKeeper)

// WHEN: QUERYING THE ALLIANCES LIST
alliances, err := queryServer.IBCAlliance(ctx, &types.QueryIBCAllianceRequest{
alliances, err := queryServer.Alliance(ctx, &types.QueryAllianceRequest{
Denom: "ibc%2Falliance2",
})
alliancesIbcEndpoint, err2 := queryServer.IBCAlliance(ctx, &types.QueryIBCAllianceRequest{ //nolint:staticcheck // SA1019: types.types.QueryIBCAllianceRequest is deprecated
Hash: "alliance2",
})

Expand All @@ -175,6 +178,20 @@ func TestQueryAnUniqueIBCAlliance(t *testing.T) {
RewardChangeInterval: 0,
},
}, alliances)

require.Nil(t, err2)
require.Equal(t, &types.QueryAllianceResponse{
Alliance: &types.AllianceAsset{
Denom: "ibc/alliance2",
RewardWeight: sdk.NewDec(10),
RewardWeightRange: types.RewardWeightRange{Min: sdk.NewDec(2), Max: sdk.NewDec(12)},
TakeRate: sdk.MustNewDecFromStr("0.14159265359"),
TotalTokens: sdk.ZeroInt(),
TotalValidatorShares: sdk.NewDec(0),
RewardChangeRate: sdk.NewDec(0),
RewardChangeInterval: 0,
},
}, alliancesIbcEndpoint)
}

func TestQueryAllianceNotFound(t *testing.T) {
Expand Down
Loading

0 comments on commit 3742bab

Please sign in to comment.