-
Notifications
You must be signed in to change notification settings - Fork 2
/
tx_info.go
90 lines (78 loc) · 2.39 KB
/
tx_info.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
84
85
86
87
88
89
90
package ethutils
import (
"encoding/json"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
)
type InternalTx struct {
From string `json:"from"`
To string `json:"to"`
Value string `json:"value"`
}
type TxInfo struct {
Status string
Tx *Transaction
InternalTxs []InternalTx
Receipt *types.Receipt
BlockHeader *types.Header
}
func (self *TxInfo) GasCost() *big.Int {
return big.NewInt(0).Mul(
big.NewInt(int64(self.Receipt.GasUsed)),
self.Tx.GasPrice(),
)
}
type Transaction struct {
*types.Transaction
Extra TxExtraInfo `json:"extra"`
}
type TxExtraInfo struct {
BlockNumber *string `json:"blockNumber,omitempty"`
BlockHash *common.Hash `json:"blockHash,omitempty"`
From *common.Address `json:"from,omitempty"`
}
func (tx *Transaction) UnmarshalJSON(msg []byte) error {
if err := json.Unmarshal(msg, &tx.Transaction); err != nil {
return err
}
return json.Unmarshal(msg, &tx.Extra)
}
type txmarshaling struct {
AccountNonce hexutil.Uint64 `json:"nonce"`
Price *hexutil.Big `json:"gasPrice"`
GasLimit hexutil.Uint64 `json:"gas"`
Recipient *common.Address `json:"to"`
Amount *hexutil.Big `json:"value"`
Payload hexutil.Bytes `json:"input"`
BlockNumber *string `json:"blockNumber,omitempty"`
BlockHash *common.Hash `json:"blockHash,omitempty"`
From *common.Address `json:"from,omitempty"`
// Signature values
V *hexutil.Big `json:"v"`
R *hexutil.Big `json:"r"`
S *hexutil.Big `json:"s"`
// This is only used when marshaling to JSON.
Hash *common.Hash `json:"hash"`
}
func (tx *Transaction) MarshalJSON() ([]byte, error) {
v, r, s := tx.Transaction.RawSignatureValues()
h := tx.Transaction.Hash()
txmar := txmarshaling{
AccountNonce: hexutil.Uint64(tx.Transaction.Nonce()),
Price: (*hexutil.Big)(tx.Transaction.GasPrice()),
GasLimit: hexutil.Uint64(tx.Transaction.Gas()),
Recipient: tx.Transaction.To(),
Amount: (*hexutil.Big)(tx.Transaction.Value()),
Payload: hexutil.Bytes(tx.Transaction.Data()),
BlockNumber: tx.Extra.BlockNumber,
BlockHash: tx.Extra.BlockHash,
From: tx.Extra.From,
V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
Hash: &h,
}
return json.Marshal(txmar)
}