Catégories
CRYPTOCURRENCY

Metamask: Solidity Smart Contract Behaving Weird on Goerli Testnet

Metamask: behavior of the intelligent contract of solidity on Goerli Testnet

As a new developer in the cryptocurrency space, you are probably anxious to test your metavees projects before distributing them on Mainnet. A way to do this is to use the famous Metamask portfolio, which allows users to archive and manage their Ethereum accounts on various networks, including the Goerli testnet. However, when it comes to building an intelligent contract that interacts with the user’s account, things can become interesting.

Background

Metamask is an extension of the browser that allows users to create, archive and manage more Ethereum accounts on different networks, including Goerli Testnet. The portfolio supports various Ethereum versions (eg 1.x, 2.x, 3.x) and allows the creation of complex intelligent contracts that use solidity, the official programming language for Ethereum.

Setup Testnet

To test the intelligent contract enabled for Metamask on Goerli Testnet, you will have to follow these steps:

  • Download and install the latest version of Truffle, which is a popular framework for the construction and distribution of intelligent Ethereum contracts.

  • Create a new truffle project using the CLI truffle:

`Bash

Truffle Init

`

  • Install the metamask extension for webpack (or esm) in the Truffle project:

Javascript

NPM Install Metamask-Webpack

`

  • Configure Metamask to use Goerli Testnet:

dictiloscript

Import {MetamaskProvider} from ‘@Metamask/Webpack’;

Const MetamaskPrier = New MetamaskPrier ({

URL: ‘http: // localhost: 8545’,

Network: 1,

});

`

Building the intelligent contract

Now that you have configured your truffle and configured Metamask project, we create a simple intelligent contract that interacts with the account of a user.

Create a new file calledcontracttt.tsand add the following code:

dictiloscript

import {eters} from ‘eters’;

Useraccount interface {

Balance: Etheter.bigint;

}

Contract = New Ethers.Conttract (‘0x … Your contract ABI …’, ‘0x … your map of the source of the contract …’, MetamaskProsider);

`

Replace‘0x … your contract abi …’ » 0x … your map of the source of the contract … ‘with the current ABI (binary interface of the application) and the Map of origin of the contract.

Intelligent contract test

Metamask: Solidity Smart Contract Behaving Weird on Goerli Testnet

To test the intelligent contract, it is possible to use the recourse function integrated of the truffle:

`dictiloscript

contracttt.test ({

Account: [‘Account1’, ‘Account2’],

// call the function of the contract

Call: ‘Function Balance () Return public (Bigint) {Return Libra (); } ‘,

});

`

Replace » Function Libra () Return public (Bigint) {Return Libra (); } ‘With the actual name of the function of the contract.

behavior problem

However, when you test the intelligent contract, you may find a problem. Function callsBalance () are not correctly performed on Goerli Testnet.

The problem lies in the way Metamask interacts with the Ethereum network. When a user stores their account on Metamask and therefore tries to interact with the account using the `test 'function' function, Metamask is unable to update the account balance due to the way it manages transactions and the transactions State updates.

Solution

To solve this problem, one of the two approaches can be used:

  • Use a different contract : Create a new intelligent contract that is not based on thebalance ()or other data provided by the user.

  • Implement your balance of the balance : If you need to archive and manage the account sales, you can create a personalized portfolio module that manages the account updates using web3.js.

For this example, we will use the second approach. Create a new file calledaccountManager.tsand add the following code:

dictiloscript

import {account} from ‘@metamask/webpack’;

import {eters} from ‘eters’;

Class AccountManager {

Private accounts: Account [];

CONSTRUTOR () {

This.acounts = [];

}

Addacount (account: account) {

This.

ETHEREUM DESKTOP

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *