Skip to main content

Enterprises all over the world use Java to build scalable systems which we use in our daily lives. Today, we will talk about Web3j – a library written in Java for Ethereum developers!

What is Web3j?

Web3j Java library to expose Ethereum JSON RPC APIs. You can use it for building web, mobile, or any other type of applications. The web3j documentation describes:

web3j is a highly modular, reactive, type safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network:

What does Web3j support?

Web3j has rich support of Ethereum JSON-RPC:

  • Complete implementation of Ethereum’s JSON-RPC client API over HTTP and IPC
  • Ethereum wallet support
  • Auto-generation of Java smart contract wrappers to create, deploy, transact with and call smart contracts from native Java code (Solidity and Truffle definition formats supported)
  • Reactive-functional API for working with filters
  • Ethereum Name Service (ENS) support
  • Support for Parity’s Personal, and Geth’s Personal client APIs
  • Support for ERC20 and ERC721 token standards
  • Comprehensive integration tests demonstrating a number of the above scenarios
  • Command-line tools
  • Android compatible
  • Support for JP Morgan’s Quorum via web3j-quorum

Additionally, it also provides ENS and Wallet support 😎.

Moreover, it supports Quorum: a blockchain framework created by JPMorgan (Chase, the bank!).

Web3j Examples

Let’s set up a project and deep-dive into the code. We will see some basic Ethereum use-cases and how we can use Web3j to build Ethereum applications in Java.

Prerequisite

  • Java ≥ version 8
  • Maven
  • Any Java IDE (I am using IntelliJ Community version)
  • Solc (Solidity compiler)
  • A QuickNode (QuickNode.com)
  • Web3j command-line tools

The SimpleBank.sol Smart Contract

We have created a SimpleBank Smart Contract which emits a Deposit event when someone deposits in our Bank.

pragma solidity ^0.5.8;

contract SimpleBank {
    uint8 private clientCount;
    mapping (address => uint) private balances;
    address public owner;

    event LogDepositMade(address indexed accountAddress, uint amount);

    constructor() public payable {
        owner = msg.sender;
        clientCount = 0;
    }


    function enroll() public returns (uint) {
        if (clientCount < 3) {
            clientCount++;
            balances[msg.sender] = 10 ether;
        }
        return balances[msg.sender];
    }


    function deposit() public payable returns (uint) {
        balances[msg.sender] += msg.value;
        emit LogDepositMade(msg.sender, msg.value);
        return balances[msg.sender];
    }


    function withdraw(uint withdrawAmount) public returns (uint remainingBal) {
        // Check enough balance available, otherwise just return balance
        if (withdrawAmount <= balances[msg.sender]) {
            balances[msg.sender] -= withdrawAmount;
            msg.sender.transfer(withdrawAmount);
        }
        return balances[msg.sender];
    }


    function balance() public view returns (uint) {
        return balances[msg.sender];
    }

    function depositsBalance() public view returns (uint) {
        return address(this).balance;
    }
}

 

Leave a Reply