Here is an article on the use of Ethers.js to connect the metamask to the local Hardhat node supplier:
Metamascus connection to a local Hardhat node supplier with Ethers.js
During the development of decentralized programs (DAPP) requiring communication with external services such as Metamask or web providers, it is very important to connect to the local solid node. In this article, we will examine how to use Ethers.js to reach this connection.
Why use Ethers.js?
Ethers.js is the official JavaScript library to interact with Ethereum Blockchain. This provides a simple and intuitive API to work with web3 suppliers such as Metamask, Web3.JS and more. With Ethers.JS, you can easily connect your local Hardhat node to external services without worrying about configuring an infrastructure or additional configurations.
Local Hardhat node parameter
Make sure your local Hardhat node is properly defined before immersing the Metamk connection. Here is a brief overview of the action:
- Install the truffle system: truffle gives you a way to manage and interact with your blockchain projects using JavaScript.
- Define the new project catalog and initiate it:
`Bash
Mkdir launch example
Example of Metamask-Union CD
NPX TRUFFEL INIT
'
- Create a new contract file (such as MyContract.sol) in the project catalog:
Solidarity
Pragma Solidity ^ 0.8,0;
Contract with myctract {
Uint256 public value;
}
'
- Make and install your contract:
Bash
Locate the truffles
Triple deployment
'
Metamascus connection with Ethers.js
Now that you need to configure a local Hardhat node using Ethers.js, connect Metamask. We will use the Ethers.js library to interact with metamask.
Create a new file called metamk-connection.js and add this code:
Javascript
Const Ethers = require ('ether');
// Define the address of the contract and both
ConstActaddress = '0x ...'; // Change your contract address
Const Bight = [...]; // Change your two contracts
// Create a new copy of the Ethers supplier for your local Hardhat node
Const Provider = New Ethers.Providers.htPProvider ('http: // Localhost: 8545');
// Create a new copy of Ethers.js Cash using Methers.
Const Wallet = New ETERS.WALLET (supplier, '0x ...'); // Change your metamask with a private key
// Get an instance of a contract using your wallet
Const Contctionance = New Ethers.Contract (Contractaddress, the two, portfolio);
// You can now use a contract copy to interact with the blockchain
Contacinstance.Value.set (123);
'
Example of use
Here is an example of how you can use the Metamk connection to determine the value of your contract:
Javascript
Metamk-connection.js
'
Assuming
Javascript
Setvalue = (meaning) => {
Contacinstance.Value.set (value);
}
` ‘
Tips and options
- Make sure to replace the Contractddress variables, “both” and “portfolios” with the address of the real contract, both and “metamask” with a private key.
- You can also use other Ethers.js suppliers such as http: // Localhost: 8546 “or” https: // mainnet.infura.io/v3/your_preiat_id.
- If you use another web3 supplier (for example, web3.js), you will need to adjust the communication code accordingly.
When you perform these actions and examples, you should now be able to connect your local Hardhat node to Metamask with Ethers.js. Happy building!