-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
40 lines (33 loc) · 965 Bytes
/
utils.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
package contract
import (
"errors"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)
func UnpackLog(log types.Log) (ContractEventPublished, error) {
contractAbi, abiErr := abi.JSON(strings.NewReader(string(ContractABI)))
if abiErr != nil {
return ContractEventPublished{}, abiErr
}
unpacked, unpackErr := contractAbi.Unpack("EventPublished", log.Data)
if unpackErr != nil {
return ContractEventPublished{}, unpackErr
}
if len(unpacked) < 4 {
return ContractEventPublished{}, errors.New("Oh no, the unpacked event must have four elements!")
}
topic := unpacked[0].(string)
id := unpacked[1].([]byte)
cid := unpacked[2].(string)
newAccounts := unpacked[3].([]common.Address)
event := ContractEventPublished{
TopicId: topic,
Id: id,
Cid: cid,
NewAccounts: newAccounts,
Raw: log,
}
return event, nil
}