-
Notifications
You must be signed in to change notification settings - Fork 4
/
transaction.go
66 lines (56 loc) · 1.45 KB
/
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
65
66
package api
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/gorilla/mux"
"github.com/tendermint/tendermint/libs/common"
"net/http"
"strings"
)
func Transaction(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
hash := strings.TrimLeft(vars["hash"], "Mt")
decoded, err := hex.DecodeString(hash)
tx, err := client.Tx(decoded, false)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(Response{
Code: 0,
Result: err.Error(),
})
return
}
w.WriteHeader(http.StatusOK)
decodedTx, _ := transaction.DecodeFromBytes(tx.Tx)
sender, _ := decodedTx.Sender()
err = json.NewEncoder(w).Encode(Response{
Code: 0,
Result: TransactionResponse{
Hash: common.HexBytes(tx.Tx.Hash()),
RawTx: fmt.Sprintf("%x", tx.Tx),
Height: tx.Height,
Index: tx.Index,
TxResult: ResponseDeliverTx{
Code: tx.TxResult.Code,
Data: tx.TxResult.Data,
Log: tx.TxResult.Log,
Info: tx.TxResult.Info,
GasWanted: tx.TxResult.GasWanted,
GasUsed: tx.TxResult.GasUsed,
Tags: tx.TxResult.Tags,
},
From: sender.String(),
Nonce: decodedTx.Nonce,
GasPrice: decodedTx.GasPrice,
Type: decodedTx.Type,
Data: decodedTx.GetDecodedData(),
Payload: decodedTx.Payload,
},
})
if err != nil {
panic(err)
}
}