func (s *Service) StartWatchers() error {

	var (
		// maps pool name -> contract address
		addresses = make(map[string]string)
		// maps pool name -> break percentage
		spotPriceBreakPercentages = make(map[string]float64)
	)

	for _, pool := range s.pools {
		addresses[strings.ToLower(pool.Name)] = pool.ContractAddress
		spotPriceBreakPercentages[strings.ToLower(pool.Name)] = pool.SpotPriceBreakPercentage
	}

	// get the bindings
	// name -> contract
	bindings, err := s.ew.NewBindings(addresses)
	if err != nil {
		return err
	}

	watchers, err := s.ew.NewWatchedContracts(s.logger, bindings, s.minimumGwei, s.gasMultiplier)
	if err != nil {
		return err
	}

	var (
		// create a dedicated context for the contract watchers
		// this will allow us to easily cancel this context upon receiving an error
		// from one of the spawned watchers, allowing us to gracefully exit and restart
		ctx, cancel = context.WithCancel(s.ctx)
		// this error channel will hold any errors returned from a single contract watcher
		// it will allow us to block until either a context is cancelled or an error is returned
		// we create a channel with a capacity equal to the number of watchers we will spawn
		errCh = make(chan error, len(watchers))
	)

	s.wg.Add(len(watchers))

	for _, watcher := range watchers {
		go func(wtchr *eventwatcher.WatchedContract, bkPercent float64) {
			defer s.wg.Done()
			if err := wtchr.Listen(ctx, s.db, s.at, s.auther, bkPercent); err != nil {
				errCh <- err
			}
		}(watcher, spotPriceBreakPercentages[strings.ToLower(watcher.Name())])
	}

	select {
	case <-ctx.Done():
		cancel()
		return nil
	case err := <-errCh:
		s.logger.Error("encountered error from contract watcher, aborting...", zap.Error(err))
		cancel()
		return err
	}

}
// StartWatchers starts all of our contract watchers
// which listen for LOG_SWAP events to potentially trigger
// circuit breaking activities
StartWatchers starts all of our contract watchers
which listen for LOG_SWAP events to potentially trigger
circuit breaking activities
// StartWatchers starts all of our contract watchers // which listen for LOG_SWAP events to potentially trigger // circuit breaking activities
func (s *Service) StartWatchers() error {
	var (
		addresses = make(map[string]string)
		spotPriceBreakPercentages = make(map[string]float64)
	)
	for _, pool := range s.pools {
		addresses[strings.ToLower(pool.Name)] = pool.ContractAddress
		spotPriceBreakPercentages[strings.ToLower(pool.Name)] = pool.SpotPriceBreakPercentage
	}
	bindings, err := s.ew.NewBindings(addresses)
	if err != nil {
		return err
	}
	watchers, err := s.ew.NewWatchedContracts(s.logger, bindings, s.minimumGwei, s.gasMultiplier)
	if err != nil {
		return err
	}
	var (
		ctx, cancel = context.WithCancel(s.ctx)
		errCh = make(chan error, len(watchers))
	)
	s.wg.Add(len(watchers))
	for _, watcher := range watchers {
		go func(wtchr *eventwatcher.WatchedContract, bkPercent float64) {
			defer s.wg.Done()
			if err := wtchr.Listen(ctx, s.db, s.at, s.auther, bkPercent); err != nil {
				errCh <- err
			}
		}(watcher, spotPriceBreakPercentages[strings.ToLower(watcher.Name())])
	}
	select {
	case <-ctx.Done():
		cancel()
		return nil
	case err := <-errCh:
		s.logger.Error("encountered error from contract watcher, aborting...", zap.Error(err))
		cancel()
		return err
	}
}
func ( s * Service ) StartWatchers ( ) error { var ( addresses = make ( map [ string ] string ) spotPriceBreakPercentages = make ( map [ string ] float64 ) ) for _ , pool := range s . pools { addresses [ strings . ToLower ( pool . Name ) ] = pool . ContractAddress spotPriceBreakPercentages [ strings . ToLower ( pool . Name ) ] = pool . SpotPriceBreakPercentage } bindings , err := s . ew . NewBindings ( addresses ) if err != nil { return err } watchers , err := s . ew . NewWatchedContracts ( s . logger , bindings , s . minimumGwei , s . gasMultiplier ) if err != nil { return err } var ( ctx , cancel = context . WithCancel ( s . ctx ) errCh = make ( chan error , len ( watchers ) ) ) s . wg . Add ( len ( watchers ) ) for _ , watcher := range watchers { go func ( wtchr * eventwatcher . WatchedContract , bkPercent float64 ) { defer s . wg . Done ( ) if err := wtchr . Listen ( ctx , s . db , s . at , s . auther , bkPercent ) ; err != nil { errCh <- err } } ( watcher , spotPriceBreakPercentages [ strings . ToLower ( watcher . Name ( ) ) ] ) } select { case <- ctx . Done ( ) : cancel ( ) return nil case err := <- errCh : s . logger . Error ( " " , zap . Error ( err ) ) cancel ( ) return err } }
StartWatchers starts all of our contract watchers
which listen for LOG_SWAP events to potentially trigger
circuit breaking activities
StartWatchers starts all of our contract watchers which listen for LOG_SWAP events to potentially trigger circuit breaking activities
// maps pool name -> contract address // maps pool name -> break percentage // get the bindings // name -> contract // create a dedicated context for the contract watchers // this will allow us to easily cancel this context upon receiving an error // from one of the spawned watchers, allowing us to gracefully exit and restart // this error channel will hold any errors returned from a single contract watcher // it will allow us to block until either a context is cancelled or an error is returned // we create a channel with a capacity equal to the number of watchers we will spawn