Testing interactions between your contract and metamask in a hardhat project
In the Ethereum ecosystem, interaction with external wallets such as metamask requires careful consideration of security, user -friendliness and test. One way to test interactions between your intelligent contracts and metamask is to use a combination of tools of a hardhat project. In this article we will examine how this can be achieved.
The problem:
If you want to interact in your contract with an external wallet like Metamask, you need:
- Send the user’s private key or token to your contract.
- Use the “Web3
library" to send the transaction.
- Check the Metamask response.
This process requires several steps and is susceptible to errors if they are not carried out correctly. To simplify this process, we will use a hardhat project as a bridge between the interaction of our contract with Metamask and the browser environment.
Configure hardhat
First install hardhat by carryingNPX Hardhat -run in Watch(or NPX Hardhat -W
). This command installs the necessary facilities to test the interactions between your contract and your metamasks. You can also use wires and add hardhat to get started.
Creating a new contract
Create a new intelligent contract of solidity with webpack, ethers.js or truffl. In this example we will use truffles.
`Solidity
// src/contracts/mycontract.sol
Pragma solidity ^0.8.0;
Mycontract {contract
Mapping (address => uint256) public credit;
Function deposit (value Uint256) Public too payable {
Balances [msg.sender] += quantity;
}
Functional deduction (Valor Uint256) Public {
Requires (balances [msg.sender]> = quantity, “insufficient balance”);
Balances [msg.sender] -= value;
}
}
`
Write a hardhat test
Create a new file called “mycontract.test.js” on the red of your project:
`JavaScript
Consta {waiting} = required (“chai”);
Test metamamask asynchied function () {
Const MyContract = Wait Web3.eth.Getcontatat (“0x …”); // replace your contract address
// Send the user’s private key to a contract
Mycontract.sendtransaction ({{{
By: “0x …”,
To: “0x …”,
Value: 1 ether, // replace with the private key value of the user
});
Const Balances for = Web3.eth.Getbalance (“0x …”); // replace the address of your user
Wait (balance before).
// Use web3 to send the transaction to metamast
Mycontract.sendtransaction ({{{
By: “0x …”,
To: “0x …”,
Value: web3.utils.towei (“1”, “Ether”), // Set up with the private key and token type of the user (e.g. white to ether)
Data: “Hello, Metamask!”,
});
}
`
Do your test
To carry out your test in a development environment:
`JavaScript
Npx hardhat racing mycontract.test.js
`
In this example, we use the Web3 to send “MyContract” dance transactions from our contract to the user’s Ethereum portfolio. Then we found that the transaction successfully checked our user’s balance.
Testing interactions with metamask
To test the interactions between your contract and your metamask:
- Replace the reserved memory values (e.g. contract address, the private key value of the user) in the file “mycontract.test.js”.
- Update the “Balancephone” variable to reflect the new balance of our user after interaction with Metamask.
`JavaScript
// update credit before the transaction
Const Balances for = Web3.eth.Getbalance (“0x …”); // replace the address of your user
Wait (balance before).
// Use web3 to send the transaction to metamast
Mycontract.sendtransaction ({{{
By: “0x …”,
To: “0x …”,
Value: web3.utils.towei (“2”, “Ether”), // Replace the amount of private key and token types of the user (e.g. Wei according to ether)
Data: “Hello, Metamask!