Testing interaction between contract and metamask in the Hardhat project

In Ethereum’s ecosystem, interaction with external wallets, such as metamask, requires careful consideration of safety, usability and testing. One way to test the interaction between intelligent contracts and metamask is to use the tools of the Hardhat project. In this article, we explore how this is achieved.

Problem:

When you want to interact with an external wallet like in your metamask contract, you have:

This process requires several steps and is susceptible to errors if it is not done correctly. To simplify this process, we use the Hardhat project as a bridge with the interaction between the contract between metamask and the browser environment.

Setting Hardhat

First, install the hardhat by performing the npx hardhat run on the Watch (ornpx hardhat -w). This command installs the necessary addictions to test the interaction between the agreement and the metamask. You can also use “yarn more hardhat” to start.

Creating a new contract

Create a new solidity with a smart contract with webpack, eetter.js or truffle. In this example we use truffle.

`Solidity

// src/contracts/mycontract.sol

Pragman solidity ^0.8.0;

Agreement MyContract {

mapping (address => UINT256) Public balances;

Function deposit (UINT256 quantity) Public payable {

Balance [msg.seender] += amount;

}

Function Lifting (UINT256 quantity) Public {

require (balances [msg.seender]> = amount, “inadequate balance”);

Balance [msg.sender] -= quantity;

}

}

`

Writing the Hardhat test

Create a new file called mycontract.test.js at your project:

`Javascript

Const {wait} = insist (“chai”);

Async function testmetamask () {

Const mycontract = Wait web3.eth.getcontractat (“0x …”); // replace with a contract address

// Submit the user’s private key to a contract

myContract.sendtransaction ({

Sender: “0x …”,

to: “0x …”,

Value: 1 Ether, // Replace the user with private key sum

});

Const BalanderdBforele = Wait web3.eth.getbalance (“0x …”); // replace the user with

expect (balance) .to.equal (0);

// Use Web3 to send an event to Metamask

myContract.sendtransaction ({

Sender: “0x …”,

to: “0x …”,

Value: web3.utils.towei (“1”, “ether”), // replace the user with private key sum and character type (eg ether wei)

Information: “Hi, Metamask!”,

});

}

`

To complete the test

Examination in a development environment:

`Javascript

Npx hardhat run mycontract.test.js

`

In this example, we use web3 to send events from our “MyContract” event to the user’s Ethereum wallet. We then make sure that the event was successful by checking the user balance.

Testing interaction with metamask

Metamask: Test Metamask interactions from a hardhat project?

Testing interaction between contract and metamask:

`Javascript

// Update balances before the event

Const BalanderdBforele = Wait web3.eth.getbalance (“0x …”); // replace the user with

expect (balance) .to.equal (1);

// Use Web3 to send an event to Metamask

myContract.sendtransaction ({

Sender: “0x …”,

to: “0x …”,

Value: web3.utils.towei (“2”, “ether”), // replace the user’s private key sum and character type (eg eetter wei)

Information: “Hi, Metamask!

metamask doesnt show metamask

Leave a Reply

Your email address will not be published. Required fields are marked *