func (w *Worker) AgentPreOfferingMsgBCPublish(job *data.Job) error {
	logger := w.logger.Add("method", "AgentPreOfferingMsgBCPublish",
		"job", job)
	offering, err := w.relatedOffering(logger, job,
		data.JobAgentPreOfferingMsgBCPublish)
	if err != nil {
		return err
	}

	minDeposit := data.MinDeposit(offering)

	agent, err := w.account(logger, offering.Agent)
	if err != nil {
		return err
	}

	agentKey, err := w.key(logger, agent.PrivateKey)
	if err != nil {
		return err
	}

	product, err := w.productByPK(logger, offering.Product)
	if err != nil {
		return err
	}

	offering.Country = country.UndefinedCountry

	if product.Country != nil && len(*product.Country) == 2 {
		offering.Country = *product.Country
	}

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

	offeringHash, err := data.HexToBytes(offering.Hash)
	if err != nil {
		return err
	}

	auth := bind.NewKeyedTransactor(agentKey)

	pscBalance, err := w.ethBack.PSCBalanceOf(&bind.CallOpts{}, auth.From)

	if err != nil {
		logger.Error(err.Error())
		return ErrPSCReturnBalance
	}

	totalDeposit := minDeposit * uint64(offering.Supply)
	if uint64(pscBalance) < totalDeposit {
		return ErrInsufficientPSCBalance
	}

	ethAmount, err := w.ethBalance(logger, auth.From)
	if err != nil {
		return err
	}

	wantedEthBalance := auth.GasLimit * publishData.GasPrice
	if wantedEthBalance > ethAmount.Uint64() {
		return ErrInsufficientEthBalance
	}

	auth.GasLimit = w.gasConf.PSC.RegisterServiceOffering
	auth.GasPrice = new(big.Int).SetUint64(publishData.GasPrice)

	if w.torHostName == "" {
		return ErrTorNoSet
	}

	offering.SOMCType = data.OfferingSOMCTor
	offering.SOMCData = w.torHostName

	tx, err := w.ethBack.RegisterServiceOffering(auth,
		[common.HashLength]byte(common.BytesToHash(offeringHash)),
		uint64(minDeposit), offering.Supply,
		offering.SOMCType, offering.SOMCData)
	if err != nil {
		logger.Add("GasLimit", auth.GasLimit,
			"GasPrice", auth.GasPrice).Error(err.Error())
		return ErrPSCRegisterOffering
	}

	offering.Status = data.OfferRegistering
	if err = w.db.Update(offering); err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	return w.saveEthTX(logger, job, tx, "RegisterServiceOffering",
		job.RelatedType, job.RelatedID, agent.EthAddr,
		data.HexFromBytes(w.pscAddr.Bytes()))
}
func (w *Worker) AgentPreOfferingMsgBCPublish(job *data.Job) error {
	logger := w.logger.Add("method", "AgentPreOfferingMsgBCPublish",
		"job", job)
	offering, err := w.relatedOffering(logger, job,
		data.JobAgentPreOfferingMsgBCPublish)
	if err != nil {
		return err
	}

	minDeposit := data.MinDeposit(offering)

	agent, err := w.account(logger, offering.Agent)
	if err != nil {
		return err
	}

	agentKey, err := w.key(logger, agent.PrivateKey)
	if err != nil {
		return err
	}

	product, err := w.productByPK(logger, offering.Product)
	if err != nil {
		return err
	}

	offering.Country = country.UndefinedCountry

	if product.Country != nil && len(*product.Country) == 2 {
		offering.Country = *product.Country
	}

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

	offeringHash, err := data.HexToBytes(offering.Hash)
	if err != nil {
		return err
	}

	auth := bind.NewKeyedTransactor(agentKey)

	pscBalance, err := w.ethBack.PSCBalanceOf(&bind.CallOpts{}, auth.From)

	if err != nil {
		logger.Error(err.Error())
		return ErrPSCReturnBalance
	}

	totalDeposit := minDeposit * uint64(offering.Supply)
	if uint64(pscBalance) < totalDeposit {
		return ErrInsufficientPSCBalance
	}

	ethAmount, err := w.ethBalance(logger, auth.From)
	if err != nil {
		return err
	}

	wantedEthBalance := auth.GasLimit * publishData.GasPrice
	if wantedEthBalance > ethAmount.Uint64() {
		return ErrInsufficientEthBalance
	}

	auth.GasLimit = w.gasConf.PSC.RegisterServiceOffering
	auth.GasPrice = new(big.Int).SetUint64(publishData.GasPrice)

	if w.torHostName == "" {
		return ErrTorNoSet
	}

	offering.SOMCType = data.OfferingSOMCTor
	offering.SOMCData = w.torHostName

	tx, err := w.ethBack.RegisterServiceOffering(auth,
		[common.HashLength]byte(common.BytesToHash(offeringHash)),
		uint64(minDeposit), offering.Supply,
		offering.SOMCType, offering.SOMCData)
	if err != nil {
		logger.Add("GasLimit", auth.GasLimit,
			"GasPrice", auth.GasPrice).Error(err.Error())
		return ErrPSCRegisterOffering
	}

	offering.Status = data.OfferRegistering
	if err = w.db.Update(offering); err != nil {
		logger.Error(err.Error())
		return ErrInternal
	}

	return w.saveEthTX(logger, job, tx, "RegisterServiceOffering",
		job.RelatedType, job.RelatedID, agent.EthAddr,
		data.HexFromBytes(w.pscAddr.Bytes()))
}
func ( w * Worker ) AgentPreOfferingMsgBCPublish ( job * data . Job ) error { logger := w . logger . Add ( " " , " " , " " , job ) offering , err := w . relatedOffering ( logger , job , data . JobAgentPreOfferingMsgBCPublish ) if err != nil { return err } minDeposit := data . MinDeposit ( offering ) agent , err := w . account ( logger , offering . Agent ) if err != nil { return err } agentKey , err := w . key ( logger , agent . PrivateKey ) if err != nil { return err } product , err := w . productByPK ( logger , offering . Product ) if err != nil { return err } offering . Country = country . UndefinedCountry if product . Country != nil && len ( * product . Country ) == 2 { offering . Country = * product . Country } publishData , err := w . publishData ( logger , job ) if err != nil { return err } offeringHash , err := data . HexToBytes ( offering . Hash ) if err != nil { return err } auth := bind . NewKeyedTransactor ( agentKey ) pscBalance , err := w . ethBack . PSCBalanceOf ( & bind . CallOpts { } , auth . From ) if err != nil { logger . Error ( err . Error ( ) ) return ErrPSCReturnBalance } totalDeposit := minDeposit * uint64 ( offering . Supply ) if uint64 ( pscBalance ) < totalDeposit { return ErrInsufficientPSCBalance } ethAmount , err := w . ethBalance ( logger , auth . From ) if err != nil { return err } wantedEthBalance := auth . GasLimit * publishData . GasPrice if wantedEthBalance > ethAmount . Uint64 ( ) { return ErrInsufficientEthBalance } auth . GasLimit = w . gasConf . PSC . RegisterServiceOffering auth . GasPrice = new ( big . Int ) . SetUint64 ( publishData . GasPrice ) if w . torHostName == " " { return ErrTorNoSet } offering . SOMCType = data . OfferingSOMCTor offering . SOMCData = w . torHostName tx , err := w . ethBack . RegisterServiceOffering ( auth , [ common . HashLength ] byte ( common . BytesToHash ( offeringHash ) ) , uint64 ( minDeposit ) , offering . Supply , offering . SOMCType , offering . SOMCData ) if err != nil { logger . Add ( " " , auth . GasLimit , " " , auth . GasPrice ) . Error ( err . Error ( ) ) return ErrPSCRegisterOffering } offering . Status = data . OfferRegistering if err = w . db . Update ( offering ) ; err != nil { logger . Error ( err . Error ( ) ) return ErrInternal } return w . saveEthTX ( logger , job , tx , " " , job . RelatedType , job . RelatedID , agent . EthAddr , data . HexFromBytes ( w . pscAddr . Bytes ( ) ) ) }
// AgentPreOfferingMsgBCPublish publishes offering to blockchain.
AgentPreOfferingMsgBCPublish publishes offering to blockchain .