-
Notifications
You must be signed in to change notification settings - Fork 6
/
status.go
69 lines (54 loc) · 1.76 KB
/
status.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
package handlers
import (
"net/http"
"github.com/ethereum/go-ethereum/common"
"github.com/gladiusio/gladius-common/pkg/blockchain"
"github.com/gladiusio/gladius-common/pkg/routing/responses"
"github.com/gorilla/mux"
)
// StatusHandler Main Status API route handler
func StatusHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Status Page\n"))
}
// StatusTxHandler - Checks Status of txHash
func StatusTxHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
txHash := vars["tx"]
transaction, isPending, err := blockchain.Tx(common.HexToHash(txHash))
complete := !isPending
if transaction == nil || err != nil {
ErrorHandler(w, r, "Transaction not found", err, http.StatusNotFound)
return
}
transactionJSON, err := transaction.MarshalJSON()
if err != nil {
ErrorHandler(w, r, "Transaction could not be formatted", err, http.StatusUnprocessableEntity)
return
}
var receiptResponseJSON []byte
var status bool
if complete {
receipt, err := blockchain.TxReceipt(common.HexToHash(txHash))
if receipt == nil || err != nil {
ErrorHandler(w, r, "Receipt not found", err, http.StatusNotFound)
return
}
statusResponse := !(receipt.Status == 0)
if statusResponse {
status = true
} else {
status = false
}
receiptJSON, err := receipt.MarshalJSON()
if err != nil {
ErrorHandler(w, r, "Receipt could not be formatted", err, http.StatusUnprocessableEntity)
return
}
receiptResponseJSON = receiptJSON
} else {
receiptResponseJSON = []byte("null")
}
w.WriteHeader(http.StatusOK)
txHashResponse := responses.TxHash{Value: txHash, Status: status, Complete: complete, Transaction: transactionJSON, Receipt: receiptResponseJSON}
ResponseHandler(w, r, "null", true, nil, txHashResponse, transaction)
}