func (w *Worker) AgentAfterChannelCreate(job *data.Job) error {
	if w.isJobInvalid(job, data.JobAgentAfterChannelCreate, data.JobChannel) {
		return ErrInvalidJob
	}

	logger := w.logger.Add("method", "AgentAfterChannelCreate", "job", job)

	ethLog, err := w.ethLog(logger, job)
	if err != nil {
		return err
	}

	ethLogTx, err := w.ethLogTx(logger, ethLog)
	if err != nil {
		return err
	}

	client, newUser, err := w.newUser(logger, ethLogTx)
	if err != nil {
		return err
	}

	logger = logger.Add("client", client)

	tx, err := w.db.Begin()
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}
	defer tx.Rollback()

	if newUser {
		if err := tx.Insert(client); err != nil {
			logger.Error(err.Error())
			return ErrInternal
		}
	}

	logChannelCreated, err := extractLogChannelCreated(logger, ethLog)
	if err != nil {
		return err
	}

	offering, err := w.offeringByHash(logger, logChannelCreated.offeringHash)
	if err != nil {
		return err
	}

	offering.CurrentSupply--
	if err := tx.Update(offering); err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	channel := &data.Channel{
		ID:            job.RelatedID,
		Client:        data.HexFromBytes(logChannelCreated.clientAddr.Bytes()),
		Agent:         data.HexFromBytes(logChannelCreated.agentAddr.Bytes()),
		TotalDeposit:  logChannelCreated.deposit.Uint64(),
		ChannelStatus: data.ChannelActive,
		ServiceStatus: data.ServicePending,
		Offering:      offering.ID,
		Block:         uint32(ethLog.Block),
	}

	if err := tx.Insert(channel); err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	if err := w.addJob(logger, tx, data.JobAgentPreEndpointMsgCreate,
		data.JobChannel, channel.ID); err != nil {
		return err
	}

	if err := tx.Commit(); err != nil {
		logger.Error("unable to commit changes: " + err.Error())
		return ErrInternal
	}

	return nil
}
func (w *Worker) AgentAfterChannelCreate(job *data.Job) error {
	if w.isJobInvalid(job, data.JobAgentAfterChannelCreate, data.JobChannel) {
		return ErrInvalidJob
	}

	logger := w.logger.Add("method", "AgentAfterChannelCreate", "job", job)

	ethLog, err := w.ethLog(logger, job)
	if err != nil {
		return err
	}

	ethLogTx, err := w.ethLogTx(logger, ethLog)
	if err != nil {
		return err
	}

	client, newUser, err := w.newUser(logger, ethLogTx)
	if err != nil {
		return err
	}

	logger = logger.Add("client", client)

	tx, err := w.db.Begin()
	if err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}
	defer tx.Rollback()

	if newUser {
		if err := tx.Insert(client); err != nil {
			logger.Error(err.Error())
			return ErrInternal
		}
	}

	logChannelCreated, err := extractLogChannelCreated(logger, ethLog)
	if err != nil {
		return err
	}

	offering, err := w.offeringByHash(logger, logChannelCreated.offeringHash)
	if err != nil {
		return err
	}

	offering.CurrentSupply--
	if err := tx.Update(offering); err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	channel := &data.Channel{
		ID:            job.RelatedID,
		Client:        data.HexFromBytes(logChannelCreated.clientAddr.Bytes()),
		Agent:         data.HexFromBytes(logChannelCreated.agentAddr.Bytes()),
		TotalDeposit:  logChannelCreated.deposit.Uint64(),
		ChannelStatus: data.ChannelActive,
		ServiceStatus: data.ServicePending,
		Offering:      offering.ID,
		Block:         uint32(ethLog.Block),
	}

	if err := tx.Insert(channel); err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	if err := w.addJob(logger, tx, data.JobAgentPreEndpointMsgCreate,
		data.JobChannel, channel.ID); err != nil {
		return err
	}

	if err := tx.Commit(); err != nil {
		logger.Error("unable to commit changes: " + err.Error())
		return ErrInternal
	}

	return nil
}
// AgentAfterChannelCreate registers client and creates pre service create job.