Get Started with Ethers.js
Set up Ethers.js for RISE development
Learn how to set up and configure Ethers.js for building on RISE.
Installation
Install Ethers.js v6 using your preferred package manager:
npm install ethersProject Setup
Create a new project:
mkdir my-rise-ethers-project
cd my-rise-ethers-project
npm init -y
npm install ethers dotenvConnect to RISE
Using JsonRpcProvider
Connect to RISE Testnet:
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://testnet.riselabs.xyz');
// Get network info
const network = await provider.getNetwork();
console.log('Connected to:', network.name);
console.log('Chain ID:', network.chainId);
// Get latest block
const blockNumber = await provider.getBlockNumber();
console.log('Latest block:', blockNumber);Create a Wallet
Create a wallet from a private key:
import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('https://testnet.riselabs.xyz');
// From private key
const wallet = new ethers.Wallet(
'YOUR_PRIVATE_KEY',
provider
);
console.log('Address:', wallet.address);
// Get balance
const balance = await provider.getBalance(wallet.address);
console.log('Balance:', ethers.formatEther(balance), 'ETH');Generate New Wallet
Create a new random wallet:
// Generate new wallet
const newWallet = ethers.Wallet.createRandom();
console.log('Address:', newWallet.address);
console.log('Private Key:', newWallet.privateKey);
console.log('Mnemonic:', newWallet.mnemonic.phrase);
// Connect to provider
const connectedWallet = newWallet.connect(provider);From Mnemonic
Restore wallet from mnemonic phrase:
const mnemonic = 'your twelve word mnemonic phrase here ...';
const wallet = ethers.Wallet.fromPhrase(mnemonic, provider);
console.log('Address:', wallet.address);Environment Variables
Create a .env file:
PRIVATE_KEY=0x...
RPC_URL=https://testnet.riselabs.xyzLoad and use environment variables:
import { ethers } from 'ethers';
import { config } from 'dotenv';
config();
const provider = new ethers.JsonRpcProvider(process.env.RPC_URL);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
console.log('Connected as:', wallet.address);Network Configuration
Create a config file for RISE network:
// config.js
export const RISE_TESTNET = {
chainId: 11155931,
name: 'RISE Testnet',
rpcUrl: 'https://testnet.riselabs.xyz',
explorer: 'https://explorer.testnet.riselabs.xyz',
symbol: 'ETH',
decimals: 18
};
export function getProvider() {
return new ethers.JsonRpcProvider(RISE_TESTNET.rpcUrl);
}
export function getWallet(privateKey) {
const provider = getProvider();
return new ethers.Wallet(privateKey, provider);
}Usage:
import { getProvider, getWallet } from './config.js';
const provider = getProvider();
const wallet = getWallet(process.env.PRIVATE_KEY);Basic Operations
Check Account Balance
const address = '0x...';
const balance = await provider.getBalance(address);
console.log('Balance (wei):', balance.toString());
console.log('Balance (ETH):', ethers.formatEther(balance));Get Transaction Count (Nonce)
const nonce = await provider.getTransactionCount(wallet.address);
console.log('Transaction count:', nonce);Get Gas Price
const feeData = await provider.getFeeData();
console.log('Gas Price:', ethers.formatUnits(feeData.gasPrice, 'gwei'), 'Gwei');
console.log('Max Fee:', ethers.formatUnits(feeData.maxFeePerGas, 'gwei'), 'Gwei');
console.log('Max Priority Fee:', ethers.formatUnits(feeData.maxPriorityFeePerGas, 'gwei'), 'Gwei');Send ETH
const tx = await wallet.sendTransaction({
to: '0x...',
value: ethers.parseEther('0.1') // 0.1 ETH
});
console.log('Transaction hash:', tx.hash);
// Wait for confirmation
const receipt = await tx.wait();
console.log('Transaction confirmed in block:', receipt.blockNumber);Example: Complete Setup
// index.js
import { ethers } from 'ethers';
import { config } from 'dotenv';
config();
// Setup
const provider = new ethers.JsonRpcProvider('https://testnet.riselabs.xyz');
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
async function main() {
// Network info
const network = await provider.getNetwork();
console.log('Network:', network.name);
console.log('Chain ID:', network.chainId.toString());
// Account info
console.log('\nAccount:', wallet.address);
const balance = await provider.getBalance(wallet.address);
console.log('Balance:', ethers.formatEther(balance), 'ETH');
const nonce = await provider.getTransactionCount(wallet.address);
console.log('Nonce:', nonce);
// Block info
const blockNumber = await provider.getBlockNumber();
console.log('\nLatest block:', blockNumber);
const block = await provider.getBlock('latest');
console.log('Block timestamp:', new Date(block.timestamp * 1000).toISOString());
// Gas info
const feeData = await provider.getFeeData();
console.log('\nGas Price:', ethers.formatUnits(feeData.gasPrice, 'gwei'), 'Gwei');
}
main().catch(console.error);Run the script:
node index.jsNext Steps
- Reading from Contracts - Query contract data
- Writing to Contracts - Send transactions and interact with contracts