JavaScript APIs
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 JSON-RPC specification, so there are a uniform set of methods 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 Electroneum stack and Electroneum Smart Chain clients.
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
Web3js example
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
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:
Would result in the following JSON:
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:
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
Last updated