-
Notifications
You must be signed in to change notification settings - Fork 4
/
transactions.go
83 lines (73 loc) · 2.25 KB
/
transactions.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package api
import (
"encoding/json"
"fmt"
"github.com/MinterTeam/minter-go-node/core/transaction"
"github.com/MinterTeam/minter-go-node/core/types"
"github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/rpc/core/types"
"math/big"
)
type TransactionResponse struct {
Hash common.HexBytes `json:"hash"`
RawTx string `json:"raw_tx"`
Height int64 `json:"height"`
Index uint32 `json:"index"`
From string `json:"from"`
Nonce uint64 `json:"nonce"`
GasPrice *big.Int `json:"gas_price"`
GasCoin types.CoinSymbol `json:"gas_coin"`
GasUsed int64 `json:"gas_used"`
Type byte `json:"type"`
Data json.RawMessage `json:"data"`
Payload []byte `json:"payload"`
Tags map[string]string `json:"tags"`
Code uint32 `json:"code,omitempty"`
Log string `json:"log,omitempty"`
}
type ResultTxSearch struct {
Txs []*core_types.ResultTx `json:"txs"`
TotalCount int `json:"total_count"`
}
func Transactions(query string) (*[]TransactionResponse, error) {
rpcResult, err := client.TxSearch(query, false, 1, 100)
if err != nil {
return nil, err
}
result := make([]TransactionResponse, len(rpcResult.Txs))
for i, tx := range rpcResult.Txs {
decodedTx, _ := transaction.DecodeFromBytes(tx.Tx)
sender, _ := decodedTx.Sender()
tags := make(map[string]string)
for _, tag := range tx.TxResult.Tags {
switch string(tag.Key) {
case "tx.type":
tags[string(tag.Key)] = fmt.Sprintf("%X", tag.Value)
default:
tags[string(tag.Key)] = string(tag.Value)
}
}
data, err := encodeTxData(decodedTx)
if err != nil {
return nil, err
}
result[i] = TransactionResponse{
Hash: common.HexBytes(tx.Tx.Hash()),
RawTx: fmt.Sprintf("%x", []byte(tx.Tx)),
Height: tx.Height,
Index: tx.Index,
From: sender.String(),
Nonce: decodedTx.Nonce,
GasPrice: decodedTx.GasPrice,
GasCoin: decodedTx.GasCoin,
GasUsed: tx.TxResult.GasUsed,
Type: decodedTx.Type,
Data: data,
Payload: decodedTx.Payload,
Tags: tags,
Code: tx.TxResult.Code,
Log: tx.TxResult.Log,
}
}
return &result, nil
}