Metamask: Solidity Intelligent Contracts Beerli Testnet
As a new developer in a crypt space, you probably want to test your metaverse projects before putting them in Mainnet. One of the ways to do this is to use the popular Metamask wallet that allows users to store and manage their Ethereum accounts in different networks, including Goerli Testnet. However, when it comes to building an intelligent contract that interacts with the user’s account, things may be interesting.
background
Metamask is an extension of your browser that allows users to create, store and manage multiple Ethereum accounts in different networks including Goerli Testnet. The wallet supports different versions of Ethereum (eg 1.x, 2.x, 3.x) and allows the creation of complex intelligent contracts using solidity, official programming language for Ethereum.
Settings Testnet
If you want to test your intelligent contract with Metamask’s support for Goerli Testnet, you will need to follow these steps:
- Download and install the latest version of Truffle, which is a popular framework to compile and deploy smart Ethereum contracts.
- Create a new truffle project using Truffle CLI:
`Bash
Truffle Init
`
- Install Metamask extension for WebPack (or ESM) to your Truffle project:
`Javascript
NPM Install Metamask-Webpack
`
- Configure Metamask to use Goerli Testnet:
`Typescript
Import {metamaskprovider} from ‘@metamask/webpack’;
Const MetamaskProvider = New MetamaskProvider ({
URL: ‘http: // localhost: 8545’,
Network: 1,
});
Building an intelligent contract
Now that you have configured your Truffle and Metamask project, let’s make a simple intelligent contract that interacts with the user’s account.
Create a new file called “Contract.TS” and add the following code:
`Typescript
Import {ethers} of ‘ether’;
UseRCOUNT interface {
Balance: Etshers.bigint;
}
Contract = New Ethers.Contract (‘0x … Your contract Abi …’, ‘0x … Your contract source …’, metamaskprrovider);
Replace '0x ... Your contract Abi ...'
and '0x ... Your contract source map ...'
with the actual Abi (binary application interface) and the source map of your contract.
Testing Smart Treaty
If you want to test an intelligent contract, you can use the built -in featuretest ‘HRUFFLE:
`Typescript
contract.Test ({
Accounts: [‘Account1’, ‘Account2’],
// Call your contractual function
Call: ‘Function Bilant () Public Returns (Bigint) {Return Bilant (); } ”,
});
Replace 'Function Bilant () Public Returns (Bigint) {Return Bilant (); } '
with the actual name of your contractual function.
CUSTOMER problem
However, when you start a smart contract test, you can encounter a problem. Functional calls balance ()
are not done correctly at Gerli Testnet.
The problem is how metamascus interacts with the Ethereum network. When a user saves his account on metamascus and then attempts to interact with the Truffle account with the TRUFFLE function, Metamask is unable to update the account balance due to the way it processes transactions and status updates.
Solution
You can use one of two approaches to solve this problem:
1.
- Implement your own account balance manager
: If you need to store and manage account balances, you can create your own wallet module that processes account status updates using Web3.Js.
In this example we will use a second approach. Create a new file called “AccountManager.TS” and add the following code:
“ Typescript
Import {foreign} from ‘@metamask/webpack’;
Import {ethers} of ‘ether’;
Class Accountmanager {
Private Accounts: Account [];
constructor () {)
this.accounts = [];
}
AddacCount (Account: Account) {
this one.