proc/worker/account.go
func (w *Worker) PreAccountAddBalanceApprove(job *data.Job) error {
	logger := w.logger.Add("method", "PreAccountAddBalanceApprove", "job", job)

	acc, err := w.relatedAccount(logger, job,
		data.JobPreAccountAddBalanceApprove)
	if err != nil {
		return err
	}

	logger = logger.Add("account", acc)

	jobData, err := w.balanceData(logger, job)
	if err != nil {
		return fmt.Errorf("failed to parse job data: %v", err)
	}

	addr, err := data.HexToAddress(acc.EthAddr)
	if err != nil {
		logger.Error(err.Error())
		return ErrParseEthAddr
	}

	amount, err := w.ethBack.PTCBalanceOf(&bind.CallOpts{}, addr)
	if err != nil {
		logger.Error(err.Error())
		return ErrPTCRetrieveBalance
	}

	if amount.Uint64() < uint64(jobData.Amount) {
		return ErrInsufficientPTCBalance
	}

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

	wantedEthBalance := w.gasConf.PTC.Approve * jobData.GasPrice

	if wantedEthBalance > ethBalance.Uint64() {
		return ErrInsufficientEthBalance
	}

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

	auth := bind.NewKeyedTransactor(key)
	auth.GasLimit = w.gasConf.PTC.Approve
	auth.GasPrice = new(big.Int).SetUint64(jobData.GasPrice)
	tx, err := w.ethBack.PTCIncreaseApproval(auth,
		w.pscAddr, new(big.Int).SetUint64(jobData.Amount))
	if err != nil {
		logger.Error(err.Error())
		return ErrPTCIncreaseApproval
	}

	return w.saveEthTX(logger, job, tx, "PTCIncreaseApproval", job.RelatedType,
		job.RelatedID, acc.EthAddr, data.HexFromBytes(w.pscAddr.Bytes()))
}
func (w *Worker) PreAccountAddBalanceApprove(job *data.Job) error {
	logger := w.logger.Add("method", "PreAccountAddBalanceApprove", "job", job)

	acc, err := w.relatedAccount(logger, job,
		data.JobPreAccountAddBalanceApprove)
	if err != nil {
		return err
	}

	logger = logger.Add("account", acc)

	jobData, err := w.balanceData(logger, job)
	if err != nil {
		return fmt.Errorf("failed to parse job data: %v", err)
	}

	addr, err := data.HexToAddress(acc.EthAddr)
	if err != nil {
		logger.Error(err.Error())
		return ErrParseEthAddr
	}

	amount, err := w.ethBack.PTCBalanceOf(&bind.CallOpts{}, addr)
	if err != nil {
		logger.Error(err.Error())
		return ErrPTCRetrieveBalance
	}

	if amount.Uint64() < uint64(jobData.Amount) {
		return ErrInsufficientPTCBalance
	}

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

	wantedEthBalance := w.gasConf.PTC.Approve * jobData.GasPrice

	if wantedEthBalance > ethBalance.Uint64() {
		return ErrInsufficientEthBalance
	}

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

	auth := bind.NewKeyedTransactor(key)
	auth.GasLimit = w.gasConf.PTC.Approve
	auth.GasPrice = new(big.Int).SetUint64(jobData.GasPrice)
	tx, err := w.ethBack.PTCIncreaseApproval(auth,
		w.pscAddr, new(big.Int).SetUint64(jobData.Amount))
	if err != nil {
		logger.Error(err.Error())
		return ErrPTCIncreaseApproval
	}

	return w.saveEthTX(logger, job, tx, "PTCIncreaseApproval", job.RelatedType,
		job.RelatedID, acc.EthAddr, data.HexFromBytes(w.pscAddr.Bytes()))
}
// PreAccountAddBalanceApprove approve balance if amount exists.