func NewOperator(config *OperatorConfig) (*Operator, error) {
	op := &Operator{}

	rpcClient, err := rpc.DialWebsocket(context.Background(), config.OperatorParams.rpcURL, "")
	if err != nil {
		return nil, err
	}

	client := ethclient.NewClient(rpcClient)
	ex, err := NewExchange(config.Admin, config.Exchange, client)
	if err != nil {
		return nil, err
	}

	op.Admin = config.Admin
	op.Params = config.OperatorParams
	op.Exchange = ex
	op.EthereumClient = client
	op.OrderTradePairs = make(map[Hash]*OrderTradePair)

	op.ErrorChannel, err = op.Exchange.ListenToErrorEvents()
	if err != nil {
		return nil, err
	}

	op.TradeChannel, err = op.Exchange.ListenToTrades()
	if err != nil {
		return nil, err
	}

	err = op.Validate()
	if err != nil {
		return nil, err
	}

	go func() {
		for {
			select {
			case errLog := <-op.ErrorChannel:
				tradeHash := errLog.TradeHash
				otp, ok := op.OrderTradePairs[tradeHash]
				if !ok {
					log.Printf("Could not retrieve initial hash")
				}

				t := otp.trade
				o := otp.order

				errId := errLog.ErrorId

				if otp.order.events != nil {
					otp.order.events <- otp.order.NewOrderTxError(t, errId)
				}

				if otp.trade.events != nil {
					otp.trade.events <- otp.trade.NewTradeTxError(o, errId)
				}

				if op.Engine != nil {
					if err := op.Engine.CancelTrade(t); err != nil {
						log.Printf("Could not update order")
					}
				}

			case tradeLog := <-op.TradeChannel:
				otp, ok := op.OrderTradePairs[tradeLog.TradeHash]
				if !ok {
					log.Printf("Could not retrieve initial hash")
				}

				if otp.order.events != nil {
					otp.order.events <- otp.order.NewOrderTxSuccess(otp.trade)
				}

				if otp.trade.events != nil {
					otp.trade.events <- otp.trade.NewTradeTxSuccess(otp.order)
				}
			}
		}
	}()

	return op, nil
}
// NewOperator creates a new operator struct. It creates an exchange contract instance from the
// provided address. The error and trade events are received in the ErrorChannel and TradeChannel.
// Upon receiving errors and trades in their respective channels, event payloads are sent to the
// associated order maker and taker sockets through the through the event channel on the Order and Trade struct.
// In addition, an error event cancels the trade in the trading engine and makes the order available again.
NewOperator creates a new operator struct. It creates an exchange contract instance from the
provided address. The error and trade events are received in the ErrorChannel and TradeChannel.
Upon receiving errors and trades in their respective channels, event payloads are sent to the
associated order maker and taker sockets through the through the event channel on the Order and Trade struct.
In addition, an error event cancels the trade in the trading engine and makes the order available again.
// NewOperator creates a new operator struct . It creates an exchange contract instance from the // provided address . The error and trade events are received in the ErrorChannel and TradeChannel . // Upon receiving errors and trades in their respective channels event payloads are sent to the // associated order maker and taker sockets through the through the event channel on the Order and Trade struct . // In addition an error event cancels the trade in the trading engine and makes the order available again .
func NewOperator(config *OperatorConfig) (*Operator, error) {
	op := &Operator{}
	rpcClient, err := rpc.DialWebsocket(context.Background(), config.OperatorParams.rpcURL, "")
	if err != nil {
		return nil, err
	}
	client := ethclient.NewClient(rpcClient)
	ex, err := NewExchange(config.Admin, config.Exchange, client)
	if err != nil {
		return nil, err
	}
	op.Admin = config.Admin
	op.Params = config.OperatorParams
	op.Exchange = ex
	op.EthereumClient = client
	op.OrderTradePairs = make(map[Hash]*OrderTradePair)
	op.ErrorChannel, err = op.Exchange.ListenToErrorEvents()
	if err != nil {
		return nil, err
	}
	op.TradeChannel, err = op.Exchange.ListenToTrades()
	if err != nil {
		return nil, err
	}
	err = op.Validate()
	if err != nil {
		return nil, err
	}
	go func() {
		for {
			select {
			case errLog := <-op.ErrorChannel:
				tradeHash := errLog.TradeHash
				otp, ok := op.OrderTradePairs[tradeHash]
				if !ok {
					log.Printf("Could not retrieve initial hash")
				}
				t := otp.trade
				o := otp.order
				errId := errLog.ErrorId
				if otp.order.events != nil {
					otp.order.events <- otp.order.NewOrderTxError(t, errId)
				}
				if otp.trade.events != nil {
					otp.trade.events <- otp.trade.NewTradeTxError(o, errId)
				}
				if op.Engine != nil {
					if err := op.Engine.CancelTrade(t); err != nil {
						log.Printf("Could not update order")
					}
				}
			case tradeLog := <-op.TradeChannel:
				otp, ok := op.OrderTradePairs[tradeLog.TradeHash]
				if !ok {
					log.Printf("Could not retrieve initial hash")
				}
				if otp.order.events != nil {
					otp.order.events <- otp.order.NewOrderTxSuccess(otp.trade)
				}
				if otp.trade.events != nil {
					otp.trade.events <- otp.trade.NewTradeTxSuccess(otp.order)
				}
			}
		}
	}()
	return op, nil
}
func NewOperator ( config * OperatorConfig ) ( * Operator , error ) { op := & Operator { } rpcClient , err := rpc . DialWebsocket ( context . Background ( ) , config . OperatorParams . rpcURL , " " ) if err != nil { return nil , err } client := ethclient . NewClient ( rpcClient ) ex , err := NewExchange ( config . Admin , config . Exchange , client ) if err != nil { return nil , err } op . Admin = config . Admin op . Params = config . OperatorParams op . Exchange = ex op . EthereumClient = client op . OrderTradePairs = make ( map [ Hash ] * OrderTradePair ) op . ErrorChannel , err = op . Exchange . ListenToErrorEvents ( ) if err != nil { return nil , err } op . TradeChannel , err = op . Exchange . ListenToTrades ( ) if err != nil { return nil , err } err = op . Validate ( ) if err != nil { return nil , err } go func ( ) { for { select { case errLog := <- op . ErrorChannel : tradeHash := errLog . TradeHash otp , ok := op . OrderTradePairs [ tradeHash ] if ! ok { log . Printf ( " " ) } t := otp . trade o := otp . order errId := errLog . ErrorId if otp . order . events != nil { otp . order . events <- otp . order . NewOrderTxError ( t , errId ) } if otp . trade . events != nil { otp . trade . events <- otp . trade . NewTradeTxError ( o , errId ) } if op . Engine != nil { if err := op . Engine . CancelTrade ( t ) ; err != nil { log . Printf ( " " ) } } case tradeLog := <- op . TradeChannel : otp , ok := op . OrderTradePairs [ tradeLog . TradeHash ] if ! ok { log . Printf ( " " ) } if otp . order . events != nil { otp . order . events <- otp . order . NewOrderTxSuccess ( otp . trade ) } if otp . trade . events != nil { otp . trade . events <- otp . trade . NewTradeTxSuccess ( otp . order ) } } } } ( ) return op , nil }
NewOperator creates a new operator struct.
NewOperator creates a new operator struct .