-
Notifications
You must be signed in to change notification settings - Fork 4
/
query.go
62 lines (54 loc) · 1.74 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
package rest
import (
"fmt"
"net/http"
"github.com/MinterTeam/mhub2/module/x/oracle/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/rest"
)
func pricesHandler(cliCtx client.Context, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, height, err := cliCtx.Query(fmt.Sprintf("custom/%s/prices", storeName))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if len(res) == 0 {
rest.WriteErrorResponse(w, http.StatusNotFound, "prices not found")
return
}
var out types.Prices
cliCtx.Codec.MustUnmarshalJSON(res, &out)
rest.PostProcessResponse(w, cliCtx.WithHeight(height), res)
}
}
func coinsHandler(cliCtx client.Context, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, height, err := cliCtx.Query(fmt.Sprintf("custom/%s/coins", storeName))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if len(res) == 0 {
rest.WriteErrorResponse(w, http.StatusNotFound, "coins not found")
return
}
rest.PostProcessResponse(w, cliCtx.WithHeight(height), res)
}
}
func ethFeeHandler(cliCtx client.Context, storeName string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
res, height, err := cliCtx.Query(fmt.Sprintf("custom/%s/eth_fee", storeName))
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}
if len(res) == 0 {
rest.WriteErrorResponse(w, http.StatusNotFound, "prices not found")
return
}
var out types.QueryEthFeeResponse
cliCtx.Codec.MustUnmarshalJSON(res, &out)
rest.PostProcessResponse(w, cliCtx.WithHeight(height), res)
}
}