How to communicate with public Ethereum Blockchain on a web server?
Asked Answered
B

1

6

In the backend of a Web application I have to communicate with the public Ethereum Blockchain.

On local development machine, I run ganache as testrpc and connect with such a line of code:

web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

I've also found out that I can develop against RinkeBy testnet with

web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/$thisistheapikey"));

But how can I communicate with the REAL public Blockchain? Do I need to run geth on the Web server and connect to its instance? Or is there any public network available that could be used? (if we can trust it)

Berri answered 13/5, 2019 at 15:2 Comment(1)
https://mainnet.infura.io/$thisistheapikeyBlimp
T
6

But how can I communicate with the REAL public Blockchain?

In order to connect to Ethereum Public Blockchain (an Ethereum node) with remote procedure call (RPC) on the Main Net, You need an Ethereum node. There are a few ways to do this. You can run your own Ethereum node with Geth or Parity. But this requires downloading a lot of data from the public blockchain and keeping it in sync. That's a huge thing to do.

Alternatively, you can use Infura (https://infura.io/) to access an Ethereum node (Ethereum Public Blockchain) without having to run any node by yourself. Infura provides a remote Ethereum node for free. All you need to do is sign up and obtain an API key and the RPC URL to connect.

The Infura RPC URL should look like this:

https://mainnet.infura.io/YOUR_INFURA_API_KEY  

Now you can use this RPC URL to communicate, like

const Web3 = require('web3')
const rpcURL = '' // Your RPC URL with infura key goes here,i.e. https://mainnet.infura.io/YOUR_INFURA_API_KEY 
const web3 = new Web3(rpcURL)
const address = '' // Your ethereum account address goes here
web3.eth.getBalance(address, (err, wei) => {
  balance = web3.utils.fromWei(wei, 'ether')
})

Do I need to run geth on the Web server and connect to its instance?

Already covered in the first answer, this can be another approach to communicate.

Or is there any public network available that could be used? (if we can trust it)

There are ethereum Main Net where the real token transaction occurs and Test Net that carry no real-world value. Before a project launches on the Ethereum blockchain it's best to run the whole scenario in a Test Net environment to find and fix security concerns. There are lots of test net services available. Like Ropsten, Kovan, Rinkeby etc. Just search the internet for "ethereum mainnet testnet" to learn more. Hope helps.  

Tali answered 17/5, 2019 at 6:53 Comment(3)
But the results of the infura mainnet does not match with those displayed in etherscan.io. Could you pls help me out, how these things work and why do they show different results.Fairlie
@MaadhavSharma You are looking at the mainnet or testnetYellowgreen
I think I was looking at the mainnet at that time, eventually I used cloudflare.com/distributed-web-gateway/#ethereum-gatewayFairlie

© 2022 - 2024 — McMap. All rights reserved.