|
| 1 | +package bytecode |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "math/big" |
| 7 | + "strings" |
| 8 | + |
| 9 | + abi "github.com/ethereum/go-ethereum/accounts/abi" |
| 10 | + "github.com/ethereum/go-ethereum/common" |
| 11 | + "github.com/ethereum/go-ethereum/core/types" |
| 12 | + "github.com/unpackdev/solgo/utils" |
| 13 | +) |
| 14 | + |
| 15 | +// Topic represents a single decoded topic from an Ethereum event log. Topics are attributes |
| 16 | +// of an event, such as the method signature and indexed parameters. |
| 17 | +type Topic struct { |
| 18 | + Name string `json:"name"` // The name of the topic. |
| 19 | + Value any `json:"value"` // The value of the topic, decoded into the appropriate Go data type. |
| 20 | +} |
| 21 | + |
| 22 | +// Log encapsulates a decoded Ethereum event log. It includes the event's details such as its name, |
| 23 | +// signature, the contract that emitted the event, and the decoded data and topics. |
| 24 | +type Log struct { |
| 25 | + Event *abi.Event `json:"-"` // ABI definition of the log's event. |
| 26 | + Address common.Address `json:"address"` // Address of the contract that emitted the event. |
| 27 | + Abi string `json:"abi"` // ABI string of the event. |
| 28 | + SignatureHex common.Hash `json:"signature_hex"` // Hex-encoded signature of the event. |
| 29 | + Signature string `json:"signature"` // Signature of the event. |
| 30 | + Type utils.LogEventType `json:"type"` // Type of the event, classified by solgo. |
| 31 | + Name string `json:"name"` // Name of the event. |
| 32 | + Data map[string]any `json:"data"` // Decoded event data. |
| 33 | + Topics []Topic `json:"topics"` // Decoded topics of the event. |
| 34 | +} |
| 35 | + |
| 36 | +// DecodeLogFromAbi decodes an Ethereum event log using the provided ABI data. It returns a Log |
| 37 | +// instance containing the decoded event name, data, and topics. The function requires the event log |
| 38 | +// and its ABI as inputs. It handles errors such as missing topics or failure to parse the ABI. |
| 39 | +func DecodeLogFromAbi(log *types.Log, abiData []byte) (*Log, error) { |
| 40 | + if log == nil || len(log.Topics) < 1 { |
| 41 | + return nil, fmt.Errorf("log is nil or has no topics") |
| 42 | + } |
| 43 | + |
| 44 | + logABI, err := abi.JSON(bytes.NewReader(abiData)) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("failed to parse abi: %s", err) |
| 47 | + } |
| 48 | + |
| 49 | + event, err := logABI.EventByID(log.Topics[0]) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to get event by id %s: %s", log.Topics[0].Hex(), err) |
| 52 | + } |
| 53 | + |
| 54 | + data := make(map[string]any) |
| 55 | + if err := event.Inputs.UnpackIntoMap(data, log.Data); err != nil { |
| 56 | + return nil, fmt.Errorf("failed to unpack inputs into map: %s", err) |
| 57 | + } |
| 58 | + |
| 59 | + decodedTopics := make([]Topic, len(log.Topics)) |
| 60 | + for i, topic := range log.Topics { |
| 61 | + if i == 0 { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + decodedTopic, err := decodeTopic(topic, event.Inputs[i-1]) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("failed to decode topic: %s", err) |
| 68 | + } |
| 69 | + |
| 70 | + decodedTopics[i] = Topic{ |
| 71 | + Name: event.Inputs[i-1].Name, |
| 72 | + Value: decodedTopic, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + eventAbi, err := utils.EventToABI(event) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("failed to cast event into the abi: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + toReturn := &Log{ |
| 82 | + Event: event, |
| 83 | + Address: log.Address, |
| 84 | + Abi: eventAbi, |
| 85 | + SignatureHex: log.Topics[0], |
| 86 | + Signature: strings.TrimLeft(event.String(), "event "), |
| 87 | + Name: event.Name, |
| 88 | + Type: utils.LogEventType(strings.ToLower(event.Name)), |
| 89 | + Data: data, |
| 90 | + Topics: decodedTopics[1:], // Exclude the first topic (event signature) |
| 91 | + } |
| 92 | + |
| 93 | + return toReturn, nil |
| 94 | +} |
| 95 | + |
| 96 | +// decodeTopic decodes a single topic from an Ethereum event log based on its ABI argument type. |
| 97 | +// It supports various data types including addresses, booleans, integers, strings, bytes, and more. |
| 98 | +// This function is internal and used within DecodeLogFromAbi to process each topic individually. |
| 99 | +func decodeTopic(topic common.Hash, argument abi.Argument) (interface{}, error) { |
| 100 | + switch argument.Type.T { |
| 101 | + case abi.AddressTy: |
| 102 | + return common.BytesToAddress(topic.Bytes()), nil |
| 103 | + case abi.BoolTy: |
| 104 | + return topic[common.HashLength-1] == 1, nil |
| 105 | + case abi.IntTy, abi.UintTy: |
| 106 | + size := argument.Type.Size |
| 107 | + if size > 256 { |
| 108 | + return nil, fmt.Errorf("unsupported integer size: %d", size) |
| 109 | + } |
| 110 | + integer := new(big.Int).SetBytes(topic[:]) |
| 111 | + if argument.Type.T == abi.IntTy && size < 256 { |
| 112 | + integer = adjustIntSize(integer, size) |
| 113 | + } |
| 114 | + return integer, nil |
| 115 | + case abi.StringTy: |
| 116 | + return topic, nil |
| 117 | + case abi.BytesTy, abi.FixedBytesTy: |
| 118 | + return topic.Bytes(), nil |
| 119 | + case abi.SliceTy, abi.ArrayTy: |
| 120 | + return nil, fmt.Errorf("array/slice decoding not implemented") |
| 121 | + default: |
| 122 | + return nil, fmt.Errorf("decoding for type %v not implemented", argument.Type.T) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// adjustIntSize adjusts the size of an integer to match its ABI-specified size, which is relevant |
| 127 | +// for signed integers smaller than 256 bits. This function ensures the integer is correctly |
| 128 | +// interpreted according to its defined bit size in the ABI. |
| 129 | +func adjustIntSize(integer *big.Int, size int) *big.Int { |
| 130 | + if size == 256 || integer.Bit(size-1) == 0 { |
| 131 | + return integer |
| 132 | + } |
| 133 | + return new(big.Int).Sub(integer, new(big.Int).Lsh(big.NewInt(1), uint(size))) |
| 134 | +} |
| 135 | + |
| 136 | +// GetTopicByName searches for and returns a Topic by its name from a slice of Topic instances. |
| 137 | +// It facilitates accessing specific topics directly by name rather than iterating over the slice. |
| 138 | +// If the topic is not found, it returns nil. |
| 139 | +func GetTopicByName(name string, topics []Topic) *Topic { |
| 140 | + for _, topic := range topics { |
| 141 | + if topic.Name == name { |
| 142 | + return &topic |
| 143 | + } |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
0 commit comments