-
Notifications
You must be signed in to change notification settings - Fork 41
/
getswap.go
62 lines (52 loc) · 1.5 KB
/
getswap.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 (
"encoding/hex"
"fmt"
"net/http"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/gorilla/mux"
"github.com/bnb-chain/node/plugins/tokens/swap"
"github.com/bnb-chain/node/wire"
)
// QuerySwapReqHandler creates an http request handler to query an AtomicSwap record by swapID
func QuerySwapReqHandler(
cdc *wire.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 func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
swapID, err := hex.DecodeString(vars["swapID"])
if err != nil {
throw(w, http.StatusBadRequest, err)
return
}
if len(swapID) != swap.SwapIDLength {
throw(w, http.StatusBadRequest, fmt.Errorf("length of swapID should be %d", swap.SwapIDLength))
return
}
params := swap.QuerySwapByID{
SwapID: swapID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
output, err := ctx.QueryWithData(fmt.Sprintf("custom/%s/%s", swap.AtomicSwapRoute, swap.QuerySwapID), bz)
if err != nil {
throw(w, http.StatusInternalServerError, err)
return
}
if output == nil {
throw(w, http.StatusNotFound, fmt.Errorf("no match swapID"))
return
}
w.Header().Set("Content-Type", responseType)
w.WriteHeader(http.StatusOK)
_, _ = w.Write(output)
}
}