Skip to content

Instantly share code, notes, and snippets.

@MariusVanDerWijden
Created July 8, 2020 09:24
Show Gist options
  • Save MariusVanDerWijden/d560b48b23d7dc1565658fe1aaab6ae4 to your computer and use it in GitHub Desktop.
Save MariusVanDerWijden/d560b48b23d7dc1565658fe1aaab6ae4 to your computer and use it in GitHub Desktop.
// Watch for a Deposited event
watchOpts := &bind.WatchOpts{Context: ctx, Start: nil}
// Setup a channel for results
channel := make(chan *coolcontract.CoolContractDeposited)
// Start a goroutine which watches new events
go func() {
sub, err := ctr.WatchDeposited(watchOpts, channel)
defer sub.Unsubscribe()
}()
// Receive events from the channel
event := <-channel
@bajelidze
Copy link

The call to ctr.WatchDeposited is non-blocking so as soon as it returns you will call sub.Unsubscribe which will prevent receipt of any events on the channel. I would suggest calling WatchDeposited without invoking a separate goroutine. Also, you should be listening on channel sub.Err() onto which come watcher errors.

Anyways, thanks for the article! It was really helpful to get some initial directions with all these Go bindings stuff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment