/
tx.go
63 lines (52 loc) · 1.57 KB
/
tx.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
package importdb
import (
"encoding/hex"
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/likecoin/likecoin-chain/v4/app"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/bytes"
"github.com/tendermint/tendermint/types"
codecTypes "github.com/cosmos/cosmos-sdk/codec/types"
)
var encodingConfig = app.MakeEncodingConfig()
func newResponseResultTx(txHash bytes.HexBytes, res *abci.TxResult, tx *codecTypes.Any, timestamp string) (sdk.TxResponse, error) {
parsedLogs, _ := sdk.ParseABCILogs(res.Result.Log)
return sdk.TxResponse{
TxHash: txHash.String(),
Height: res.Height,
Code: res.Result.Code,
Data: strings.ToUpper(hex.EncodeToString(res.Result.Data)),
RawLog: res.Result.Log,
Logs: parsedLogs,
Info: res.Result.Info,
GasWanted: res.Result.GasWanted,
GasUsed: res.Result.GasUsed,
Tx: tx,
Timestamp: timestamp,
}, nil
}
type intoAny interface {
AsAny() *codecTypes.Any
}
func parseTx(txBytes []byte) (*codecTypes.Any, error) {
txb, err := encodingConfig.TxConfig.TxDecoder()(txBytes)
if err != nil {
return nil, err
}
p, ok := txb.(intoAny)
if !ok {
return nil, fmt.Errorf("expecting a type implementing intoAny, got: %T", txb)
}
any := p.AsAny()
return any, nil
}
func formatTxResult(txHash bytes.HexBytes, resTx *abci.TxResult, block *types.Block) (sdk.TxResponse, error) {
tx, err := parseTx(resTx.Tx)
if err != nil {
return sdk.TxResponse{}, err
}
return newResponseResultTx(txHash, resTx, tx, block.Header.Time.Format(time.RFC3339))
}