Skip to content

Console

mdranger edited this page Jun 23, 2018 · 4 revisions

MOAC developed based on the Ethereum project. It also implements a javascript runtime environment (JSRE) that can be used in either interactive (console) or non-interactive (script) mode.

MOAC Javascript console exposes the chain3 JavaScript Dapp API and the admin API.

Interactive use: the JSRE REPL Console

The MOAC go client has a JavaScript console (a Read, Evaluate & Print Loop = REPL exposing the JSRE), which can be started with the console or attach subcommand. The console subcommands starts the MOAC node and then opens the console. The attach subcommand will not start a new MOAC node but instead tries to open the console on a running MOAC instance.

$ moac console
$ moac attach

Default attach method only connect to a default ipc endpoint. If you are running a non default ipc endpoint or connect over the rpc interface, you should run with the arguments:

$ moac attach $HOME/.moac/testnet/moac.ipc
$ moac attach http://127.0.0.1:8545
$ moac attach ws://127.0.0.1:8545

Note that by default the MOAC node doesn't start the http and weboscket service and not all functionality is provided over these interfaces due to security reasons. These defaults can be overridden when the --rpcapi and --wsapi arguments when the MOAC node is started, or with admin.startRPC and admin.startWS.

If you need log information, start with 'verbosity' argument:

$ moac --verbosity 5 console 2>> /tmp/console.log

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

$ moac console 2>> /dev/null

or

$ moac --verbosity 0 console

MOAC has support to load custom JavaScript files into the console through the --preload argument. This can be used to load often used functions, setup chain3 contract objects:

$moac --preload "./scripts/utils.js,./scripts/contracts.js" console

Non-interactive use: JSRE 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.

$ moac --exec "mc.blockNumber" attach

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

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

$ moac --exec 'loadScript("./scripts/checkbalances.js")' attach http://127.0.0.1:8545
$ moac --jspath "./scripts" --exec 'loadScript("checkbalances.js")' attach http://127.0.0.1:8545

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

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

Caveat

The MOAC go client JSRE uses the Otto JS VM which has some limitations:

"use strict" will parse, but does nothing. The regular expression engine (re2/regexp) is not fully compatible with the ECMA5 specification. Note that the other known limitation of Otto (namely the lack of timers) is taken care of. Like Ethereum, MOAC JSRE implements both setTimeout and setInterval. In addition to this, the console provides admin.sleep(seconds) and a "blocktime sleep" method admin.sleepBlocks(number).

Since chain3.js uses the bignumber.js library (MIT Expat Licence), it is also autoloded.

Timers

In addition to the full functionality of JS (as per ECMA5), the MOAC 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".

Management APIs

Beside the official DApp API interface the MOAC go package has support for additional management API's. These API's are offered using JSON-RPC and follow the same conventions as used in the DApp API. The MOAC go package comes with a console client which has support for all additional API's.

Clone this wiki locally