Metamask Simplified: A Step-by-Step Guide to Using Metamask with Brownie and Flask
As a developer building decentralized applications (dApps) on top of blockchain networks, understanding how to interact with them requires a solid grasp of various tools. One crucial tool for managing smart contracts is Metamask, which enables seamless interaction between your frontend app and the underlying blockchain network. In this article, we’ll walk you through the process of setting up Metamask with Brownie and Flask.
What is Metamask?
Metamask is a user-friendly, open-source tool that allows developers to manage access to the Ethereum blockchain from any application or browser. It provides a secure way to store and manage your private keys, enabling you to interact with smart contracts without exposing them directly to the public network.
Setting up Brownie and Metamask
First, let’s set up our Brownie app:
- Install Brownie: Open your terminal and install Brownie using npm by running the following command:
npm install -g brownie
- Create a new Brownie project: Run the following command to create a new directory for your project and navigate into it:
brownie init
- Install Metamask
: Run the following command to install Metamask using npm by running the following command in your terminal:
npm install -g metamask
Setting up Metamask
Now, let’s set up our Metamask instance:
- Launch MetaMask: Open a browser and navigate to [metamask.io]( to launch your personal MetaMask wallet.
- Create a new account (optional): If you want to manage multiple wallets, create an additional account on the site.
Spin up a frontend with Brownie
To spin up a frontend application using Brownie and Metamask, follow these steps:
- Install dependencies
: In your main project directory, run the following command to install any required dependencies:
npm install express flask web3
- Create an Express server: Create a new file
app.js
in the root of your project and add the following code:
const express = require('express');
const app = express();
const Web3 = require('web3');
// Initialize Metamask instance with your private key
const web3 = new Web3(new Web3.providers.HttpProvider('
app.use(express.static(__dirname));
let contractAddress;
if (process.env.BROWNIE_CONTRACT_ADDRESS) {
contractAddress = process.env.BROWNIE_CONTRACT_ADDRESS;
}
// Your main app route
app.get('/', (req, res) => {
res.send('Hello World!');
});
const port = 3000;
app.listen(port, () => {
console.log(Server is running on
});
This code initializes a basic Express server that listens for requests to the root URL. When a request is made to the root URL, it checks if the BROWNIE_CONTRACT_ADDRESSenvironment variable is set and uses it as the contract address.
Initiate a transaction
To initiate a transaction with Metamask, use the following code:
const txHash = await web3.eth.sendTransaction({
from: '0xYOUR_METAMASK_ADDRESS',
to: '0xCONTRACT_ADDRESS',
value: '0.01 ether',
gas: '20000',
gasPrice: '20 gwei'
});
This code sends a transaction with the following properties:
- from
: The sender's Ethereum address.
- to
: The recipient's Ethereum address.
- value
: The amount to be transferred in Ether.
- gas
: The gas limit for the transaction.
- gasPrice
: The gas price of the transaction.
Have Metamask sign the transaction
To have Metamask sign the transaction, use the following code:
``javascript
const tx = await web3.eth.sendSignedTransaction({
from: '0xYOUR_METAMASK_ADDRESS',
to: '0xCONTRACT_ADDRESS',
value: '0.