func (t *TicketPurchaser) calcAverageTicketPrice(height int64, keyHeight int64) (hcashutil.Amount, error) {
	var avgPricePoolAmt hcashutil.Amount
	if t.priceMode == AvgPricePoolMode || t.priceMode == AvgPriceDualMode {
		poolValue, err := t.hcashdChainSvr.GetTicketPoolValue()
		if err != nil {
			return 0, err
		}
		bestBlockH, err := t.hcashdChainSvr.GetBestBlockHash()
		if err != nil {
			return 0, err
		}
		blkHeader, err := t.hcashdChainSvr.GetBlockHeader(bestBlockH)
		if err != nil {
			return 0, err
		}
		poolSize := blkHeader.PoolSize
		if poolSize == 0 {
			poolSize++
		}
		avgPricePoolAmt = poolValue / hcashutil.Amount(poolSize)
		if t.priceMode == AvgPricePoolMode {
			return avgPricePoolAmt, err
		}
	}
	var ticketVWAP hcashutil.Amount
	if t.priceMode == AvgPriceVWAPMode || t.priceMode == AvgPriceDualMode {
		startVWAPkeyHeight := uint32(0)
		if keyHeight-int64(t.cfg.AvgPriceVWAPDelta) > 0 {
			startVWAPkeyHeight = uint32(keyHeight - int64(t.cfg.AvgPriceVWAPDelta))
		}
		endVWAPKeyHeight := uint32(keyHeight)
		var err error
		endVWAPHeight := uint32(height)
		for i := 0; i < vwapReqTries; i++ {
			ticketVWAP, err = t.hcashdChainSvr.TicketVWAP(&startVWAPkeyHeight,
				&endVWAPKeyHeight, &endVWAPHeight)
			if err != nil && containsVWAPHeightOffsetError(err) {
				log.Tracef("Failed to fetch ticket VWAP "+
					"on attempt %v: %v", i, err.Error())
				err = nil
				time.Sleep(vwapReqTryDelay)
				continue
			}
			if err == nil {
				break
			}
		}
		if err != nil {
			return 0, err
		}
		if t.priceMode == AvgPriceVWAPMode {
			return ticketVWAP, err
		}
	}
	return (ticketVWAP + avgPricePoolAmt) / 2, nil
}
calcAverageTicketPrice calculates the average price of a ticket based on
the parameters set by the user.
package ticketbuyer
import (
	"strings"
	"time"
	"github.com/HcashOrg/hcashutil"
)
var (
	vwapReqTries = 20
	vwapReqTryDelay = time.Millisecond * 500
)
type avgPriceMode int
const (
	AvgPriceVWAPMode = iota
	AvgPricePoolMode
	AvgPriceDualMode
)
var vwapHeightOffsetErrStr = "beyond blockchain tip height"
func containsVWAPHeightOffsetError(err error) bool {
	if err == nil {
		return false
	}
	return strings.Contains(err.Error(), vwapHeightOffsetErrStr)
}