Пример #1
0
// Sleep will halt the console for arg[0] seconds.
func (self *Jeth) Sleep(call otto.FunctionCall) (response otto.Value) {
	if len(call.ArgumentList) >= 1 {
		if call.Argument(0).IsNumber() {
			sleep, _ := call.Argument(0).ToInteger()
			time.Sleep(time.Duration(sleep) * time.Second)
			return otto.TrueValue()
		}
	}
	return throwJSExeception("usage: sleep(<sleep in seconds>)")
}
Пример #2
0
// SleepBlocks will wait for a specified number of new blocks or max for a
// given of seconds. sleepBlocks(nBlocks[, maxSleep]).
func (self *Jeth) SleepBlocks(call otto.FunctionCall) (response otto.Value) {
	nBlocks := int64(0)
	maxSleep := int64(9999999999999999) // indefinitely

	nArgs := len(call.ArgumentList)

	if nArgs == 0 {
		throwJSExeception("usage: sleepBlocks(<n blocks>[, max sleep in seconds])")
	}

	if nArgs >= 1 {
		if call.Argument(0).IsNumber() {
			nBlocks, _ = call.Argument(0).ToInteger()
		} else {
			throwJSExeception("expected number as first argument")
		}
	}

	if nArgs >= 2 {
		if call.Argument(1).IsNumber() {
			maxSleep, _ = call.Argument(1).ToInteger()
		} else {
			throwJSExeception("expected number as second argument")
		}
	}

	// go through the console, this will allow web3 to call the appropriate
	// callbacks if a delayed response or notification is received.
	currentBlockNr := func() int64 {
		result, err := call.Otto.Run("eth.blockNumber")
		if err != nil {
			throwJSExeception(err.Error())
		}
		blockNr, err := result.ToInteger()
		if err != nil {
			throwJSExeception(err.Error())
		}
		return blockNr
	}

	targetBlockNr := currentBlockNr() + nBlocks
	deadline := time.Now().Add(time.Duration(maxSleep) * time.Second)

	for time.Now().Before(deadline) {
		if currentBlockNr() >= targetBlockNr {
			return otto.TrueValue()
		}
		time.Sleep(time.Second)
	}

	return otto.FalseValue()
}
Пример #3
0
// loadScript executes a JS script from inside the currently executing JS code.
func (self *JSRE) loadScript(call otto.FunctionCall) otto.Value {
	file, err := call.Argument(0).ToString()
	if err != nil {
		// TODO: throw exception
		return otto.FalseValue()
	}
	file = common.AbsolutePath(self.assetPath, file)
	source, err := ioutil.ReadFile(file)
	if err != nil {
		// TODO: throw exception
		return otto.FalseValue()
	}
	if _, err := compileAndRun(call.Otto, file, source); err != nil {
		// TODO: throw exception
		fmt.Println("err:", err)
		return otto.FalseValue()
	}
	// TODO: return evaluation result
	return otto.TrueValue()
}