Ethers.js
Overview
The Ethers.js code library provides a variety of tools for interacting with Ethereum nodes using JavaScript, similar to Web3.js. SCS Chain offers an API that is similar to Ethereum and fully compatible with Ethereum-style JSON RPC calls. Therefore, developers can leverage this compatibility and use the Ethers.js library to interact with SCS Chain nodes in the same way as with Ethereum. You can learn more about Ethers.js in this article.
Creating a Project
First, you need to create a directory to store all the files you will create in this tutorial:
mkdir project-demo && cd project-demo
You will need to install the Ethers.js code library and the Solidity compiler. You can install both npm packages by running the following command:
npm install ethers solc@0.8.0
Setting up Ethers Provider
You will be creating scripts that perform different functions such as sending transactions, deploying contracts, and interacting with a deployed contract. In most of the scripts, you will need to create an Ethers provider to interact with the network.
- Importing the Ethers Code Library
- Define the providerRPC object, including the network configuration on which you want to send transactions. You need to include the name, rpc, and chainId of the network.
- Create a provider using the ethers.JsonRpcProvider function.
// 1. Add the Ethers provider logic here:
// {...}
// 2. Define network configurations
const providerRPC = {
scs: {
name: 'SCS Chain',
rpc: 'INSERT_RPC_API_ENDPOINT',
chainId: 1969, // testnet 1969, mainnet 1970
},
};
// 3. Create ethers provider
const provider = new ethers.JsonRpcProvider(
providerRPC.scs.rpc,
{
chainId: providerRPC.scs.chainId,
name: providerRPC.scs.name,
}
);
Checking Balances
First, you can run the following command to create a balances.js file:
touch balances.js
Next, you will create a script for this file and follow these steps:
- Set up the Ethers provider.
- Define the addressFrom and addressTo variables.
- Create an asynchronous balances function that wraps the provider.getBalance function.
- Use the provider.getBalance function to retrieve the balances of the addressFrom and addressTo addresses. You can also use the ethers.formatEther function to convert the balances into a readable format in ETH units.
- Finally, call the balances function.
// 1. Add the Ethers provider logic here:
// {...}
// 2. Create address variables
const addressFrom = 'ADDRESS-FROM-HERE';
const addressTo = 'ADDRESS-TO-HERE';
// 3. Create balances function
const balances = async () => {
// 4. Fetch balances
const balanceFrom = ethers.formatEther(await provider.getBalance(addressFrom));
const balanceTo = ethers.formatEther(await provider.getBalance(addressTo));
console.log(`The balance of ${addressFrom} is: ${balanceFrom} SCS`);
console.log(`The balance of ${addressTo} is: ${balanceTo} SCS`);
};
// 5. Call the balances function
balances();
You can run the following command to execute the script and retrieve the account balances:
node balances.js
Sending Transactions
First, you can run the following command to create a transaction.js file:
touch transaction.js
Next, you will create a script for this file and follow these steps:
- Set up the Ethers provider.
- Define the privateKey and addressTo variables. Here, you need the private key to create a wallet instance. Note: This operation is for demonstration purposes only.Do not store your private key in a JavaScript file.
- Create a wallet using the privateKey and provider from the previous steps. This wallet instance will be used to sign transactions.
- Create an asynchronous send function that encapsulates the transaction target and the wallet.sendTransaction function.
- Create a transaction object that only requires the recipient address and the amount to send. Note that you can use ethers.parseEther, which handles the necessary unit conversion from Ether to Wei, just like using ethers.parseUnits(value, 'ether').
- Use the wallet.sendTransaction function to send the transaction and then await the completion of the transaction and retrieve the transaction receipt.
- Finally, call the send function.
// 1. Add the Ethers provider logic here:
// {...}
// 2. Create account variables
const account_from = {
privateKey: 'YOUR-PRIVATE-KEY-HERE',
};
const addressTo = 'ADDRESS-TO-HERE';
// 3. Create wallet
let wallet = new ethers.Wallet(account_from.privateKey, provider);
// 4. Create send function
const send = async () => {
console.log(`Attempting to send transaction from ${wallet.address} to ${addressTo}`);
// 5. Create tx object
const tx = {
to: addressTo,
value: ethers.parseEther('1'),
};
// 6. Sign and send tx - wait for receipt
const createReceipt = await wallet.sendTransaction(tx);
await createReceipt.wait();
console.log(`Transaction successful with hash: ${createReceipt.hash}`);
};
// 7. Call the send function
send();
You can run the following command in the terminal to execute the script:
node transaction.js