How to Build and Deploy an Ethereum Wallet with Brownie and Infura

Muhammed Ali
CoinsBench
Published in
5 min readJun 8, 2022

--

You may be new to Solidity and were wondering if you could build a project with Solidity and Python. If this is you, you have come to the right place.

In this article, you will learn how to build an Ethereum wallet with Solidity and then deploy it to Infura using Python. This will be a minimalistic wallet that will enable you to send and receive Ether. Instead of building this project directly with Python, we will use Brownie. Brownie is a Python-based framework that enables you to build, deploy, interact, debug and test Ethereum smart contracts. Among other advantages, Brownie enables you to work with different Ethereum networks easily.

Solidity will be used to build the wallet itself, while Python will be used to build the interactive functionality and server.

Although this article will not go through details of how the wallet is built in Solidity, there are comments attached to the code functions. As for Brownie, you will get all the details you need.

Prerequisites

  1. Basic understanding of Solidity and Python
  2. Two Metamask account on Rinkeby Test network (with test ETH)

Initialize Brownie for Smart Contract

Here you will do the initial setup for Brownie so that we can build and deploy the smart contract (Ethereum wallet).

First, install Brownie with pip by running: pip install eth-brownie.

Next, create a new directory (mkdir) named project for your project (directory name is arbitrary), and switch (cd) to that directory.

mkdir projectcd project

Next, run the following command to initialize a Brownie project. Running the command below will generate some boilerplate files that will enable you to build with Brownie.

brownie init

Developing the Ethereum Wallet in Solidity

In this section, you will build an Ethereum wallet that is able to receive and send Ethereum with Solidity. The addresses of senders will be stored and mapped to the amount sent.

To accomplish this, you will create a function that is able to receive payment, then map addresses and the amount of ETH deposited.

Next, you will write a function for withdrawal, then add a condition to make sure only the owner of the account can withdraw. The first person to deploy the contract is the owner in this case.

To get started, create a new Solidity file in contracts/ (was created when you ran brownie init), in my case, it’ll be wallet.sol. Now paste the following code into the file you just created. For convenience, you will have comments to describe what each line is doing.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;
contract EtherWallet {

//mapping to store addresses and amount of ETH deposited
mapping(address => uint256) public addressToAmountReceived;

// array of addresses who deposited
address[] public senders;
address public owner; // the first person to deploy the contract is the ownerfunction received() public payable {
//add to mapping and senders array
addressToAmountReceived[msg.sender] += msg.value;
senders.push(msg.sender);
}


modifier onlyOwner {
//is the message sender owner of the contract?
require(msg.sender == owner, "Only the owner can call this method!");

_; // withdraw() function will be inserted here
}

// onlyOwner modifer will first check the condition inside it
// and if true, withdraw function will be executed
function withdraw(uint _amount) external onlyOwner {
payable(msg.sender).transfer(_amount);
}function currentBalance() external view returns (uint) {
return address(this).balance;
}
}

Now, run the following command to compile the solidity code:

brownie compile

Setting up Infura

Infura is a platform that is used to connect application to Ethereum blockchain. In this article, you will use it to deploy the smart contract (Ethereum wallet in this case) so that it can run.

To get started, create an account with Infura, then open the Infura dashboard and click the “CREATE NEW PROJECT” button.

Give your project a name and submit it by clicking the “CREATE” button.

Open the project you just created and click on “PROJECT SETTINGS” button and copy the “PROJECT ID”. This will enable us to connect your local smart contract to Infura

Develop Deploy Script

Here you will write a Python script that will deploy the smart contract to Infura.

Brownie has a list of networks that it is compatible with, and Rinkeby is one of them. You can check the list of networks Brownie is compatible with by running Brownie networks list.

Before we get started with writing the code for deployment you need to set up some environment variables. At the root of your project, create a new file with the name .env and paste the text below. Replace 0xde6c2749e7da7b5e22f268c6a93xxxxxxxxxxxxxxxxxxxxxxxxx with the private keys of one of the MetaMask accounts for the Rinkeby network and 6a776acb7a5147xxxxxxxxxxxxxxxxxxx with the Infura project ID, you copied earlier.

export PRIVATE_KEY=0xde6c2749e7da7b5e22f268c6a93xxxxxxxxxxxxxxxxxxxxexport WEB3_INFURA_PROJECT_ID=6a776acb7a5147xxxxxxxxxxxxxxxxxxx

Now you need to make sure Brownie can access these environment variables. You can do this by going to the root of your project and creating a new file with the name brownie-config.yml. In the file you just created, paste the following code to tell Brownie where the PRIVATE_KEY is.

dotenv: .envwallets:  from_key: ${PRIVATE_KEY}

Now, in scripts/ create a new file with the name deploy.py and paste the code below. The following code is going to add the private key you stated in the .env file to Brownie then deploy and verify the smart contract (EtherWallet)

from brownie import EtherWallet, config, accountsdef deploy_wallet():    account = accounts.add(config["wallets"]["from_key"])    wallet = EtherWallet.deploy({"from":account}, publish_source=True)    print(f"Contract deployed to {wallet.address}")def main():    deploy_wallet()

Get API key from Etherscan to verify smart contracts

Before you can interact with your smart contract, you need to verify it. To do this directly in your code, you need an API key from Etherscan.

You can get the API key by registering and logging in to Etherscan. Then on your account page click on the hamburger icon and

Now click the “+Add” button to get the API key then copy it.

Now put the text below in your .env file, this will enable Brownie to verify your smart contract. Replace V5RUC7GU4xxxxxxxxxxxxxxx in the text below with the API key you just copied.

export ETHERSCAN_TOKEN=V5RUC7GU4xxxxxxxxxxxxxxx

Now to deploy, run the command below. Make sure you are at the root of your project.

brownie run scripts/deploy.py --networks rinkeby

When you run the command above you should see something like the statements below

Waiting for https://api-rinkeby.etherscan.io/api to process contract...
Verification submitted successfully. Waiting for result...
Verification pending...
Verification complete. Result: Pass - Verified
Contract deployed to 0x9fDe7BDe93fdCD4d8AA9D0cb386xxxxxxxxxx

Now copy 0x9fDe7BDe93fdCD4d8AA9D0cb386xxxxxxxxxx from the statement above and head over to Rinkeby Etherscan where you can test the smart contract. Or you can just easily open https://rinkeby.etherscan.io/address/<PasteTheAddressYouJustCopied> on your browser to try out your deployed Ethereum wallet

--

--

Technical Writer with experience in building awesome stuff with Django, Python, JavaScript, React, Kubernetes, etc. || Cloud-Native enthusiast.