-
Notifications
You must be signed in to change notification settings - Fork 176
/
query.go
91 lines (72 loc) · 2.23 KB
/
query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//nolint
package rest
import (
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
bankTypes "github.com/maticnetwork/heimdall/bank/types"
"github.com/maticnetwork/heimdall/types"
)
//It represents the bank balance of particluar account
//swagger:response bankBalanceByAddressResponse
type bankBalanceByAddressResponse struct {
//in:body
Output bankBalanceByAddress `json:"output"`
}
type bankBalanceByAddress struct {
Height string `json:"height"`
Result []bankBalance `json:"result"`
}
type bankBalance struct {
//Denomination of the token
Denom string `json:"denom"`
//Amount of token in the bank
Amount string `json:"amount"`
}
//swagger:parameters bankBalanceByAddress
type Address struct {
//Address of the account
//required:true
//in:path
Address string `json:"address"`
//Address of the account
//in:query
Height string `json:"height"`
}
// swagger:route GET /bank/balances/{address} bank bankBalanceByAddress
// It returns the matic balance of particular address
// responses:
// 200: bankBalanceByAddressResponse
// QueryBalancesRequestHandlerFn query accountREST Handler
func QueryBalancesRequestHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vars := mux.Vars(r)
addr := types.HexToHeimdallAddress(vars["address"])
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}
params := bankTypes.NewQueryBalanceParams(addr)
bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
res, height, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/%s", bankTypes.QuerierRoute, bankTypes.QueryBalance), bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}
cliCtx = cliCtx.WithHeight(height)
// the query will return empty if there is no data for this account
if len(res) == 0 {
rest.PostProcessResponse(w, cliCtx, sdk.Coins{})
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}