Ethers.js
Introduction
Section titled “Introduction”Ethers.js is a lightweight library that enables interaction with Ethereum Virtual Machine (EVM)-compatible blockchains through JavaScript. Ethers is widely used as a toolkit to establish connections and read and write blockchain data. This article demonstrates using Ethers.js to interact and deploy smart contracts to Polkadot Hub.
This guide is intended for developers who are familiar with JavaScript and want to interact with Polkadot Hub using Ethers.js.
Prerequisites
Section titled “Prerequisites”Before getting started, ensure you have the following installed:
- Node.js: v22.13.1 or later, check the Node.js installation guide.
- npm: v6.13.4 or later (comes bundled with Node.js).
- Solidity: This guide uses Solidity
^0.8.9for smart contract development.
Project Structure
Section titled “Project Structure”This project organizes contracts, scripts, and compiled artifacts for easy development and deployment.
ethers-project├── contracts│ ├── Storage.sol├── scripts│ ├── connectToProvider.js│ ├── fetchLastBlock.js│ ├── compile.js│ ├── deploy.js│ ├── checkStorage.js├── abis│ ├── Storage.json├── artifacts│ ├── Storage.bin├── contract-address.json├── node_modules/├── package.json├── package-lock.json└── README.mdSet Up the Project
Section titled “Set Up the Project”To start working with Ethers.js, create a new folder and initialize your project by running the following commands in your terminal:
mkdir ethers-projectcd ethers-projectnpm init -yInstall Dependencies
Section titled “Install Dependencies”Next, run the following command to install the Ethers.js library:
npm install ethersAdd the Solidity compiler so you can generate standard EVM bytecode:
npm install --save-dev solc@0.8.38This guide uses solc version 0.8.38.
Set Up the Ethers.js Provider
Section titled “Set Up the Ethers.js Provider”A Provider is an abstraction of a connection to the Ethereum network, allowing you to query blockchain data and send transactions. It serves as a bridge between your application and the blockchain.
To interact with Polkadot Hub, you must set up an Ethers.js provider. This provider connects to a blockchain node, allowing you to query blockchain data and interact with smart contracts. In the root of your project, create a file named connectToProvider.js and add the following code:
const { JsonRpcProvider } = require('ethers');
const createProvider = (rpcUrl, chainId, chainName) => { const provider = new JsonRpcProvider(rpcUrl, { chainId: chainId, name: chainName, });
return provider;};
const PROVIDER_RPC = { rpc: 'INSERT_RPC_URL', chainId: 'INSERT_CHAIN_ID', name: 'INSERT_CHAIN_NAME',};
createProvider(PROVIDER_RPC.rpc, PROVIDER_RPC.chainId, PROVIDER_RPC.name);To connect to the provider, execute:
node scripts/connectToProvider.jsWith the provider set up, you can start querying the blockchain. For instance, to fetch the latest block number:
fetchLastBlock.js code
const { JsonRpcProvider } = require('ethers');
const createProvider = (rpcUrl, chainId, chainName) => { const provider = new JsonRpcProvider(rpcUrl, { chainId: chainId, name: chainName, });
return provider;};
const PROVIDER_RPC = { rpc: 'https://eth-rpc-testnet.polkadot.io', chainId: 420420417, name: 'polkadot-hub-testnet',};
const main = async () => { try { const provider = createProvider( PROVIDER_RPC.rpc, PROVIDER_RPC.chainId, PROVIDER_RPC.name, ); const latestBlock = await provider.getBlockNumber(); console.log(`Latest block: ${latestBlock}`); } catch (error) { console.error('Error connecting to Polkadot Hub TestNet: ' + error.message); }};
main();Compile Contracts
Section titled “Compile Contracts”Polkadot Hub exposes an Ethereum JSON-RPC endpoint, so you can compile Solidity contracts to familiar EVM bytecode with the upstream solc compiler. The resulting artifacts work with any EVM-compatible toolchain and can be deployed through Ethers.js.
Sample Storage Smart Contract
Section titled “Sample Storage Smart Contract”This example demonstrates compiling a Storage.sol Solidity contract for deployment to Polkadot Hub. The contract's functionality stores a number and permits users to update it with a new value.
//SPDX-License-Identifier: MIT
// Solidity files have to start with this pragma.// It will be used by the Solidity compiler to validate its version.pragma solidity ^0.8.9;
contract Storage { // Public state variable to store a number uint256 public storedNumber;
/** * Updates the stored number. * * The `public` modifier allows anyone to call this function. * * @param _newNumber - The new value to store. */ function setNumber(uint256 _newNumber) public { storedNumber = _newNumber; }}Compile the Smart Contract
Section titled “Compile the Smart Contract”To compile this contract, use the following script:
const solc = require('solc');const { readFileSync, writeFileSync, mkdirSync, existsSync } = require('fs');const { basename, join } = require('path');
const ensureDir = (dirPath) => { if (!existsSync(dirPath)) { mkdirSync(dirPath, { recursive: true }); }};
const compileContract = (solidityFilePath, abiDir, artifactsDir) => { try { // Read the Solidity file const source = readFileSync(solidityFilePath, 'utf8'); const fileName = basename(solidityFilePath);
// Construct the input object for the Solidity compiler const input = { language: 'Solidity', sources: { [fileName]: { content: source, }, }, settings: { outputSelection: { '*': { '*': ['abi', 'evm.bytecode'], }, }, }, };
console.log(`Compiling contract: ${fileName}...`);
// Compile the contract const output = JSON.parse(solc.compile(JSON.stringify(input)));
// Check for errors if (output.errors) { const errors = output.errors.filter(error => error.severity === 'error'); if (errors.length > 0) { console.error('Compilation errors:'); errors.forEach(err => console.error(err.formattedMessage)); return; } // Show warnings const warnings = output.errors.filter(error => error.severity === 'warning'); warnings.forEach(warn => console.warn(warn.formattedMessage)); }
// Ensure output directories exist ensureDir(abiDir); ensureDir(artifactsDir);
// Process compiled contracts for (const [sourceFile, contracts] of Object.entries(output.contracts)) { for (const [contractName, contract] of Object.entries(contracts)) { console.log(`Compiled contract: ${contractName}`);
// Write the ABI const abiPath = join(abiDir, `${contractName}.json`); writeFileSync(abiPath, JSON.stringify(contract.abi, null, 2)); console.log(`ABI saved to ${abiPath}`);
// Write the bytecode const bytecodePath = join(artifactsDir, `${contractName}.bin`); writeFileSync(bytecodePath, contract.evm.bytecode.object); console.log(`Bytecode saved to ${bytecodePath}`); } } } catch (error) { console.error('Error compiling contracts:', error); }};
const solidityFilePath = join(__dirname, '../contracts/Storage.sol');const abiDir = join(__dirname, '../abis');const artifactsDir = join(__dirname, '../artifacts');
compileContract(solidityFilePath, abiDir, artifactsDir);The ABI (Application Binary Interface) is a JSON representation of your contract's functions, events, and their parameters. It serves as the interface between your JavaScript code and the deployed smart contract, allowing your application to know how to format function calls and interpret returned data.
Execute the script above by running:
node scripts/compile.jsAfter executing the script, the Solidity contract is compiled into standard EVM bytecode. The ABI and bytecode are saved into files with .json and .bin extensions, respectively. You can now proceed with deploying the contract to Polkadot Hub, as outlined in the next section.
Deploy the Compiled Contract
Section titled “Deploy the Compiled Contract”To deploy your compiled contract to Polkadot Hub, you'll need a wallet with a private key to sign the deployment transaction.
You can create a deploy.js script in the root of your project to achieve this. The deployment script can be divided into key components:
-
Set up the required imports and utilities:
const { writeFileSync, existsSync, readFileSync } = require('fs');const { join } = require('path');const { ethers, JsonRpcProvider } = require('ethers'); -
Create a provider to connect to Polkadot Hub:
// Creates a provider with specified RPC URL and chain detailsconst createProvider = (rpcUrl, chainId, chainName) => {const provider = new JsonRpcProvider(rpcUrl, {chainId: chainId,name: chainName,});return provider;}; -
Set up functions to read contract artifacts:
// Reads and parses the ABI file for a given contractconst getAbi = (contractName) => {try {const abiPath = join(artifactsDir, `${contractName}.json`);return JSON.parse(readFileSync(abiPath, 'utf8'));} catch (error) {console.error(`Could not find ABI for contract ${contractName}:`,error.message,);throw error;}};// Reads the compiled bytecode for a given contractconst getByteCode = (contractName) => {try {const bytecodePath = join(artifactsDir, `${contractName}.bin`);const bytecode = readFileSync(bytecodePath, 'utf8').trim();// Add 0x prefix if not presentreturn bytecode.startsWith('0x') ? bytecode : `0x${bytecode}`;} catch (error) {console.error(`Could not find bytecode for contract ${contractName}:`,error.message,);throw error;}}; -
Create the main deployment function:
const deployContract = async (contractName, mnemonic, providerConfig) => {console.log(`Deploying ${contractName}...`);try {// Step 1: Set up provider and walletconst provider = createProvider(providerConfig.rpc,providerConfig.chainId,providerConfig.name,);const walletMnemonic = ethers.Wallet.fromPhrase(mnemonic);const wallet = walletMnemonic.connect(provider);// Step 2: Create and deploy the contractconst factory = new ethers.ContractFactory(getAbi(contractName),getByteCode(contractName),wallet,);const contract = await factory.deploy();await contract.waitForDeployment();// Step 3: Save deployment informationconst address = await contract.getAddress();console.log(`Contract ${contractName} deployed at: ${address}`);const addressesFile = join(scriptsDir, 'contract-address.json');const addresses = existsSync(addressesFile)? JSON.parse(readFileSync(addressesFile, 'utf8')): {};addresses[contractName] = address;writeFileSync(addressesFile, JSON.stringify(addresses, null, 2), 'utf8');} catch (error) {console.error(`Failed to deploy contract ${contractName}:`, error);}}; -
Configure and execute the deployment:
const providerConfig = {rpc: 'https://eth-rpc-testnet.polkadot.io',chainId: 420420417,name: 'polkadot-hub-testnet',};const mnemonic = 'INSERT_MNEMONIC';deployContract('Storage', mnemonic, providerConfig);
View complete script
const { writeFileSync, existsSync, readFileSync } = require('fs');const { join } = require('path');const { ethers, JsonRpcProvider } = require('ethers');
const scriptsDir = __dirname;const artifactsDir = join(__dirname, '../contracts');
// Creates a provider with specified RPC URL and chain detailsconst createProvider = (rpcUrl, chainId, chainName) => { const provider = new JsonRpcProvider(rpcUrl, { chainId: chainId, name: chainName, }); return provider;};
// Reads and parses the ABI file for a given contractconst getAbi = (contractName) => { try { const abiPath = join(artifactsDir, `${contractName}.json`); return JSON.parse(readFileSync(abiPath, 'utf8')); } catch (error) { console.error( `Could not find ABI for contract ${contractName}:`, error.message, ); throw error; }};
// Reads the compiled bytecode for a given contractconst getByteCode = (contractName) => { try { const bytecodePath = join(artifactsDir, `${contractName}.bin`); const bytecode = readFileSync(bytecodePath, 'utf8').trim(); // Add 0x prefix if not present return bytecode.startsWith('0x') ? bytecode : `0x${bytecode}`; } catch (error) { console.error( `Could not find bytecode for contract ${contractName}:`, error.message, ); throw error; }};
const deployContract = async (contractName, mnemonic, providerConfig) => { console.log(`Deploying ${contractName}...`); try { // Step 1: Set up provider and wallet const provider = createProvider( providerConfig.rpc, providerConfig.chainId, providerConfig.name, ); const walletMnemonic = ethers.Wallet.fromPhrase(mnemonic); const wallet = walletMnemonic.connect(provider);
// Step 2: Create and deploy the contract const factory = new ethers.ContractFactory( getAbi(contractName), getByteCode(contractName), wallet, ); const contract = await factory.deploy(); await contract.waitForDeployment();
// Step 3: Save deployment information const address = await contract.getAddress(); console.log(`Contract ${contractName} deployed at: ${address}`);
const addressesFile = join(scriptsDir, 'contract-address.json'); const addresses = existsSync(addressesFile) ? JSON.parse(readFileSync(addressesFile, 'utf8')) : {};
addresses[contractName] = address; writeFileSync(addressesFile, JSON.stringify(addresses, null, 2), 'utf8'); } catch (error) { console.error(`Failed to deploy contract ${contractName}:`, error); }};
const providerConfig = { rpc: 'https://eth-rpc-testnet.polkadot.io', chainId: 420420417, name: 'polkadot-hub-testnet',};
const mnemonic = 'INSERT_MNEMONIC';
deployContract('Storage', mnemonic, providerConfig);To run the script, execute the following command:
node scripts/deploy.jsAfter running this script, your contract will be deployed to Polkadot Hub, and its address will be saved in contract-address.json within your project directory. You can use this address for future contract interactions.
Interact with the Contract
Section titled “Interact with the Contract”Once the contract is deployed, you can interact with it by calling its functions. For example, to set a number, read it and then modify that number by its double, you can create a file named checkStorage.js in the root of your project and add the following code:
const { ethers } = require('ethers');const { readFileSync } = require('fs');const { join } = require('path');
const artifactsDir = join(__dirname, '../contracts');
const createProvider = (providerConfig) => { return new ethers.JsonRpcProvider(providerConfig.rpc, { chainId: providerConfig.chainId, name: providerConfig.name, });};
const createWallet = (mnemonic, provider) => { return ethers.Wallet.fromPhrase(mnemonic).connect(provider);};
const loadContractAbi = (contractName, directory = artifactsDir) => { const contractPath = join(directory, `${contractName}.json`); const contractJson = JSON.parse(readFileSync(contractPath, 'utf8')); return contractJson.abi || contractJson; // Depending on JSON structure};
const createContract = (contractAddress, abi, wallet) => { return new ethers.Contract(contractAddress, abi, wallet);};
const interactWithStorageContract = async ( contractName, contractAddress, mnemonic, providerConfig, numberToSet,) => { try { console.log(`Setting new number in Storage contract: ${numberToSet}`);
// Create provider and wallet const provider = createProvider(providerConfig); const wallet = createWallet(mnemonic, provider);
// Load the contract ABI and create the contract instance const abi = loadContractAbi(contractName); const contract = createContract(contractAddress, abi, wallet);
// Send a transaction to set the stored number const tx1 = await contract.setNumber(numberToSet); await tx1.wait(); // Wait for the transaction to be mined console.log(`Number successfully set to ${numberToSet}`);
// Retrieve the updated number const storedNumber = await contract.storedNumber(); console.log(`Retrieved stored number:`, storedNumber.toString());
// Send a transaction to set the stored number const tx2 = await contract.setNumber(numberToSet * 2); await tx2.wait(); // Wait for the transaction to be mined console.log(`Number successfully set to ${numberToSet * 2}`);
// Retrieve the updated number const updatedNumber = await contract.storedNumber(); console.log(`Retrieved stored number:`, updatedNumber.toString()); } catch (error) { console.error('Error interacting with Storage contract:', error.message); }};
const providerConfig = { name: 'polkadot-hub', rpc: 'https://eth-rpc-testnet.polkadot.io', chainId: 420420417,};
const mnemonic = 'INSERT_MNEMONIC'const contractName = 'Storage';const contractAddress = 'INSERT_CONTRACT_ADDRESS'const newNumber = 42;
interactWithStorageContract( contractName, contractAddress, mnemonic, providerConfig, newNumber,);Ensure you replace the INSERT_MNEMONIC and INSERT_CONTRACT_ADDRESS placeholders with actual values. Also, ensure the contract ABI file (Storage.json) is correctly referenced. The script prints the balance for ADDRESS_TO_CHECK before it writes and doubles the stored value, so pick any account you want to monitor.
To interact with the contract, run:
node scripts/checkStorage.jsWhere to Go Next
Section titled “Where to Go Next”Now that you have the foundational knowledge to use Ethers.js with Polkadot Hub, you can:
- Dive into Ethers.js utilities: Discover additional Ethers.js features, such as wallet management, signing messages, etc.
- Implement batch transactions: Use Ethers.js to execute batch transactions for efficient multi-step contract interactions.
- Build scalable applications: Combine Ethers.js with frameworks like
Next.jsorNode.jsto create full-stack decentralized applications (dApps).