Use Infura and MetaMask in Python Scripts to Deploy Web3 Smart Contracts on a Real Ethereum BlockChain Network

The business designs the Web3 smart contract logic based on its business model and needs. The next step for a company to think of is how to deploy this logic in a public blockchain network where people are staying and which they are using. Moreover, a script can smoothly interact with the blockchain network seamlessly no matter it’s for testing purposes or enhancing the user experience.

In this article, I will expand on how to use Infura, Metamask, and Python to make it function in your Web3.0. The piece would share the approach based on the previous Ganache script.

infura and metamask

The business designs the Web3 smart contract logic based on its business model and needs. The next step for a company to think of is how to deploy this logic in a public blockchain network where people are staying and which they are using. Moreover, a script can smoothly interact with the blockchain network seamlessly no matter it’s for testing purposes or enhancing the user experience.

In this article, I will expand on how to use Infura, Metamask, and Python to make it function in your Web3.0. The piece would share the approach based on the previous Ganache script.

Connect Web3.0 Smart Contracts to Ganache Using Python For Developing and Testing DApp

Ingredients to build a Python script to connect the Ethereum blockchain network using Infura, MetaMask

  • Python3, Web3, Infura, MetaMask wallet, Ganache Python script.

Table of Content

Infura

Different from Web2.0, there is no HTTP URL you can use directly and get you onto a Blockchain network, e.g Ethereum, in Web3.0. To access these networks natively and see the data being passed across them, you would have to install what’s known as a client. You do can develop an endpoint that can connect to Ethereum. Nevertheless, that would be much more complicated and cost you more. Under this context, this is why we recommend Infura. 

Infura provides an endpoint directly that your code can point to the Ethereum network so that your Web3.0 smart contract requests to receive or record information can always get to the network. Meanwhile, they return to you the information you’ve requested as well.

infura and metamask

As discussed in the previous article, we use the web3 module to connect our local HTTP protocol. So if we like to connect to the real Ethereum network, Infura can provide us with an HTTP protocol with the API key that is used in this Python script.

For example, our live test network is Goerili (You can select Mainnet if it’s not a test). So we select the network in Infura and copy and paste the HTTPS protocol to our script. Here is the code as follows:

# for connecting to Ethereum network
w3 = Web3(Web3.HTTPProvider(
"https://your http address"))

MetaMask ID Address & Private Key

Without a doubt, MetaMask is the most popular wallet currently used in Web3.0. With 21 million+ monthly active users, its user-friendly interface enables the ability to interact with the blockchain while having an account back up with a seed phrase. All in all, if you are developing a DApp, using MetaMask to test is notably the most proper because it ensures MetaMask login and payment can work in your DApp.

Moreover, MetaMask is configurable with Infura. Thus, your Web3 smart contracts built in the DApp can connect with MetaMask through Infura.

For the wallet address, we need to copy the MetaMask account ID (Above the number under the Easy2Digital title) and paste it to my_address variable in the Python script.

my_address = "your metamask ID address"
private_key = os.getenv("PRIVATE_KEY2")
 
We also need to update our MetaMask account private key in our .env file. Please remember to update the source information in the terminal using this command.
 
source .env

Type of Blockchain Network ID

Each Blockchain network has a unique ID. For example, our test case uses the Goerili network and the chain ID is 5.

chain_id = 5
 
infura and metamask

We can not only go to chainlist.org to look up your network ID, but also we can run the Python script, and then the response would give you feedback on what the Chain ID should be from the network now you are accessed.

Full Python Script of Ethereum BloackNetwork Connection Using Infura and MetaMask

If you are interested in Web3.0 tutorial 2 – Use Infura and Metamask in Python Scripts to Deploy Web3 Smart Contracts on a Real Ethereum BlockChain Network, please subscribe to our newsletter by adding the message “Web3.0 tutorial 2”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Web3.0 tutorial 2 – Use Infura and Metamask in Python Scripts to Deploy Web3 Smart Contracts on a Real Ethereum BlockChain Network. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

1 thought on “Use Infura and MetaMask in Python Scripts to Deploy Web3 Smart Contracts on a Real Ethereum BlockChain Network

  1. An outstanding share! I’ve just forwarded this to my team, thanks for spending time talking about this subject.

Comments are closed.