Metamask Wallet Source Development Getting Started Guide: From Environment Configuration to First DApp

Metamask WalletSource CodeDevelopmentGetting Started Guide

MetaMaskis one of the most popular browser plug-in wallets with multi-chain support. It allows users to interact with the ethereum ecosystem, which hosts a large number of decentralized applications (Dapps), without having to download the entireBlockchain.
Author:DexNavMetaMask MetaMask wallet features custom development: DexDao

I. Metamask wallet developmentEnvironment Configuration

Metamask wallet development requires a good development environment to be set up first. The following are the basic steps for setting up the Metamask wallet development environment.

A. Install Node.js and npm

In developing the Metamask wallet, you need to use Node.js and npm. node.js is a JavaScript runtime environment based on the Chrome V8 engine, and npm is a package manager for Node.js. You can check if Node.js and npm are installed by using the following command.

node -v
npm -v

Have not yet downloaded Node.js , go to the official website to download:.

B. InstallationTruffle Framework.Ganache

Truffle is a smart contract development framework based on the Solidity language that provides some handy command line tools to compile, deploy and test smart contracts. Ganache is a personal blockchain for local development and testing of blockchain applications.

Truffle and Ganache can be installed using the following commands.

npm install -g truffle
npm install -g ganache-cli

C. Install Metamask wallet

Metamask wallet is a browser-based ethereum wallet that provides a convenient interface to manage ethereum and ERC20 tokens, and also works with ethereumDAppPerform interaction.

You can install the wallet by searching for "Metamask" in the extensions store of Chrome or Firefox browsers.

Once the installation is complete, you need to create an Ether account in your Metamask wallet and connect that account to Ganache in your development environment.

These are the basic steps to build the Metamask wallet development environment. Next, you can start writing the first DApp and interacting in the Metamask wallet

Metamask Wallet Source Development Getting Started Guide: From Environment Configuration to First DApp

II. Development of the first DApp

The Metamask wallet is a browser-based access to the Ethernet network that can be usedWallet Plugin, supports managing Ether and ERC-20 tokens, and provides a Web3.js interactive interface for DApps to use. This section will describe how to develop the first DApp using the Metamask wallet.

A. Create a new Truffle project

First, a new Truffle project needs to be created for the DApp development. A new Truffle project can be created from the command line using the following command.

truffle init

B. Writing Solidity smart contracts

in the Truffle projectcontractsfolder, create a folder namedSimpleStorage.solof the Solidity smart contract, the code is as follows.

pragma solidity ^0.8.0;

contract SimpleStorage {
  uint256 private _value;

  function getValue() public view returns (uint256) {
    return _value;

  function setValue(uint256 value) public {
    _value = value;

The smart contract is a simple storage contract that can be used to store auint256type and provides an interface to read and modify that value.

C. Run smart contracts and deploy them to the local blockchain

Run the following command from the command line to start the local blockchain and deploy smart contracts to the local blockchain.

truffle migrate

D. PreparationWeb3.js interaction code

In the root directory of the Truffle project, create a file calledapp.jsJavaScript file and write the following code.

const Web3 = require("web3");
const SimpleStorage = require(". /build/contracts/SimpleStorage.json");

const web3 = new Web3(Web3.givenProvider || "http://localhost:8545");

const contractInstance = new web3.eth.Contract(

async function getValue() {
  const value = await contractInstance.methods.getValue().call();
  console.log(`Value: ${value}`);

async function setValue() {
  const value = 42;
  await contractInstance.methods.setValue(value).send({ from: web3.eth.defaultAccount });
  console.log(`Value set to: ${value}`);

window.getValue = getValue;
window.setValue = setValue;

The code uses the Web3.js library to interact with the smart contracts on the local blockchain. In the code, it first creates aweb3instance and uses the ABI and address of the smart contract to create acontractInstanceinstances. Next, write thegetValue.setValuefunction to read and modify the stored values in the smart contract.

E. Using DApp in the Metamask Wallet

Finally, you need to start a web server in your browser and add the RPC URL and private key of the local blockchain network to the Metamask wallet.

Start the web server from the command line using the following command.

npm install -g http-server
http-server -p 8000

F. Deploy DApp to test network

    1. Select a suitable test network, such as Ropsten, Kovan, Rinkeby, etc.
    2. Modify the Truffle configuration file (truffle-config.js or truffle.js) to add the corresponding network configuration, e.g.
ropsten: {
  provider: () => new HDWalletProvider(mnemonic, `${infuraKey}`),
  network_id: 3,
  gas: 5500000,
  confirmations: 2,
  timeoutBlocks: 200,
  skipDryRun: true

Among them.mnemonicis a helper word.infuraKeyis the API Key for the Infura project.

      1. Execute the command in the terminal for deployment.
truffle migrate --network ropsten

Hereropstenis the name of the test network previously added in the configuration file.

G. Adding a test network to the Metamask wallet

    1. Open Metamask Wallet and click the Network Switch button in the upper left corner.
    2. Click on the "Custom RPC" button and enter the network name, RPC URL, chain ID and symbol of the test network, e.g.
    1. Click the "Save" button and the Metamask wallet will automatically switch to the added test network.

Now you are ready to use your DApp on the test network. Remember that the ETH used on the test network is not real ETH and therefore cannot be used for actual transactions, only for testing.

III. Advanced development skills and applications

A. Use the Metamask provided byAPI Interface

The Metamask wallet provides a number of API interfaces that make it easier for developers to interact with the wallet. The following are some of the commonly used API interfaces.

    1. ethereum.enable(): request user authorization to connect to the ethereum network.
    2. ethereum.on('accountsChanged', callback): listen for account change events.
    3. ethereum.request({ method: 'eth_sendTransaction', params: [transactionParams] }): sends the transaction.

B. Development of Metamask wallet plugin

Metamask Wallet is a Chrome-based plugin. Developers can extend the functionality of Metamask Wallet by developing wallet plugins. The following are some common wallet plugin development tips.

    1. Inject custom scripts into the page.
    2. Listens for Metamask wallet events.
    3. Interact with Metamask wallets, such as sending transactions, etc.

C. with otherBlockchain Application Integration

Metamask wallet supports integration with other blockchain applications. The following are some common blockchain application integration tips.

    1. Use Web3.js to interact with other blockchain applications.
    2. Connect to other blockchain networks via RPC calls.
    3. Use smart contracts to interact with other blockchain applications.

Code example.

The following is a code example for sending a transaction using the Metamask API.

const ethereum = window.ethereum;
if (ethereum) {
  try {
    await ethereum.enable();
    const provider = new ethers.providers.Web3Provider(window.ethereum);
    const signer = provider.getSigner();
    const transaction = {
      to: '0x...' ,
      value: ethers.utils.parseEther('1.0')
    const tx = await signer.sendTransaction(transaction);
    console.log(`Transaction sent: ${tx.hash}`);
  } catch (error) {
} else {
  console.error('Metamask not detected');

The above code first checks if it is already connected to Metamask wallet, if it is, it uses Web3Provider to get the signer and sends the transaction using signer.sendTransaction.

IV. Summary

The concluding part of the introductory guide to Metamask wallet development will explore the current and future trends of Metamask wallet, as well as its importance and application value in the DApp ecosystem.

A. Current and future trends of Metamask wallet development

Metamask wallet, one of the most popular ethereum wallets, has been widely recognized and used for its development and usage. Currently, Metamask wallet has become an integral part of the DApp ecosystem and more and more people are using it for cryptocurrency transactions and management. In the future, with the continuous development of blockchain technology and the expansion of application scenarios, the application prospect of Metamask wallet will be even broader.

B. The importance and application value of Metamask wallet in DApp ecology

Metamask wallet plays a very important role in the DApp ecosystem by providing a convenient and secure way to manage users' crypto assets and interact with DApps. In addition to that, Metamask wallet provides a powerful API interface that makes it easy for developers to integrate DApps with Metamask wallet. This provides great convenience for DApp development and promotion, and also makes Metamask wallet an integral part of the DApp ecosystem.

In short, Metamask wallet is a powerful, convenient and secure wallet application that has become part of the DApp ecosystem. With the continuous development of blockchain technology and the expansion of application scenarios, the application prospect of Metamask wallet will be even broader. For developers who want to enter the blockchain application development field, it will be very important and valuable to learn and master the development techniques and application scenarios of Metamask wallet.

Wallet function custom development: DexDao
Scientists Telegraph Community: DexNav
DexFilter Traders Exchange Group:DexFilter

Related posts

No comments

No comments...