export event names in bindings. remove added APIs Filter/Watch-Logs which take event ids.

This commit is contained in:
Jared Wasinger 2024-12-16 14:08:46 +07:00 committed by Felix Lange
parent 89fcfac1cf
commit 3129593b3f
5 changed files with 23 additions and 43 deletions

View File

@ -462,25 +462,15 @@ func (c *BoundContract) transact(opts *TransactOpts, contract *common.Address, i
return signedTx, nil
}
// FilterLogsById filters contract logs for past blocks, returning the necessary
// channels to construct a strongly typed bound iterator on top of them.
func (c *BoundContract) FilterLogsById(opts *FilterOpts, eventID common.Hash, query ...[]interface{}) (<-chan types.Log, event.Subscription, error) {
return c.filterLogs(opts, eventID, query...)
}
// FilterLogs filters contract logs for past blocks, returning the necessary
// channels to construct a strongly typed bound iterator on top of them.
func (c *BoundContract) FilterLogs(opts *FilterOpts, name string, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
return c.filterLogs(opts, c.abi.Events[name].ID, query...)
}
func (c *BoundContract) filterLogs(opts *FilterOpts, eventID common.Hash, query ...[]interface{}) (chan types.Log, event.Subscription, error) {
// Don't crash on a lazy user
if opts == nil {
opts = new(FilterOpts)
}
// Append the event selector to the query parameters and construct the topic set
query = append([][]interface{}{{eventID}}, query...)
query = append([][]interface{}{{c.abi.Events[name].ID}}, query...)
topics, err := abi.MakeTopics(query...)
if err != nil {
return nil, nil, err

View File

@ -120,9 +120,7 @@ var (
Raw *types.Log // Blockchain specific contextual infos
}
func {{$contract.Type}}{{.Normalized.Name}}EventID() common.Hash {
return common.HexToHash("{{.Original.ID}}")
}
const {{$contract.Type}}{{.Normalized.Name}}EventName = "{{.Original.Name}}"
func (_{{$contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Event(log *types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) {
event := "{{.Original.Name}}"

View File

@ -129,13 +129,9 @@ type CBasic1 struct {
Raw *types.Log // Blockchain specific contextual infos
}
func CBasic1EventID() common.Hash {
return common.HexToHash("0x8f17dc823e2f9fcdf730b8182c935574691e811e7d46399fe0ff0087795cd207")
}
const CBasic1EventName = "basic1"
func (_C *C) UnpackBasic1Event(log *types.Log) (*CBasic1, error) {
// TODO: okay to index by the original name here? I think so because we assume that the abi json is well-formed.
// and we only need normalized name when dealing with generated go symbols.
event := "basic1"
if log.Topics[0] != _C.abi.Events[event].ID {
return nil, errors.New("event signature mismatch")
@ -166,13 +162,9 @@ type CBasic2 struct {
Raw *types.Log // Blockchain specific contextual infos
}
func CBasic2EventID() common.Hash {
return common.HexToHash("0x3b29b9f6d15ba80d866afb3d70b7548ab1ffda3ef6e65f35f1cb05b0e2b29f4e")
}
const CBasic2EventName = "basic2"
func (_C *C) UnpackBasic2Event(log *types.Log) (*CBasic2, error) {
// TODO: okay to index by the original name here? I think so because we assume that the abi json is well-formed.
// and we only need normalized name when dealing with generated go symbols.
event := "basic2"
if log.Topics[0] != _C.abi.Events[event].ID {
return nil, errors.New("event signature mismatch")

View File

@ -30,13 +30,14 @@ import (
type ContractInstance struct {
Address common.Address
Backend bind.ContractBackend
abi abi.ABI
}
// FilterEvents returns an EventIterator instance for filtering historical events based on the event id and a block range.
func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, eventName string, unpack func(*types.Log) (*T, error), topics ...[]any) (*EventIterator[T], error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
logs, sub, err := c.FilterLogsById(opts, eventID, topics...)
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
logs, sub, err := c.FilterLogs(opts, eventName, topics...)
if err != nil {
return nil, err
}
@ -47,10 +48,10 @@ func FilterEvents[T any](instance *ContractInstance, opts *bind.FilterOpts, even
// contract to be intercepted, unpacked, and forwarded to sink. If
// unpack returns an error, the returned subscription is closed with the
// error.
func WatchEvents[T any](instance *ContractInstance, opts *bind.WatchOpts, eventID common.Hash, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
func WatchEvents[T any](instance *ContractInstance, opts *bind.WatchOpts, eventName string, unpack func(*types.Log) (*T, error), sink chan<- *T, topics ...[]any) (event.Subscription, error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
logs, sub, err := c.WatchLogsForId(opts, eventID, topics...)
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
logs, sub, err := c.WatchLogs(opts, eventName, topics...)
if err != nil {
return nil, err
}
@ -159,7 +160,7 @@ func Transact(instance *ContractInstance, opts *bind.TransactOpts, input []byte)
addr = instance.Address
backend = instance.Backend
)
c := bind.NewBoundContract(addr, abi.ABI{}, backend, backend, backend)
c := bind.NewBoundContract(addr, instance.abi, backend, backend, backend)
return c.RawTransact(opts, input)
}
@ -167,7 +168,7 @@ func Transact(instance *ContractInstance, opts *bind.TransactOpts, input []byte)
// provided abi-encoded input (or nil).
func Call[T any](instance *ContractInstance, opts *bind.CallOpts, packedInput []byte, unpack func([]byte) (*T, error)) (*T, error) {
backend := instance.Backend
c := bind.NewBoundContract(instance.Address, abi.ABI{}, backend, backend, backend)
c := bind.NewBoundContract(instance.Address, instance.abi, backend, backend, backend)
packedOutput, err := c.CallRaw(opts, packedInput)
if err != nil {
return nil, err

View File

@ -290,9 +290,11 @@ func TestEvents(t *testing.T) {
t.Fatalf("error instantiating contract instance: %v", err)
}
boundContract := ContractInstance{
ctrctABI, _ := events.CMetaData.GetAbi()
ctrctInstance := ContractInstance{
res.Addrs[events.CMetaData.Pattern],
backend,
*ctrctABI,
}
newCBasic1Ch := make(chan *events.CBasic1)
@ -301,23 +303,19 @@ func TestEvents(t *testing.T) {
Start: nil,
Context: context.Background(),
}
sub1, err := WatchEvents(&boundContract, watchOpts, events.CBasic1EventID(), ctrct.UnpackBasic1Event, newCBasic1Ch)
sub1, err := WatchEvents(&ctrctInstance, watchOpts, events.CBasic1EventName, ctrct.UnpackBasic1Event, newCBasic1Ch)
if err != nil {
t.Fatalf("WatchEvents returned error: %v", err)
}
sub2, err := WatchEvents(&boundContract, watchOpts, events.CBasic2EventID(), ctrct.UnpackBasic2Event, newCBasic2Ch)
sub2, err := WatchEvents(&ctrctInstance, watchOpts, events.CBasic2EventName, ctrct.UnpackBasic2Event, newCBasic2Ch)
if err != nil {
t.Fatalf("WatchEvents returned error: %v", err)
}
defer sub1.Unsubscribe()
defer sub2.Unsubscribe()
crtctInstance := &ContractInstance{
Address: res.Addrs[events.CMetaData.Pattern],
Backend: backend,
}
packedInput, _ := ctrct.PackEmitMulti()
tx, err := Transact(crtctInstance, txAuth, packedInput)
tx, err := Transact(&ctrctInstance, txAuth, packedInput)
if err != nil {
t.Fatalf("failed to send transaction: %v", err)
}
@ -356,11 +354,11 @@ done:
Start: 0,
Context: context.Background(),
}
it, err := FilterEvents[events.CBasic1](crtctInstance, filterOpts, events.CBasic1EventID(), ctrct.UnpackBasic1Event)
it, err := FilterEvents[events.CBasic1](&ctrctInstance, filterOpts, events.CBasic1EventName, ctrct.UnpackBasic1Event)
if err != nil {
t.Fatalf("error filtering logs %v\n", err)
}
it2, err := FilterEvents[events.CBasic2](crtctInstance, filterOpts, events.CBasic2EventID(), ctrct.UnpackBasic2Event)
it2, err := FilterEvents[events.CBasic2](&ctrctInstance, filterOpts, events.CBasic2EventName, ctrct.UnpackBasic2Event)
if err != nil {
t.Fatalf("error filtering logs %v\n", err)
}
@ -409,7 +407,8 @@ func TestErrors(t *testing.T) {
}
packedInput, _ = ctrct.PackFoo()
instance := ContractInstance{res.Addrs[solc_errors.CMetaData.Pattern], backend}
ctrctABI, _ := solc_errors.CMetaData.GetAbi()
instance := ContractInstance{res.Addrs[solc_errors.CMetaData.Pattern], backend, *ctrctABI}
_, err = Call[struct{}](&instance, opts, packedInput, func(packed []byte) (*struct{}, error) { return nil, nil })
if err == nil {
t.Fatalf("expected call to fail")