Ethers.js
Overview
Ethers.js is a compact library designed to enable developers to interact with the Ethereum blockchain. By using Ethers.js, you can interact with smart contracts deployed on Soneium Minato.
Install
To install Ethers.js, run the following command:
$ pnpm i ethers
Setting Up the Provider and Connecting to Minato
To begin using ethers.js, set up your provider as shown below:
import { ethers } from 'ethers';
const rpc = 'https://rpc.minato.soneium.org/'
// Connect to the Soneium Minato RPC
const provider = new ethers.JsonRpcProvider(rpc);
Reading Data from Soneium Minato
You can read data from Soneium Minato by using the provider.
For instance, to get the balance of a specific wallet address, you can use the getBalance method:
import { ethers } from 'ethers';
import { provider } from './config'
const address = '0xYourWalletAddress';
const balance = await provider.getBalance(address);
console.log(`Balance: ${ethers.formatEther(balance)} ETH`);
Writing Data to Soneium Minato
To send transactions or interact with accounts, you need to create a signer using your private key. This allows you to sign and send transactions:
import { ethers } from 'ethers';
import { PRIVATE_KEY, provider } from './config';
const signer = new ethers.Wallet(PRIVATE_KEY, provider);
const tx = {
to: '0xDestinationAddress',
value: ethers.parseEther('0.01'),
gasLimit: 21000,
gasPrice: ethers.parseUnits('20', 'gwei'),
};
// Send the transaction
const transaction = await signer.sendTransaction(tx);
Interacting with Smart Contracts
Ethers.js provides a simple interface to interact with Ethereum smart contracts. Below is an example of interacting with an ERC20 contract on Soneium Minato.
Read the values from the contract:
import { ethers } from 'ethers';
import { erc20Abi } from './ABI';
import { USDC_ADDRESS, SENDER_ADDRESS, SPENDER_ADDRESS } from './config';
const contract = new ethers.Contract(USDC_ADDRESS, erc20Abi, provider);
const allowance = await contract.allowance(SENDER_ADDRESS, SPENDER_ADDRESS);
Send contract function:
import { ethers } from 'ethers';
import { erc20Abi } from './ABI';
import { PRIVATE_KEY, USDC_ADDRESS, SENDER_ADDRESS, SPENDER_ADDRESS, provider } from './config';
const signer = new ethers.Wallet(PRIVATE_KEY, provider);
const contract = new ethers.Contract(USDC_ADDRESS, erc20Abi, signer);
const amount = "1000000";
await contract.approve(SPENDER_ADDRESS, amount);