Skip to content

Commit 8bce8d0

Browse files
feat(rpc): Allowing for ValAddr on /transfer and /balance/$ADDRESS (#1102)
* feat(rpc): Allowing for ValAddr on /transfer and /balance/ADDRESS * refactor(rpc): error variables in rpc/state.go * chore(lint): fixing ineffassign and making const from addrKey
1 parent 7221e8c commit 8bce8d0

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

Diff for: service/rpc/state.go

+32-14
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ const (
2626
queryRedelegationsEndpoint = "/query_redelegations"
2727
)
2828

29-
var addrKey = "address"
29+
const addrKey = "address"
30+
31+
var (
32+
ErrInvalidAddressFormat = errors.New("address must be a valid account or validator address")
33+
ErrMissingAddress = errors.New("address not specified")
34+
ErrInvalidAmount = errors.New("amount must be greater than zero")
35+
)
3036

3137
// submitTxRequest represents a request to submit a raw transaction
3238
type submitTxRequest struct {
@@ -78,6 +84,7 @@ type cancelUnbondRequest struct {
7884
GasLimit uint64 `json:"gas_limit"`
7985
}
8086

87+
// queryRedelegationsRequest represents a request to query redelegations
8188
type queryRedelegationsRequest struct {
8289
From string `json:"from"`
8390
To string `json:"to"`
@@ -93,10 +100,16 @@ func (h *Handler) handleBalanceRequest(w http.ResponseWriter, r *http.Request) {
93100
addrStr, exists := vars[addrKey]
94101
if exists {
95102
// convert address to Address type
96-
addr, addrerr := types.AccAddressFromBech32(addrStr)
97-
if addrerr != nil {
98-
writeError(w, http.StatusBadRequest, balanceEndpoint, addrerr)
99-
return
103+
var addr state.AccAddress
104+
addr, err = types.AccAddressFromBech32(addrStr)
105+
if err != nil {
106+
// first check if it is a validator address and can be converted
107+
valAddr, err := types.ValAddressFromBech32(addrStr)
108+
if err != nil {
109+
writeError(w, http.StatusBadRequest, balanceEndpoint, ErrInvalidAddressFormat)
110+
return
111+
}
112+
addr = valAddr.Bytes()
100113
}
101114
bal, err = h.state.BalanceForAddress(r.Context(), addr)
102115
} else {
@@ -190,13 +203,18 @@ func (h *Handler) handleTransfer(w http.ResponseWriter, r *http.Request) {
190203
return
191204
}
192205
if req.Amount <= 0 {
193-
writeError(w, http.StatusBadRequest, transferEndpoint, errors.New("amount must be greater than 0"))
206+
writeError(w, http.StatusBadRequest, transferEndpoint, ErrInvalidAmount)
194207
return
195208
}
196209
addr, err := types.AccAddressFromBech32(req.To)
197210
if err != nil {
198-
writeError(w, http.StatusBadRequest, transferEndpoint, err)
199-
return
211+
// first check if it is a validator address and can be converted
212+
valAddr, err := types.ValAddressFromBech32(req.To)
213+
if err != nil {
214+
writeError(w, http.StatusBadRequest, transferEndpoint, ErrInvalidAddressFormat)
215+
return
216+
}
217+
addr = valAddr.Bytes()
200218
}
201219
amount := types.NewInt(req.Amount)
202220

@@ -224,7 +242,7 @@ func (h *Handler) handleDelegation(w http.ResponseWriter, r *http.Request) {
224242
return
225243
}
226244
if req.Amount <= 0 {
227-
writeError(w, http.StatusBadRequest, delegationEndpoint, errors.New("amount must be greater than 0"))
245+
writeError(w, http.StatusBadRequest, delegationEndpoint, ErrInvalidAmount)
228246
return
229247
}
230248
addr, err := types.ValAddressFromBech32(req.To)
@@ -258,7 +276,7 @@ func (h *Handler) handleUndelegation(w http.ResponseWriter, r *http.Request) {
258276
return
259277
}
260278
if req.Amount <= 0 {
261-
writeError(w, http.StatusBadRequest, undelegationEndpoint, errors.New("amount must be greater than 0"))
279+
writeError(w, http.StatusBadRequest, undelegationEndpoint, ErrInvalidAmount)
262280
return
263281
}
264282
addr, err := types.ValAddressFromBech32(req.From)
@@ -292,7 +310,7 @@ func (h *Handler) handleCancelUnbonding(w http.ResponseWriter, r *http.Request)
292310
return
293311
}
294312
if req.Amount <= 0 {
295-
writeError(w, http.StatusBadRequest, cancelUnbondingEndpoint, errors.New("amount must be greater than 0"))
313+
writeError(w, http.StatusBadRequest, cancelUnbondingEndpoint, ErrInvalidAmount)
296314
return
297315
}
298316
addr, err := types.ValAddressFromBech32(req.From)
@@ -326,7 +344,7 @@ func (h *Handler) handleRedelegation(w http.ResponseWriter, r *http.Request) {
326344
return
327345
}
328346
if req.Amount <= 0 {
329-
writeError(w, http.StatusBadRequest, beginRedelegationEndpoint, errors.New("amount must be greater than 0"))
347+
writeError(w, http.StatusBadRequest, beginRedelegationEndpoint, ErrInvalidAmount)
330348
return
331349
}
332350
srcAddr, err := types.ValAddressFromBech32(req.From)
@@ -362,7 +380,7 @@ func (h *Handler) handleQueryDelegation(w http.ResponseWriter, r *http.Request)
362380
vars := mux.Vars(r)
363381
addrStr, exists := vars[addrKey]
364382
if !exists {
365-
writeError(w, http.StatusBadRequest, queryDelegationEndpoint, errors.New("address not specified"))
383+
writeError(w, http.StatusBadRequest, queryDelegationEndpoint, ErrMissingAddress)
366384
return
367385
}
368386

@@ -393,7 +411,7 @@ func (h *Handler) handleQueryUnbonding(w http.ResponseWriter, r *http.Request) {
393411
vars := mux.Vars(r)
394412
addrStr, exists := vars[addrKey]
395413
if !exists {
396-
writeError(w, http.StatusBadRequest, queryUnbondingEndpoint, errors.New("address not specified"))
414+
writeError(w, http.StatusBadRequest, queryUnbondingEndpoint, ErrMissingAddress)
397415
return
398416
}
399417

Diff for: service/state/core_access.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (ca *CoreAccessor) Balance(ctx context.Context) (*Balance, error) {
136136
return ca.BalanceForAddress(ctx, addr)
137137
}
138138

139-
func (ca *CoreAccessor) BalanceForAddress(ctx context.Context, addr AccAddress) (*Balance, error) {
139+
func (ca *CoreAccessor) BalanceForAddress(ctx context.Context, addr Address) (*Balance, error) {
140140
head, err := ca.getter.Head(ctx)
141141
if err != nil {
142142
return nil, err

Diff for: service/state/interface.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ type Accessor interface {
2727
// NOTE: the balance returned is the balance reported by the block right before
2828
// the node's current head (head-1). This is due to the fact that for block N, the block's
2929
// `AppHash` is the result of applying the previous block's transaction list.
30-
BalanceForAddress(ctx context.Context, addr AccAddress) (*Balance, error)
30+
BalanceForAddress(ctx context.Context, addr Address) (*Balance, error)
3131

3232
// Transfer sends the given amount of coins from default wallet of the node to the given account address.
3333
Transfer(ctx context.Context, to AccAddress, amount math.Int, gasLimit uint64) (*TxResponse, error)

Diff for: service/state/service.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (s *Service) Balance(ctx context.Context) (*Balance, error) {
3737
return s.accessor.Balance(ctx)
3838
}
3939

40-
func (s *Service) BalanceForAddress(ctx context.Context, addr AccAddress) (*Balance, error) {
40+
func (s *Service) BalanceForAddress(ctx context.Context, addr Address) (*Balance, error) {
4141
return s.accessor.BalanceForAddress(ctx, addr)
4242
}
4343

Diff for: service/state/state.go

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type Tx = coretypes.Tx
1515
// TxResponse is an alias to the TxResponse type from Cosmos-SDK.
1616
type TxResponse = sdk.TxResponse
1717

18+
// Address is an alias to the Address type from Cosmos-SDK.
19+
type Address = sdk.Address
20+
1821
// ValAddress is an alias to the ValAddress type from Cosmos-SDK.
1922
type ValAddress = sdk.ValAddress
2023

0 commit comments

Comments
 (0)