πUnderstanding the EVM provider/signer concepts
In this section you'll learn what a provider and a signer are and how to use them.
The provider
A provider in Ethers.js
serves as a bridge between your application and the Ethereum blockchain. It facilitates communication, allowing your application to retrieve information from the Ethereum network. Think of it as a window into the blockchain, providing you with data without exposing the complexities of the underlying infrastructure.
The different types of providers available in ethers:
Declare a provider using ethers:
const { ethers } = require('ethers');
// Replace the moonbeam RPC by your desired RPC provider
const provider = new ethers.providers.JsonRpcProvider(`https://moonbeam.unitedbloc.com`);
// Now you can use the provider to interact with the EVM blockchains
provider.getBlockNumber().then((blockNumber) => {
console.log('Current Block Number:', blockNumber);
});
The signer
Moving on to the concept of a signer, it plays a crucial role in facilitating transactions on the Ethereum blockchain. A signer is responsible for signing transactions with a private key, providing the necessary authorization to execute actions like sending Ether or interacting with smart contracts.
Use a signer using ethers and send a transaction:
const { ethers } = require('ethers');
// Replace 'your_private_key' with the private key of your Ethereum account
const privateKey = 'your_private_key';
const wallet = new ethers.Wallet(privateKey);
// Now, the wallet acts as a signer, allowing you to sign transactions
const transaction = {
to: '0x0123456789ABCDEF0123456789ABCDEF01234567', // Replace with the recipient's address
value: ethers.utils.parseEther('1.0'), // Sending 1 Ether
};
wallet.sendTransaction(transaction).then((transactionHash) => {
console.log('Transaction Hash:', transactionHash);
});
Last updated