-
Notifications
You must be signed in to change notification settings - Fork 20.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: redraft JS console page #24974
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
65c356c
initial commit for new console page
jmcook1186 4e77ffa
finish js-console redraft
jmcook1186 e3939e1
minor improvements
s1na 881fbe4
add line about `js` command
jmcook1186 11840c5
remove whitespace from, code blocks
jmcook1186 b513d88
Revert "add line about `js` command"
s1na 38be0d6
minor
s1na File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,173 @@ | ||
--- | ||
title: JavaScript Console | ||
sort_key: B | ||
sort_key: D | ||
--- | ||
|
||
The Geth JavaScript console exposes the full [web3 JavaScript Dapp | ||
API](https://github.com/ethereum/wiki/wiki/JavaScript-API) and further administrative | ||
APIs. | ||
Geth responds to instructions encoded as JSON objects as defined in the [JSON-RPC-API](/docs/rpc/server). A Geth user can send these instructions directly, for example over HTTP using tools like [Curl](https://github.com/curl/curl). The code snippet below shows a request for an account balance sent to a local Geth node with the HTTP port `8545` exposed. | ||
|
||
(note: the web3 version bundled within geth is very old, and not up to date with official docs) | ||
``` | ||
curl --data '{"jsonrpc":"2.0","method":"eth_getBalance", "params": ["0x9b1d35635cc34752ca54713bb99d38614f63c955", "latest"], "id":2}' -H "Content-Type: application/json" localhost:8545 | ||
|
||
## 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. | ||
This returns a result which is also a JSON object, with values expressed as hexadecimal strings, for example: | ||
|
||
geth console | ||
geth attach | ||
```terminal | ||
|
||
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. | ||
{"id":2,"jsonrpc":"2.0","result":"0x1639e49bba16280000"} | ||
|
||
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 `--http.api` and `--ws.api` arguments when the geth node is | ||
started, or with [admin.startRPC](../rpc/ns-admin#admin_startrpc) and | ||
[admin.startWS](../rpc/ns-admin#admin_startws). | ||
While this approach is valid, it is also a very low level and rather error-prone way to interact with Geth. Most developers prefer to use convenience libraries that abstract away some of the more tedious and awkward tasks such as converting values from hexadecimal strings into numbers, or converting between denominations of ether (Wei, Gwei, etc). One such library is [Web3.js](https://web3js.readthedocs.io/en/v1.7.3/). This is a collection of Javascript libraries for interacting with an Ethereum node at a higher level than sending raw JSON objects to the node. The purpose of Geth's Javascript console is to provide a built-in environment to use a subset of the Web3.js libraries to interact with a Geth node. | ||
|
||
If you need log information, start with: | ||
{% include note.html content="The web3.js version that comes bundled with Geth is not up to date with the official Web3.js documentation. There are several Web3.js libraries that are not available in the Geth Javascript Console. There are also administrative APIs included in the Geth console that are not documented in the Web3.jc documentation. The full list of libraries available in the Geth console is available on the [JSON-RPC API page](/docs/_rpc/server)" %} | ||
|
||
geth console --verbosity 5 2>> /tmp/eth.log | ||
|
||
Otherwise mute your logs, so that it does not pollute your console: | ||
## Starting the console | ||
|
||
geth console 2> /dev/null | ||
There are two ways to start an interactive session using Geth console. The first is to provide the `console` flag when Geth is started up. This starts the node and runs the console in the same terminal. It is therefore convenient to suppress the logs from the node to prevent them from obscuring the console. If the logs are not needed, they can be redirected to the `dev/null` path, effectively muting them. Alternatively, if the logs are required they can be redirected to a text file. The level of detail provided in the logs can be adjusted by providing a value between 1-6 to the `--verbosity` flag as in the example below: | ||
|
||
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. | ||
```shell | ||
# to mute logs | ||
geth <other commands> console 2 > /dev/null | ||
|
||
geth console --preload "/my/scripts/folder/utils.js,/my/scripts/folder/contracts.js" | ||
# to save logs to file | ||
geth <other commands> --verbosity 3 2 > geth-logs.log | ||
|
||
``` | ||
|
||
Alternatively, a Javascript console can be attached to an existing Geth instance (i.e. one that is running in another terminal or remotely). In this case, `geth attach` can be used to open a Javascript console connected to the Geth node. It is also necessary to define the method used to connect the console to the node. Geth supports websockets, HTTP or local IPC. To use HTTP or Websockets, these must be enabled at the node by providing the following flags at startup: | ||
|
||
```shell | ||
|
||
# enable websockets | ||
geth <other commands> --ws | ||
|
||
# enable http | ||
|
||
geth <other commands> --http | ||
|
||
``` | ||
|
||
The commands above use default HTTP/WS endpoints and only enables the default JSON-RPC libraries. To update the Websockets or HTTP endpoints used, or to add support for additional libraries, the `.addr` `.port` and `.api` flags can be used as follows: | ||
|
||
```shell | ||
|
||
# define a custom http adress, custom http port and enable libraries | ||
geth <other commands> --http --http.addr 192.60.52.21 --http.port 8552 --http.api eth,web3,admin | ||
|
||
# define a custom Websockets address and enable libraries | ||
geth <other commands> --ws --ws.addr 192.60.52.21 --ws.port 8552 --ws.api eth,web3,admin | ||
|
||
``` | ||
|
||
It is important to note that by default **some functionality, including account unlocking is forbidden when HTTP or Websockets access is enabled**. This is because an attacker that manages to access the node via the externally-exposed HTTP/WS port then control the unlocked account. It is possible to force account unlock by including the `--allow-insecure-unlock` flag but this is not recommended if there is any chance of the node connecting to Ethereum Mainnet. This is not a hypothetical risk: **there are bots that continually scan for http-enabled Ethereum nodes to attack**" | ||
|
||
The Javascript console can also be connected to a Geth node using IPC. When Geth is started, a `geth.ipc` file is automatically generated and saved to the data directory. This file, or a custom path to a specific ipc file can be passed to `geth attach` as follows: | ||
|
||
```shell | ||
|
||
geth attach datadir/geth.ipc | ||
|
||
``` | ||
|
||
Once started, the console looks like this: | ||
|
||
```terminal | ||
|
||
Welcome to the Geth Javascript console! | ||
|
||
instance: Geth/v1.10.18-unstable-8d85a701-20220503/linux-amd64/go1.18.1 | ||
coinbase: 0x281aabb85c68e1638bb092750a0d9bb06ba103ee | ||
at block: 12305815 (Thu May 26 2022 16:16:00 GMT+0100 (BST)) | ||
datadir: /home/go-ethereum/data | ||
modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 | ||
|
||
To exit, press ctrl-d or type exit | ||
> | ||
|
||
``` | ||
|
||
|
||
## Interactive use | ||
|
||
Once the console has been started, it can be used to interact with Geth. The console supports Javascript and the full Geth [JSON-RPC API](/docs/rpc/server). For example, to create an account: | ||
|
||
```js | ||
|
||
personal.newAccount() | ||
|
||
``` | ||
|
||
To check the balance of the first account already existing in the keystore: | ||
|
||
```js | ||
|
||
eth.getBalance(personal.listAccounts[0]) | ||
|
||
``` | ||
|
||
|
||
To make a transaction (without global account unlocking): | ||
|
||
```js | ||
|
||
personal.sendTransaction({to: eth.accounts[0], to: eth.accounts[1], value: web3.toWei(0.5, "ether")}) | ||
|
||
``` | ||
|
||
It is also possible to load pre-written Javascript files into the consoleby passing the `--preload` flag | ||
when starting the console. This is useful for setting up complex contract objects or loading frequently-used | ||
functions. | ||
|
||
|
||
```shell | ||
|
||
geth console --preload "/my/scripts/folder/utils.js" | ||
|
||
``` | ||
|
||
## Non-interactive Use: Script Mode | ||
|
||
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. | ||
It is also possible to execute files to the JavaScript interpreter non-interactively by passing the `--exec` and a JSON-RPC-API endpoint | ||
to `geth attach` or `geth console`. The result is displayed directly in the terminal rather than in an interactive Javascript console. | ||
|
||
geth attach --exec "eth.blockNumber" | ||
For example, to display the accounts in the keystore: | ||
|
||
This prints the current block number of a running geth instance. | ||
```shell | ||
|
||
Or execute a local script with more complex statements on a remote node over http: | ||
geth attach --exec eth.accounts | ||
|
||
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`. | ||
```shell | ||
|
||
## Caveats | ||
geth attach --exec eth.blockNumber | ||
|
||
``` | ||
|
||
The same syntax can be used to execute a local script with more complex statements on a remote node over http, for example: | ||
|
||
```shell | ||
|
||
geth attach http://geth.example.org:8545 --exec 'loadScript("/tmp/checkbalances.js")' | ||
|
||
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: | ||
geth attach http://geth.example.org:8545 --jspath "/tmp" --exec 'loadScript("checkbalances.js")' | ||
|
||
* Promises and `async` won't work. | ||
``` | ||
|
||
`web3.js` uses the [`bignumber.js`](https://github.com/MikeMcl/bignumber.js) library. | ||
This library is auto-loaded into the console. | ||
The `--jspath` flag is used to set a library directory for the Javascript scripts. Any parameters passed to `loadScript()` | ||
that do not explicitly define an absolute path will be interpreted relative to the `jspath` directory. | ||
|
||
### Timers | ||
Once the interactive session is over, the console can be closed down by typing `exit` or `CTRL-D`. | ||
|
||
|
||
## Timers | ||
|
||
In addition to the full functionality of JS (as per ECMA5), the Ethereum Javascript Runtime Environment (JSRE) is augmented with various timers. It implements `setInterval`, `clearInterval`, `setTimeout`, `clearTimeout` which some users will be familiar with from 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`. | ||
|
||
|
||
## Caveats | ||
|
||
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". | ||
Geth's console is built using the [GoJa JS Virtual Machine](https://github.com/dop251/goja) which is compatible with ECMAScript 5.1. This does not support promises or `async` functions. Web3js depends upon the `bignumber.js` library. This is auto-loaded into the console. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there no alternative way to includes notes like these? I would prefer not to mix markup with text, as much as possible.
I think I'd prefer just having the note as plain text rather than in such a tag. But that's just my 5c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only way I found to add an eye-catching note in jekyll. I can look to see if there are alternative ways of doing it but don't think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
afaik the only pure markdown options available are backticks, bold/italic text or quote blocks. Only the quote block is really viable in my opinion but none are really sufficiently eye-catching.