Developer Resources
Block ExplorerGitHubRun a NodeMigration GuideETN Testnet Faucet
  • Overview
  • Foundational topics
    • Intro to the Electroneum Smart Chain
    • Intro to ETN
    • Intro to dapps
    • Web2 vs Web3
    • Accounts
    • Transactions
    • Blocks
    • Electroneum Virtual Machine (EVM)
      • Opcodes
    • Gas and Fees
    • Nodes and clients
    • Networks
    • Consensus mechanisms
      • IBFT
  • Electroneum Stack
    • Intro to the stack
    • Smart contracts
    • Development networks
    • Development frameworks
    • Electroneum client APIs
      • JavaScript APIs
      • JSON-RPC
    • Storage
    • Integrated Development Environments (IDEs)
    • Metamask
  • Advanced
    • Bridges
    • Standards
      • Token standards
        • ERC-20 Fungible Tokens
        • ERC-721 NFTs
    • Oracles
    • Networking layer
      • Network addresses
    • Data structures and encoding
      • Patricia Merkle Trie
      • Recursive-length prefix (RLP)
      • Web3 secret storage definition
  • Design fundamentals
    • Intro to design and UX
  • ETN-SC Client
    • Getting started
      • Introduction
      • Hardware requirements
      • Instaling ETN-SC
    • Fundamentals
      • Command-line options
      • Security
      • Sync-modes
      • Account management
      • Databases
      • Backup & restore
      • Logs
      • Connecting to peers
      • Pruning
      • Private networks
      • Config files
      • Light client
    • Interacting with ETN-SC
      • JSON-RPC Server
        • Batch requests
        • Real-time events
      • JSON-RPC Namespaces
        • admin
        • clique
        • debug
        • eth
        • istanbul
        • les
        • miner
        • net
        • personal
        • txpool
      • JS Console
      • JS Console 2: Contracts
      • GraphQL Server
    • Developers
      • Introduction
      • Dapp developers
        • Dev mode
        • Go API
        • Go Account Management
        • Go Contract Bindings
      • EVM tracing
        • Introduction
        • Basic traces
        • Built-in tracers
        • Custom EVM tracer
        • Tutorial for JavaScript tracing
      • ETN-SC developer
        • Developer guide
        • Disclosures
        • DNS discovery setup guide
        • Code review guidelines
      • Contributing
    • Monitoring
      • Creating Dashboards
      • Understanding Dashboards
      • Ethstats
      • Metrics
    • Tools
      • Clef
        • Introduction
        • APIs
        • Rules
        • Setup
        • Datatypes
        • Tutorial
        • Clique-signing
      • abigen
      • devp2p
    • FAQs
  • Migration to Smart Chain
    • Overview
    • How to Migrate
      • ETN Online Wallets
      • Paper Wallets
      • CLI Wallet Users
      • Exchange Holders
      • Exchanges
    • Bridge Smart Contract
  • Misc. Guides
    • Set up Ledger + Metamask
  • MY.ELECTRONEUM.COM
    • Vendor API
Powered by GitBook
On this page
  • Starting the console
  • Interactive use
  • Non-interactive Use: Script Mode
  • Timers
  • Caveats

Was this helpful?

  1. ETN-SC Client
  2. Interacting with ETN-SC

JS Console

PrevioustxpoolNextJS Console 2: Contracts

Last updated 1 year ago

Was this helpful?

Etn-sc responds to instructions encoded as JSON objects as defined in the . An Etn-sc user can send these instructions directly, for example over HTTP using tools like . The code snippet below shows a request for an account balance sent to a local Etn-sc node with the HTTP port 8545 exposed.

curl --data '{"jsonrpc":"2.0","method":"eth_getBalance", "params": ["0x9b1d35635cc34752ca54713bb99d38614f63c955", "latest"], "id":2}' -H "Content-Type: application/json" localhost:8545

This returns a result which is also a JSON object, with values expressed as hexadecimal strings, for example:

{"id":2,"jsonrpc":"2.0","result":"0x1639e49bba16280000"}

This is a low level and rather error-prone way to interact with Etn-sc. 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 ETN (Wei, Gwei, etc). One such library is . The purpose of Etn-sc'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.

The web3.js version that comes bundled with Etn-sc is not up to date with the official Web3.js documentation. There are several Web3.js libraries that are not available in the Etn-sc Javascript Console. There are also administrative APIs included in the Etn-sc console that are not documented in the Web3.js documentation. The full list of libraries available in the Geth console is available on the .

Starting the console

There are two ways to start an interactive session using Etn-sc console. The first is to provide the console command when Etn-sc 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:

# to mute logs
etn-sc <other flags> console 2> /dev/null

# to save logs to file
etn-sc <other flags> console --verbosity 3 2> etn-sc-logs.log

Alternatively, a Javascript console can be attached to an existing Etn-sc instance (i.e. one that is running in another terminal or remotely). In this case, etn-sc attach can be used to open a Javascript console connected to the Etn-sc node. It is also necessary to define the method used to connect the console to the node. Etn-sc 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:

# enable websockets
etn-sc <other flags> --ws

# enable http
etn-sc <other flags> --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:

# define a custom http adress, custom http port and enable libraries
etn-sc <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
etn-sc <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. This is not a hypothetical risk: there are bots that continually scan for http-enabled Electroneum nodes to attack"

The Javascript console can also be connected to a Etn-sc node using IPC. When Etn-sc is started, a etn-sc.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 etn-sc attach as follows:

etn-sc attach datadir/etn-sc.ipc

Once started, the console looks like this:

Welcome to the ETN-SC JavaScript console!

instance: etn-sc/v5.0.0-stable-95cf8a9f/darwin-arm64/go1.20.6
coinbase: 0xa71299ee4c91bf07d84a51620f54379de83a5ba5
at block: 2754218 (Wed Jul 26 2023 15:07:49 GMT-0300 (-03))
 datadir: /home/electroneum-sc/data
 modules: admin:1.0 debug:1.0 eth:1.0 istanbul: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

eth.getBalance(eth.accounts[0]);

To send a transaction (without global account unlocking):

eth.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 console by passing the --preload flag when starting the console. This is useful for setting up complex contract objects or loading frequently-used functions.

etn-sc console --preload "/my/scripts/folder/utils.js"

Once the interactive session is over, the console can be closed down by typing exit or CTRL-D.

Remember that interactions that touch accounts need approval in Clef - either manually or by writing a custom ruleset.

Non-interactive Use: Script Mode

It is also possible to execute JavaScript code non-interactively by passing the --exec and a JSON-RPC-API endpoint to etn-sc attach or etn-sc console. The result is displayed directly in the terminal rather than in an interactive Javascript console.

For example, to display the accounts in the keystore:

etn-sc attach --exec eth.accounts
etn-sc attach --exec eth.blockNumber

The same syntax can be used to execute a local script file with more complex statements on a remote node over http, for example:

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

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

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

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

Once the console has been started, it can be used to interact with Etn-sc. The console supports Javascript and the full Etn-sc . For example, to check the balance of the first account already existing in the keystore:

Etn-sc's console is built using the 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.

JSON-RPC-API
Curl
Web3.js
JSON-RPC API page
JSON-RPC API
GoJa JS Virtual Machine