JavaScript Console

The Geth JavaScript console exposes the full web3 JavaScript Dapp API and further administrative APIs.

Geth JavaScript控制台公开了完整的web3 JavaScript Dapp API 和其他管理API。

Interactive Use: The Console 交互使用:控制台

The geth JavaScript console is started with the console or attach geth sub-commands. The console subcommands starts the geth node and then opens the console. The attach subcommand attaches to the console to an already-running geth instance.

geth console
geth attach

Attach mode accepts an endpoint in case the geth node is running with a non default ipc endpoint or you would like to connect over the rpc interface.

如果geth节点与非默认ipc终结点一起运行,或者您想通过rpc接口进行连接,则Attach模式接受一个终结点。

geth attach /some/custom/path.ipc
geth attach http://191.168.1.1:8545
geth attach ws://191.168.1.1:8546

Note that by default the geth node doesn’t start the HTTP and WebSocket servers and not all functionality is provided over these interfaces for security reasons. These defaults can be overridden with the --rpcapi and --wsapi arguments when the geth node is started, or with admin.startRPC and admin.startWS.

请注意,默认情况下,geth节点不会启动HTTP和WebSocket服务器,并且出于安全原因,并非所有功能都通过这些接口提供。启动geth节点时,可以使用–rpcapi和–wsapi参数或 admin.startRPCadmin.startWS覆盖这些默认值。

If you need log information, start with:

如果您需要日志信息,请开始于:

geth console --verbosity 5 2>> /tmp/eth.log

Otherwise mute your logs, so that it does not pollute your console:

否则,将日志静音,以免污染控制台:

geth console 2> /dev/null

Geth has support to load custom JavaScript files into the console through the --preload option. This can be used to load often used functions, or to setup web3 contract objects.

Geth支持通过–preload选项将自定义JavaScript文件加载到控制台中。这可用于加载常用功能或设置web3合同对象。

geth console --preload "/my/scripts/folder/utils.js,/my/scripts/folder/contracts.js"

Non-interactive Use: Script Mode

It’s also possible to execute files to the JavaScript interpreter. The console and attach subcommand accept the --execargument which is a javascript statement.

geth attach --exec "eth.blockNumber"

This prints the current block number of a running geth instance.

Or execute a local script with more complex statements on a remote node over http:

geth attach http://geth.example.org:8545 --exec 'loadScript("/tmp/checkbalances.js")'
geth attach http://geth.example.org:8545 --jspath "/tmp" --exec 'loadScript("checkbalances.js")'

Use the --jspath <path/to/my/js/root> to set a library directory for your js scripts. Parameters to loadScript() with no absolute path will be understood relative to this directory.

You can exit the console by typing exit or simply with CTRL-C.

Caveats

go-ethereum now uses the GoJa JS VM which is compatible with ECMAScript 5.1. There are some limitations though:

  • Promises and async won’t work.

web3.js uses the bignumber.js library. This library is auto-loaded into the console.

Timers

In addition to the full functionality of JS (as per ECMA5), the ethereum JSRE is augmented with various timers. It implements setIntervalclearIntervalsetTimeoutclearTimeout you may be used to using in browser windows. It also provides implementation for admin.sleep(seconds) and a block based timer, admin.sleepBlocks(n) which sleeps till the number of new blocks added is equal to or greater than n, think “wait for n confirmations”.