go-ethereum/docs/_interface/JavaScript-Console.md

82 lines
3.2 KiB
Markdown
Raw Normal View History

2019-03-27 03:32:29 -05:00
---
title: JavaScript Console
sort_key: B
2019-03-27 03:32:29 -05:00
---
The Geth JavaScript console exposes the full [web3 JavaScript Dapp
API](https://github.com/ethereum/wiki/wiki/JavaScript-API) and further administrative
APIs.
2019-03-27 03:32:29 -05:00
## Interactive Use: The Console
2019-03-27 03:32:29 -05:00
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.
2019-03-27 03:32:29 -05:00
geth console
geth attach
2019-03-27 03:32:29 -05:00
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.
2019-03-27 03:32:29 -05:00
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
2020-05-04 02:37:58 -05:00
can be overridden with the `--rpcapi` and `--wsapi` arguments when the geth node is
started, or with [admin.startRPC](../rpc/ns-admin#admin_startrpc) and
[admin.startWS](../rpc/ns-admin#admin_startws).
2019-03-27 03:32:29 -05:00
If you need log information, start with:
geth console --verbosity 5 2>> /tmp/eth.log
2019-03-27 03:32:29 -05:00
Otherwise mute your logs, so that it does not pollute your console:
geth console 2> /dev/null
2019-03-27 03:32:29 -05:00
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.
2019-03-27 03:32:29 -05:00
geth console --preload "/my/scripts/folder/utils.js,/my/scripts/folder/contracts.js"
2019-03-27 03:32:29 -05:00
## Non-interactive Use: Script Mode
2019-03-27 03:32:29 -05:00
It's also possible to execute files to the JavaScript interpreter. The `console` and
`attach` subcommand accept the `--exec` argument which is a javascript statement.
2019-03-27 03:32:29 -05:00
geth attach --exec "eth.blockNumber"
2019-03-27 03:32:29 -05:00
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")'
2019-03-27 03:32:29 -05:00
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.
2019-03-27 03:32:29 -05:00
You can exit the console by typing `exit` or simply with `CTRL-C`.
2019-03-27 03:32:29 -05:00
## Caveats
2019-03-27 03:32:29 -05:00
go-ethereum now uses the [GoJa JS VM](https://github.com/dop251/goja) which is compatible with ECMAScript 5.1. There are some limitations though:
2019-03-27 03:32:29 -05:00
* Promises and `async` won't work.
2019-03-27 03:32:29 -05:00
`web3.js` uses the [`bignumber.js`](https://github.com/MikeMcl/bignumber.js) library.
This library is auto-loaded into the console.
2019-03-27 03:32:29 -05:00
### Timers
2019-03-27 03:32:29 -05:00
In addition to the full functionality of JS (as per ECMA5), the ethereum JSRE is augmented
with various timers. It implements `setInterval`, `clearInterval`, `setTimeout`,
`clearTimeout` 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".