-
Notifications
You must be signed in to change notification settings - Fork 244
/
transfer_view.go
94 lines (88 loc) · 2.96 KB
/
transfer_view.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
91
92
93
94
package wallet
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
)
func castToTransferViews(transfers []Transfer) []TransferView {
views := make([]TransferView, len(transfers))
for i := range transfers {
views[i] = castToTransferView(transfers[i])
}
return views
}
func castToTransferView(t Transfer) TransferView {
view := TransferView{}
view.ID = t.ID
view.Type = t.Type
view.Address = t.Address
view.BlockNumber = (*hexutil.Big)(t.BlockNumber)
view.BlockHash = t.BlockHash
view.Timestamp = hexutil.Uint64(t.Timestamp)
view.GasPrice = (*hexutil.Big)(t.Transaction.GasPrice())
view.GasLimit = hexutil.Uint64(t.Transaction.Gas())
view.GasUsed = hexutil.Uint64(t.Receipt.GasUsed)
view.Nonce = hexutil.Uint64(t.Transaction.Nonce())
view.TxStatus = hexutil.Uint64(t.Receipt.Status)
view.Input = hexutil.Bytes(t.Transaction.Data())
view.TxHash = t.Transaction.Hash()
switch t.Type {
case ethTransfer:
view.From = t.From
if t.Transaction.To() != nil {
view.To = *t.Transaction.To()
}
view.Value = (*hexutil.Big)(t.Transaction.Value())
view.Contract = t.Receipt.ContractAddress
case erc20Transfer:
view.Contract = t.Log.Address
from, to, amount := parseLog(t.Log)
view.From, view.To, view.Value = from, to, (*hexutil.Big)(amount)
}
return view
}
func parseLog(ethlog *types.Log) (from, to common.Address, amount *big.Int) {
if len(ethlog.Topics) < 3 {
log.Warn("not enough topics for erc20 transfer", "topics", ethlog.Topics)
return
}
if len(ethlog.Topics[1]) != 32 {
log.Warn("second topic is not padded to 32 byte address", "topic", ethlog.Topics[1])
return
}
if len(ethlog.Topics[2]) != 32 {
log.Warn("third topic is not padded to 32 byte address", "topic", ethlog.Topics[2])
return
}
copy(from[:], ethlog.Topics[1][12:])
copy(to[:], ethlog.Topics[2][12:])
if len(ethlog.Data) != 32 {
log.Warn("data is not padded to 32 byts big int", "data", ethlog.Data)
return
}
amount = new(big.Int).SetBytes(ethlog.Data)
return
}
// TransferView stores only fields used by a client and ensures that all relevant fields are
// encoded in hex.
type TransferView struct {
ID common.Hash `json:"id"`
Type TransferType `json:"type"`
Address common.Address `json:"address"`
BlockNumber *hexutil.Big `json:"blockNumber"`
BlockHash common.Hash `json:"blockhash"`
Timestamp hexutil.Uint64 `json:"timestamp"`
GasPrice *hexutil.Big `json:"gasPrice"`
GasLimit hexutil.Uint64 `json:"gasLimit"`
GasUsed hexutil.Uint64 `json:"gasUsed"`
Nonce hexutil.Uint64 `json:"nonce"`
TxStatus hexutil.Uint64 `json:"txStatus"`
Input hexutil.Bytes `json:"input"`
TxHash common.Hash `json:"txHash"`
Value *hexutil.Big `json:"value"`
From common.Address `json:"from"`
To common.Address `json:"to"`
Contract common.Address `json:"contract"`
}