How to Set up a Hosted Ethereum Node in Windows OS using Web3.py and InfurasteemCreated with Sketch.

in #ethereum6 years ago (edited)

If you want to interact with the Ethereum blockchain to get the latest block's details, mine Ethereum, information of a transaction or send a transaction you need to connect to the Ethereum blockchain using a node either local or hosted node. Since there are many third party plugins and apps available to do this, you may think why do we actually need a node. If you want to deploy a smart contract or interact with a smart contract automatically using a script you need to set up a node and this may not possible with the current thrid party plugins or apps.

If you choose to set up a local node using a client like geth, it will sync the blockchain to your computer which will eat up your disk storage. However, this can be eliminated by setting up a hosted node where somebody controls the node but you have the freedom to connect to the Ethereum blockchain and play with it. Infura is such an service that helps to set up a local node.

Here I am going to explain how to set up a local node for a 64-bit Windows operating system. Please note that this tutorial is for 64-bit Windows 8.1 version. However, I believe that there may no major differences in the other Windows versions such as Windows 10, Windows 8, Windows 7 and Windows Vista. 

First of all, I will list down the steps assuming that you haven't installed the dependencies such as NPM and Python. Codes and explanations are mentioned later part of this post.

1. Install CMDER (Not mandatory)

2. Install Node.js and NPM

3. Install Windows build tools 

4. Install Python Latest Version

5.  Install Web3.py

6. Install iPython

7. Create a infura.io account 

8. Enabling to Interact with Ethereum Blockchain


Let's begin the detailed explanation including codes.


Step 1 -  Install CMDER

Actually this is a replacement for the traditional Windows command prompt. I personally don't like the command prompt as it lacks of options and less attractive user interface. You can go ahead with the command prompt if you wish or download the CMDER to any folder in the C drive. 

Visit http://cmder.net/ to download CMDER

After you downloaded just double click the cmder.exe file to open the CMDER (It's not an installation file, it's the program). You will be asked to grant permission to make changes in the computer when you run CMDER for the first time. Just grant permission. Then you are ready to use CMDER.


Step 2 - Install Node.js and NPM

Go to the official Node.js website and download Node.js. Then install it. NPM will be automatically installed along with Node.js. NPM will help us to download and install the windows build tools. Furthermore, Node.js and NPM will help us to do lot of things with the Ethereum blockchain such as building scripts.

Node.js website: https://nodejs.org/en/

After installation open CMDER and check whether they are working properly by entering following codes.

node -v

npm -v

You should be able to see the version number of each program as the result.

Most of the times NPM version you get could be outdated as this get updated frequently. Therefore use the following command in CMDER to upgrade NPM to the latest version.

npm install npm@latest -g


Step 3 - Install Windows build tools

By installing windows build tools you will be able to create the necessary environment to run web3.py which helps to interact with the Ethereum blockchain.  So let's do it.
First open the Windows Powershell with the administrator mode. (Don't use command prompt or CMDER as it will give you errors)

Now we need to enter the following code and hit Enter to install these Windows build tools.

npm install --global --production windows-build-tools 

This may take few minutes to complete.


NPM will install Python 2.7 in this step. However, we need Python 3.5 or later version in order to use Web3.py 


Step 4 - Install Python Latest Version

Go to official Python website and download the latest 3.x executable version for Windows and run the set up file.

Python downloads page : https://www.python.org/downloads/windows/

Tick Add Python to PATH option and Install launcher for all users options in the Python installation step.

Tick pip and py launcher options in the next step and install Python.


Both Python and pip should work properly to continue with Web3.py installation. Therefore, check entering the following commands in  CMDER

python -v

It will run Python and should show something like below.

You can use the following code to exit from Python

exit()

Now let's see whether pip works properly.


Use the follwing code in CMDER

pip -v

If you get a response like shown below it works properly.


Step  5 - Install Web3.py

You can easily install web3.py using the follwing command in CMDER

pip install web3


Step 6 - Install iPython

We need  a python interpreter such as ipython to run the web3.py codes. Let's install it.

Enter the following command in CMDER

pip install ipython


Step 7 - Create an Infura Account

Now we need to create an account at Infura in order to connect to the Ethereum blockchain.

Visit infura.io and sign up for an account for free. Once you verified your email address you will be able to log in to dashboard. Now all you need is the URL which is available under the ENDPOINT section.  If you need to connect to main net select that option and copy that URL. 


Step 8 - Enabling to Interact with Ethereum Blockchain

Once you got the URL from Infura. Now open CMDER and type the following codes.

ipython

from web3 import Web3

web3 = Web3(Web3.HTTPProvider("https://mainnet.infura.io/xxxxxxxxxxxxxxxxx"))

(Replace the URL with your personal infura URL)

If there is no error occurred after ruining above commands, cheers you can now interact with the Ethereum blockchain.


Let's test whether we can interact with the Ethereum blockchain.

Let's enter the following code to get the latest block number.

web3.eth.blockNumber  


You should get the latest block number as the result like shown below.

This means you can now interact with the Ethereum blockchain now.

You can use the follwing command to exit from ipython

exit()

Please note that you have to use the first three commands of this step again each time when you re-run ipython.


I hope that this tutorial covered the basic steps to set up a hosted ethereum node in Windows OS via Infura.


Other Useful Links

Node.js documentation : https://nodejs.org/en/docs/

NPM documentation : https://docs.npmjs.com/

Python 3.x documentation : https://docs.python.org/3/

Web3.py documentation : http://web3py.readthedocs.io/en/stable/