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
  • Prerequisites
  • Why use a library?
  • Library features
  • Connect to Electroneum Smart Chain nodes
  • Wallet functionality
  • Interact with smart contract functions
  • Utility functions
  • Available libraries
  • Web3.js - Ethereum JavaScript API.
  • Ethers.js - Complete Ethereum wallet implementation and utilities in JavaScript and TypeScript.
  • The Graph - A protocol for indexing Ethereum and IPFS data and querying it using GraphQL.
  • light.js - A high-level reactive JS library optimized for light clients.
  • Web3-wrapper - Typescript alternative to Web3.js.
  • Alchemyweb3 - Wrapper around Web3.js with automatic retries and enhanced apis.
  • Alchemy NFT API - API for fetching NFT data, including ownership, metadata attributes and more.
  • viem - TypeScript Interface for Ethereum.
  • Related topics

Was this helpful?

  1. Electroneum Stack
  2. Electroneum client APIs

JavaScript APIs

PreviousElectroneum client APIsNextJSON-RPC

Last updated 1 year ago

Was this helpful?

In order for a web app to interact with the Electroneum Smart Chain (i.e. read blockchain data and/or send transactions to the network), it must connect to an Electroneum node.

For this purpose, every Electroneum Smart Chain client implements the specification, so there are a uniform set of that applications can rely on.

If you want to use JavaScript to connect with an Electroneum node, it's possible to use vanilla JavaScript but several convenience libraries exist within the ecosystem that make this much easier. With these libraries, developers can write intuitive, one-line methods to initialise JSON RPC requests (under the hood) that interact with the Electroneum Smart Chain.

Prerequisites

As well as understanding JavaScript, it might be helpful to understand the and .

Why use a library?

These libraries abstract away much of the complexity of interacting directly with an Electroneum Smart Chain node. They also provide utility functions (e.g. converting ETN to Gwei) so as a developer you can spend less time dealing with the intricacies of Electroneum Smart Chain clients and more time focused on the unique functionality of your application.

Library features

Connect to Electroneum Smart Chain nodes

Using providers, these libraries allow you to connect to the Electroneum Smart Chain and read its data, whether that's over JSON-RPC or MetaMask.

Ethers example

// A Web3Provider wraps a standard Web3 provider, which is
// what MetaMask injects as window.ethereum into each page
const provider = new ethers.providers.Web3Provider(window.ethereum)

// The MetaMask plugin also allows signing transactions to
// send ether and pay to change state within the blockchain.
// For this, we need the account signer...
const signer = provider.getSigner()

Web3js example

var web3 = new Web3("http://localhost:8545")
// or
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))

// change provider
web3.setProvider("ws://localhost:8546")
// or
web3.setProvider(new Web3.providers.WebsocketProvider("ws://localhost:8546"))

// Using the IPC provider in node.js
var net = require("net")
var web3 = new Web3("/Users/myuser/Library/Electroneum-sc/etn-sc.ipc", net) // mac os path
// or
var web3 = new Web3(
  new Web3.providers.IpcProvider("/Users/myuser/Library/Electroneum-sc/etn-sc.ipc", net)
) // mac os path
// on windows the path is: "\\\\.\\pipe\\etn-sc.ipc"
// on linux the path is: "/users/myuser/.electroneum-sc/etn-sc.ipc"

Once set up you'll be able to query the blockchain for:

  • block numbers

  • gas estimates

  • smart contract events

  • network id

  • and more...

Wallet functionality

These libraries give you functionality to create wallets, manage keys and sign transactions.

Here's an example from Ethers

// Create a wallet instance from a mnemonic...
mnemonic =
  "announce room limb pattern dry unit scale effort smooth jazz weasel alcohol"
walletMnemonic = Wallet.fromMnemonic(mnemonic)

// ...or from a private key
walletPrivateKey = new Wallet(walletMnemonic.privateKey)

walletMnemonic.address === walletPrivateKey.address
// true

// The address as a Promise per the Signer API
walletMnemonic.getAddress()
// { Promise: '0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1' }

// A Wallet address is also available synchronously
walletMnemonic.address
// '0x71CB05EE1b1F506fF321Da3dac38f25c0c9ce6E1'

// The internal cryptographic components
walletMnemonic.privateKey
// '0x1da6847600b0ee25e9ad9a52abbd786dd2502fa4005dd5af9310b7cc7a3b25db'
walletMnemonic.publicKey
// '0x04b9e72dfd423bcf95b3801ac93f4392be5ff22143f9980eb78b3a860c4843bfd04829ae61cdba4b3b1978ac5fc64f5cc2f4350e35a108a9c9a92a81200a60cd64'

// The wallet mnemonic
walletMnemonic.mnemonic
// {
//   locale: 'en',
//   path: 'm/44\'/60\'/0\'/0/0',
//   phrase: 'announce room limb pattern dry unit scale effort smooth jazz weasel alcohol'
// }

// Note: A wallet created with a private key does not
//       have a mnemonic (the derivation prevents it)
walletPrivateKey.mnemonic
// null

// Signing a message
walletMnemonic.signMessage("Hello World")
// { Promise: '0x14280e5885a19f60e536de50097e96e3738c7acae4e9e62d67272d794b8127d31c03d9cd59781d4ee31fb4e1b893bd9b020ec67dfa65cfb51e2bdadbb1de26d91c' }

tx = {
  to: "0x8ba1f109551bD432803012645Ac136ddd64DBA72",
  value: utils.parseEther("1.0"),
}

// Signing a transaction
walletMnemonic.signTransaction(tx)
// { Promise: '0xf865808080948ba1f109551bd432803012645ac136ddd64dba72880de0b6b3a7640000801ca0918e294306d177ab7bd664f5e141436563854ebe0a3e523b9690b4922bbb52b8a01181612cec9c431c4257a79b8c9f0c980a2c49bb5a0e6ac52949163eeb565dfc' }

// The connect method returns a new instance of the
// Wallet connected to a provider
wallet = walletMnemonic.connect(provider)

// Querying the network
wallet.getBalance()
// { Promise: { BigNumber: "42" } }
wallet.getTransactionCount()
// { Promise: 0 }

// Sending etn
wallet.sendTransaction(tx)

Once set up you'll be able to:

  • create accounts

  • send transactions

  • sign transactions

  • and more...

Interact with smart contract functions

JavaScript client libraries allow your application to call smart contract functions by reading the Application Binary Interface (ABI) of a compiled contract.

The ABI essentially explains the contract's functions in a JSON format and allows you to use it like a normal JavaScript object.

So the following Solidity contract:

contract Test {
    uint a;
    address d = 0x12345678901234567890123456789012;

    function Test(uint testInt)  { a = testInt;}

    event Event(uint indexed b, bytes32 c);

    event Event2(uint indexed b, bytes32 c);

    function foo(uint b, bytes32 c) returns(address) {
        Event(b, c);
        return d;
    }
}

Would result in the following JSON:

[{
    "type":"constructor",
    "payable":false,
    "stateMutability":"nonpayable"
    "inputs":[{"name":"testInt","type":"uint256"}],
  },{
    "type":"function",
    "name":"foo",
    "constant":false,
    "payable":false,
    "stateMutability":"nonpayable",
    "inputs":[{"name":"b","type":"uint256"}, {"name":"c","type":"bytes32"}],
    "outputs":[{"name":"","type":"address"}]
  },{
    "type":"event",
    "name":"Event",
    "inputs":[{"indexed":true,"name":"b","type":"uint256"}, {"indexed":false,"name":"c","type":"bytes32"}],
    "anonymous":false
  },{
    "type":"event",
    "name":"Event2",
    "inputs":[{"indexed":true,"name":"b","type":"uint256"},{"indexed":false,"name":"c","type":"bytes32"}],
    "anonymous":false
}]

This means you can:

  • Send a transaction to the smart contract and execute its method

  • Call to estimate the gas a method execution will take when executed in the EVM

  • Deploy a contract

  • And more...

Utility functions

Utility functions give you handy shortcuts that make building with the Electroneum Smart Chain a little easier.

ETN values are in Wei by default. 1 ETN = 1,000,000,000,000,000,000 WEI – this means you're dealing with a lot of numbers! web3.utils.toWei converts ETN to Wei for you.

And in ethers it looks like this:

// Get the balance of an account (by address or ENS name)
balance = await provider.getBalance("ethers.eth")
// { BigNumber: "2337132817842795605" }

// Often you will need to format the output for the user
// which prefer to see values in ether (instead of wei)
ethers.utils.formatEther(balance)
// '2.337132817842795605'

Available libraries

Web3.js - Ethereum JavaScript API.

Ethers.js - Complete Ethereum wallet implementation and utilities in JavaScript and TypeScript.

The Graph - A protocol for indexing Ethereum and IPFS data and querying it using GraphQL.

light.js - A high-level reactive JS library optimized for light clients.

Web3-wrapper - Typescript alternative to Web3.js.

Alchemyweb3 - Wrapper around Web3.js with automatic retries and enhanced apis.

Alchemy NFT API - API for fetching NFT data, including ownership, metadata attributes and more.

viem - TypeScript Interface for Ethereum.

Related topics

JSON-RPC
methods
Electroneum stack
Electroneum Smart Chain clients
Read the full docs↗
Web3js utility functions↗
Ethers utility functions↗
Documentation↗
GitHub↗
Documentation↗
GitHub↗
The Graph↗
Graph Explorer↗
Documentation↗
GitHub↗
Discord↗
GitHub↗
Documentation↗
GitHub↗
Documentation↗
GitHub↗
Documentation↗
GitHub↗
Documentation↗
GitHub↗
Nodes and clients
Development frameworks