forked from hyperledger-archives/burrow
/
decoder.go
52 lines (44 loc) · 1.65 KB
/
decoder.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
package service
import (
"math/big"
"strconv"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/evm/abi"
"github.com/hyperledger/burrow/execution/exec"
"github.com/hyperledger/burrow/vent/chain"
"github.com/hyperledger/burrow/vent/types"
"github.com/pkg/errors"
)
// decodeEvent unpacks & decodes event data
func decodeEvent(log chain.Event, txOrigin *chain.Origin, evAbi *abi.EventSpec) (map[string]interface{}, error) {
// to prepare decoded data and map to event item name
data := make(map[string]interface{})
// decode header to get context data for each event
data[types.EventNameLabel] = evAbi.Name
data[types.ChainIDLabel] = txOrigin.ChainID
data[types.BlockHeightLabel] = strconv.FormatUint(txOrigin.Height, 10)
data[types.TxIndexLabel] = strconv.FormatUint(txOrigin.Index, 10)
data[types.EventIndexLabel] = strconv.FormatUint(log.GetIndex(), 10)
data[types.EventTypeLabel] = exec.TypeLog.String()
data[types.TxTxHashLabel] = log.GetTransactionHash().String()
// build expected interface type array to get log event values
unpackedData := abi.GetPackingTypes(evAbi.Inputs)
// unpack event data (topics & data part)
if err := abi.UnpackEvent(evAbi, log.GetTopics(), log.GetData(), unpackedData...); err != nil {
return nil, errors.Wrap(err, "Could not unpack event data")
}
// for each decoded item value, stores it in given item name
for i, input := range evAbi.Inputs {
switch v := unpackedData[i].(type) {
case *crypto.Address:
data[input.Name] = v.String()
case *big.Int:
data[input.Name] = v.String()
case *string:
data[input.Name] = *v
default:
data[input.Name] = v
}
}
return data, nil
}