-
Notifications
You must be signed in to change notification settings - Fork 4
/
send_transaction.go
64 lines (49 loc) · 1.3 KB
/
send_transaction.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
package api
import (
"encoding/json"
"io"
"io/ioutil"
"github.com/MinterTeam/minter-go-node/core/code"
"github.com/MinterTeam/minter-go-node/core/types"
"net/http"
"strings"
)
type SendTransactionRequest struct {
Transaction string `json:"transaction"`
}
type SendTransactionResponse struct {
Hash string `json:"hash"`
}
func SendTransaction(w http.ResponseWriter, r *http.Request) {
var req SendTransactionRequest
body, _ := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
json.Unmarshal(body, &req)
result, err := client.BroadcastTxCommit(types.Hex2Bytes(req.Transaction))
if err != nil {
panic(err)
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if result.CheckTx.Code != code.OK {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(Response{
Code: result.CheckTx.Code,
Log: "Check tx error: " + result.CheckTx.Log,
})
return
}
if result.DeliverTx.Code != code.OK {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(Response{
Code: result.CheckTx.Code,
Log: "Deliver tx error: " + result.DeliverTx.Log,
})
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(Response{
Code: code.OK,
Result: SendTransactionResponse{
Hash: "Mt" + strings.ToLower(result.Hash.String()),
},
})
}