Skip to content

Commit

Permalink
adds query for withdrawal addr
Browse files Browse the repository at this point in the history
  • Loading branch information
Lazar955 committed Dec 2, 2024
1 parent 58e3c5f commit b55f6a0
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 43 deletions.
26 changes: 26 additions & 0 deletions proto/babylon/incentive/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "google/api/annotations.proto";
import "babylon/incentive/params.proto";
import "babylon/incentive/incentive.proto";
import "cosmos/base/v1beta1/coin.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/babylonlabs-io/babylon/x/incentive/types";

Expand All @@ -23,6 +24,11 @@ service Query {
rpc BTCStakingGauge(QueryBTCStakingGaugeRequest) returns (QueryBTCStakingGaugeResponse) {
option (google.api.http).get = "/babylon/incentive/btc_staking_gauge/{height}";
}

// DelegatorWithdrawAddress queries withdraw address of a delegator.
rpc DelegatorWithdrawAddress(QueryDelegatorWithdrawAddressRequest) returns (QueryDelegatorWithdrawAddressResponse) {
option (google.api.http).get = "/babylon/incentive/delegators/{delegator_address}/withdraw_address";
}
}

// QueryParamsRequest is request type for the Query/Params RPC method.
Expand Down Expand Up @@ -84,3 +90,23 @@ message QueryBTCStakingGaugeResponse {
// gauge is the BTC staking gauge at the queried height
BTCStakingGaugeResponse gauge = 1;
}

// QueryDelegatorWithdrawAddressRequest is the request type for the
// Query/DelegatorWithdrawAddress RPC method.
message QueryDelegatorWithdrawAddressRequest {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// delegator_address defines the delegator address to query for.
string delegator_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}

// QueryDelegatorWithdrawAddressResponse is the response type for the
// Query/DelegatorWithdrawAddress RPC method.
message QueryDelegatorWithdrawAddressResponse {
option (gogoproto.equal) = false;
option (gogoproto.goproto_getters) = false;

// withdraw_address defines the delegator address to query for.
string withdraw_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"];
}
2 changes: 1 addition & 1 deletion x/incentive/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func NewSetWithdrawAddressCmd() *cobra.Command {

msg := &types.MsgSetWithdrawAddress{
DelegatorAddress: delAddr.String(),
WithdrawAddress: string(withdrawAddr),
WithdrawAddress: withdrawAddr.String(),
}

return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
Expand Down
32 changes: 32 additions & 0 deletions x/incentive/keeper/query_delegator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package keeper

import (
"context"
"github.com/babylonlabs-io/babylon/x/incentive/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

func (k Keeper) DelegatorWithdrawAddress(goCtx context.Context, req *types.QueryDelegatorWithdrawAddressRequest) (*types.QueryDelegatorWithdrawAddressResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

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

ctx := sdk.UnwrapSDKContext(goCtx)
delegatorAddress, err := sdk.AccAddressFromBech32(req.DelegatorAddress)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}

withdrawAddr, err := k.GetWithdrawAddr(ctx, delegatorAddress)
if err != nil {
return nil, err
}

return &types.QueryDelegatorWithdrawAddressResponse{WithdrawAddress: withdrawAddr.String()}, nil
}
21 changes: 21 additions & 0 deletions x/incentive/keeper/query_delegator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keeper_test

import (
"github.com/babylonlabs-io/babylon/testutil/datagen"
testkeeper "github.com/babylonlabs-io/babylon/testutil/keeper"
"github.com/babylonlabs-io/babylon/x/incentive/types"
"github.com/stretchr/testify/require"
"testing"
)

func TestDelegatorAddressQuery(t *testing.T) {
keeper, ctx := testkeeper.IncentiveKeeper(t, nil, nil, nil)
withdrawalAddr := datagen.GenRandomAccount().GetAddress()
delegatorAddr := datagen.GenRandomAccount().GetAddress()
err := keeper.SetWithdrawAddr(ctx, delegatorAddr, withdrawalAddr)
require.NoError(t, err)

response, err := keeper.DelegatorWithdrawAddress(ctx, &types.QueryDelegatorWithdrawAddressRequest{DelegatorAddress: delegatorAddr.String()})
require.NoError(t, err)
require.Equal(t, &types.QueryDelegatorWithdrawAddressResponse{WithdrawAddress: withdrawalAddr.String()}, response)
}
Loading

0 comments on commit b55f6a0

Please sign in to comment.