-
Notifications
You must be signed in to change notification settings - Fork 0
/
getfees.go
68 lines (59 loc) · 1.75 KB
/
getfees.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
package rest
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/tendermint/go-amino"
"github.com/aximchain/axc-cosmos-sdk/client/context"
"github.com/aximchain/axc-cosmos-sdk/x/paramHub"
"github.com/aximchain/axc-cosmos-sdk/x/paramHub/types"
)
func GetFeesParamHandler(cdc *amino.Codec, ctx context.CLIContext) http.HandlerFunc {
responseType := "application/json"
throw := func(w http.ResponseWriter, status int, err error) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(status)
w.Write([]byte(err.Error()))
return
}
return func(w http.ResponseWriter, r *http.Request) {
bz, err := ctx.Query(fmt.Sprintf("%s/fees", paramHub.AbciQueryPrefix), nil)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
var fees []types.FeeParam
err = cdc.UnmarshalBinaryLengthPrefixed(bz, &fees)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
formats, exist := r.URL.Query()["format"]
format := types.JSONFORMAT
if exist {
if len(formats) < 1 {
throw(w, http.StatusBadRequest, errors.New(fmt.Sprintf("Format parameter is invalid")))
return
}
format = formats[0]
if format != types.JSONFORMAT && format != types.AMINOFORMAT {
throw(w, http.StatusBadRequest, errors.New(fmt.Sprintf("Format %s is not supported, options [%s, %s]", format, types.JSONFORMAT, types.AMINOFORMAT)))
return
}
}
var output []byte
if format == types.JSONFORMAT {
output, err = json.Marshal(fees)
} else if format == types.AMINOFORMAT {
output, err = cdc.MarshalJSON(fees)
}
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
w.Header().Set("Content-Type", responseType)
w.WriteHeader(http.StatusOK)
w.Write(output)
}
}