github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/vent/service/decoder.go (about)

     1  package service
     2  
     3  import (
     4  	"math/big"
     5  	"strconv"
     6  
     7  	"github.com/hyperledger/burrow/crypto"
     8  	"github.com/hyperledger/burrow/execution/evm/abi"
     9  	"github.com/hyperledger/burrow/execution/exec"
    10  	"github.com/hyperledger/burrow/vent/chain"
    11  	"github.com/hyperledger/burrow/vent/types"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // decodeEvent unpacks & decodes event data
    16  func decodeEvent(log chain.Event, txOrigin *chain.Origin, evAbi *abi.EventSpec) (map[string]interface{}, error) {
    17  	// to prepare decoded data and map to event item name
    18  	data := make(map[string]interface{})
    19  
    20  	// decode header to get context data for each event
    21  	data[types.EventNameLabel] = evAbi.Name
    22  	data[types.ChainIDLabel] = txOrigin.ChainID
    23  	data[types.BlockHeightLabel] = strconv.FormatUint(txOrigin.Height, 10)
    24  	data[types.TxIndexLabel] = strconv.FormatUint(txOrigin.Index, 10)
    25  	data[types.EventIndexLabel] = strconv.FormatUint(log.GetIndex(), 10)
    26  	data[types.EventTypeLabel] = exec.TypeLog.String()
    27  	data[types.TxTxHashLabel] = log.GetTransactionHash().String()
    28  
    29  	// build expected interface type array to get log event values
    30  	unpackedData := abi.GetPackingTypes(evAbi.Inputs)
    31  
    32  	// unpack event data (topics & data part)
    33  	if err := abi.UnpackEvent(evAbi, log.GetTopics(), log.GetData(), unpackedData...); err != nil {
    34  		return nil, errors.Wrap(err, "Could not unpack event data")
    35  	}
    36  
    37  	// for each decoded item value, stores it in given item name
    38  	for i, input := range evAbi.Inputs {
    39  		switch v := unpackedData[i].(type) {
    40  		case *crypto.Address:
    41  			data[input.Name] = v.String()
    42  		case *big.Int:
    43  			data[input.Name] = v.String()
    44  		case *string:
    45  			data[input.Name] = *v
    46  		default:
    47  			data[input.Name] = v
    48  		}
    49  	}
    50  
    51  	return data, nil
    52  }