1

This might be a silly question. if one is able to call JSON RPC API methods such as eth_sendTransaction directly using Postman or any web client, albeit a little troublesome, why use web3?

I have been using web3 js in my DApp. Asking this for understanding.

https://github.com/ethereum/wiki/wiki/JSON-RPC

  • The same reasons you would use any other library. I guess the main benefit is that it saves you from having to write and test the code yourself. – user19510 Jun 14 '18 at 6:11
  • there might be cases where direct call to JSON RPC API is better as I am trying to integrate quorum with some existing systems which might support Ethereum JSON RPC API calls. If web3 is needed, I need to build a DApp and expose these methods as an endpoint thereby creating another middle tier. What do you think? I really wish to understand if there is a better way to put things together. – Nathan Aw Jun 14 '18 at 6:30
3

As you said, you can do everything without web3. Most users just don't find it easy to do most of the things web3 does for you.

So web3 is just a wrapper library which provides easy-to-use access to various functionalities. It makes development a lot easier for those who are not hardcore blockchain enthusiasts (who don't want to write RPC calls). In doing that, it lowers the entry barrier into Ethereum world.

I could also write this answer with on-screen keyboard but it's just a lot easier to write with a real keyboard.

| improve this answer | |
  • there might be cases where direct call to JSON RPC API is better as I am trying to integrate quorum with some existing systems which might support Ethereum JSON RPC API calls. If web3 is needed, I need to build a DApp and expose these methods as an endpoint thereby creating another middle tier. What do you think? I really wish to understand if there is a better way to put things together. – Nathan Aw Jun 14 '18 at 6:31
  • Nobody forces you to create a dapp or anything public. You can just use web3 with any environment which can run javascript - such as node. Check for example: medium.com/@codetractio/… – Lauri Peltonen Jun 14 '18 at 6:34
1

Like any other library, Web3.js is just trying to make things simple for you, the app developer.

You do not need to use it. In fact, you could literally rewrite all the work it does in your own code, but that would be an incredible waste of time.

Instead, Web3.js enables app developers to build things quickly and simply.

For example, get the balance of an Ethereum Address in 40 lines:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script type="text/javascript" src="./web3.min.js"></script>
    <script type="text/javascript">
        window.addEventListener('load', function () {
            if (typeof web3 !== 'undefined') {
                console.log('Web3 Detected! ' + web3.currentProvider.constructor.name)
                window.web3 = new Web3(web3.currentProvider);
            } else {
                console.log('No Web3 Detected... using HTTP Provider')
                window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/<APIKEY>"));
            }
        })
        function getBalance() {
            var address, wei, balance
            address = document.getElementById("address").value
            try {
                web3.eth.getBalance(address, function (error, wei) {
                    if (!error) {
                        var balance = web3.fromWei(wei, 'ether');
                        document.getElementById("output").innerHTML = balance + " ETH";
                    }
                });
            } catch (err) {
                document.getElementById("output").innerHTML = err;
            }
        }
    </script>
</head>
<body>
    <h1>ETH Balance Fetcher</h1>
    <p>Enter your Ethereum Address:</p>
    <input type="text" size="50" id="address" />
    <button type="button" onClick="getBalance();">Get Balance</button>
    <br />
    <br />
    <div id="output"></div>
</body>
</html>

Imagine having to implement:

  • Connecting to a Web3 provider
  • Integrating the getBalance function
  • Converting from Wei to Ether
  • etc...
| improve this answer | |
  • there might be cases where direct call to JSON RPC API is better as I am trying to integrate quorum with some existing systems which might support Ethereum JSON RPC API calls. If web3 is needed, I need to build a DApp and expose these methods as an endpoint thereby creating another middle tier. What do you think? I really wish to understand if there is a better way to put things together. – Nathan Aw Jun 14 '18 at 6:30
0

Web3 is just a javascript wrapper for the JSON RPC API which can be used to interact with your ethereum node without writing raw JSON RPC calls.

This doesn't mean that web3 is required, you can use other library (ethers.js - still javascript) or do raw call in case the language your using to develop your DApp doesn't have any JSON RPC client for Ethereum.

So in case you're doing something standard just use the libraries already created by the community, if not you can create your own in case there's nothing or you're doing something custom (eg. your private blockchain with custom RPC API in a controlled environment)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.