Convert Figma logo to code with AI

hyperledger logoweb3j

Lightweight Java and Android library for integration with Ethereum clients

5,065
1,674
5,065
137

Top Related Projects

19,150

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.

14,020

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.

Complete Ethereum library and wallet implementation in JavaScript.

OpenZeppelin Contracts is a library for secure smart contract development.

Quick Overview

Web3j is a lightweight, highly modular, reactive, type-safe Java and Android library for working with Smart Contracts and integrating with clients (nodes) on the Ethereum network. It enables you to work with Ethereum, without the additional overhead of having to write your own integration code for the platform.

Pros

  • Easy integration with Ethereum blockchain
  • Type-safe wrapper generation for smart contracts
  • Supports both Java and Android platforms
  • Reactive-functional API for asynchronous composability

Cons

  • Learning curve for developers new to blockchain technology
  • Limited to Ethereum ecosystem
  • Dependency on external Ethereum nodes for full functionality
  • May require frequent updates to keep up with Ethereum protocol changes

Code Examples

  1. Connecting to an Ethereum client:
Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR-PROJECT-ID"));
  1. Sending Ether:
TransactionReceipt transactionReceipt = Transfer.sendFunds(
    web3j, credentials,
    "0x19e03255f667bdfd50a32722df860b1eeaf4d635",  // address
    BigDecimal.valueOf(1.0), Convert.Unit.ETHER)  // 1 Ether
    .send();
  1. Deploying a smart contract:
YourSmartContract contract = YourSmartContract.deploy(
    web3j, credentials,
    GAS_PRICE, GAS_LIMIT,
    <param1>, <param2>).send();
  1. Calling a smart contract function:
TransactionReceipt receipt = contract.someFunction(<param1>, <param2>).send();

Getting Started

  1. Add Web3j dependency to your project:
dependencies {
    implementation 'org.web3j:core:4.9.4'
}
  1. Create a Web3j instance:
Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR-PROJECT-ID"));
  1. Load credentials:
Credentials credentials = WalletUtils.loadCredentials("password", "/path/to/walletfile");
  1. Interact with the Ethereum network:
EthGetBalance balanceWei = web3.ethGetBalance("0x...", DefaultBlockParameterName.LATEST).send();
BigInteger balance = balanceWei.getBalance();

Competitor Comparisons

19,150

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.

Pros of web3.js

  • Larger community and more extensive ecosystem
  • More comprehensive documentation and examples
  • Better support for browser-based applications

Cons of web3.js

  • JavaScript-only, limiting language options
  • Less type safety compared to Java-based web3j
  • Potentially more challenging to integrate with enterprise Java applications

Code Comparison

web3.js (JavaScript):

const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR-PROJECT-ID');

web3.eth.getBalance('0x1234567890123456789012345678901234567890')
  .then(console.log);

web3j (Java):

Web3j web3 = Web3j.build(new HttpService("https://mainnet.infura.io/v3/YOUR-PROJECT-ID"));

EthGetBalance balance = web3.ethGetBalance("0x1234567890123456789012345678901234567890", DefaultBlockParameterName.LATEST).send();
System.out.println(balance.getBalance());

Both libraries provide similar functionality for interacting with Ethereum networks, but web3.js is more popular and better suited for JavaScript environments, while web3j offers stronger typing and integration with Java ecosystems.

14,020

:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.

Pros of Truffle

  • More comprehensive development environment with built-in testing framework and asset pipeline
  • Larger ecosystem and community support, with extensive documentation and plugins
  • Simpler setup and configuration for beginners

Cons of Truffle

  • JavaScript-centric, which may not be ideal for developers preferring other languages
  • Can be slower for large projects due to its all-in-one nature
  • Less flexibility for custom build processes compared to more modular alternatives

Code Comparison

Truffle contract deployment:

const MyContract = artifacts.require("MyContract");

module.exports = function(deployer) {
  deployer.deploy(MyContract);
};

Web3j contract deployment:

YourSmartContract contract = YourSmartContract.deploy(
    web3j, credentials, gasPrice, gasLimit,
    <constructor parameters>
).send();

Summary

Truffle offers a more comprehensive and user-friendly development environment for Ethereum smart contracts, particularly suited for JavaScript developers and beginners. It provides a full suite of tools including testing, deployment, and asset management. Web3j, on the other hand, is a Java library for interacting with Ethereum, offering more flexibility for Java developers but requiring more setup and external tools for a complete development workflow. The choice between the two depends on the preferred programming language, project requirements, and desired level of control over the development process.

Complete Ethereum library and wallet implementation in JavaScript.

Pros of ethers.js

  • Written in TypeScript, providing better type safety and developer experience
  • More comprehensive documentation and examples
  • Lighter weight and faster performance

Cons of ethers.js

  • Less integration with enterprise blockchain solutions
  • Smaller ecosystem compared to Web3j's Java-based environment

Code Comparison

ethers.js:

const provider = new ethers.providers.JsonRpcProvider();
const signer = provider.getSigner();
const contract = new ethers.Contract(address, abi, signer);
const result = await contract.someFunction();

Web3j:

Web3j web3 = Web3j.build(new HttpService());
Credentials credentials = WalletUtils.loadCredentials(password, walletFile);
MyContract contract = MyContract.load(address, web3, credentials, gasPrice, gasLimit);
TransactionReceipt result = contract.someFunction().send();

Summary

ethers.js is a lightweight, TypeScript-based library for Ethereum interactions, offering excellent documentation and performance. It's ideal for web-based dApps and modern JavaScript environments. Web3j, on the other hand, is Java-based and better suited for enterprise blockchain solutions with stronger integration capabilities in Java ecosystems. The choice between the two depends on the specific project requirements, programming language preference, and target environment.

OpenZeppelin Contracts is a library for secure smart contract development.

Pros of openzeppelin-contracts

  • Extensive library of secure, audited smart contract components
  • Widely adopted and battle-tested in production environments
  • Regular updates and active community support

Cons of openzeppelin-contracts

  • Focused solely on Ethereum and EVM-compatible chains
  • May require more in-depth Solidity knowledge for customization

Code Comparison

openzeppelin-contracts (Solidity):

contract MyToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
        _mint(msg.sender, initialSupply);
    }
}

web3j (Java):

ERC20 token = ERC20.deploy(
    web3j, credentials, gasPrice, gasLimit,
    "MyToken", "MTK", BigInteger.valueOf(1000000)
).send();

Key Differences

  • openzeppelin-contracts provides Solidity smart contract templates and libraries
  • web3j is a Java library for interacting with Ethereum and smart contracts
  • openzeppelin-contracts focuses on contract development, while web3j emphasizes blockchain interaction and integration

Use Cases

  • openzeppelin-contracts: Developing secure smart contracts on Ethereum
  • web3j: Building Java applications that interact with Ethereum networks

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Web3j: Web3 Java Ethereum Ðapp API

Documentation Status build status codecov Discord

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

image

This allows you to work with the Ethereum blockchain, without the additional overhead of having to write your own integration code for the platform.

The Java and the Blockchain talk provides an overview of blockchain, Ethereum and Web3j.

NEW! Get involved!

Since Web3J moved under Hyperledger we started to do Web3J Contributors calls every 2 weeks! Subscribe to our community page and to see check our call schedule. Your contribution matters!

Features

It has five runtime dependencies:

It also uses JavaPoet for generating smart contract wrappers.

QuickStart

The simplest way to start your journey with Web3j is to create a project. We provide this functionality using the Web3j CLI. This latter can be installed as follows:

For Unix:

curl -L get.web3j.io | sh && source ~/.web3j/source.sh

For Windows, in Powershell:

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://raw.githubusercontent.com/web3j/web3j-installer/master/installer.ps1'))

Create a new project by running:

$ web3j new 

Or use our Maven or Gradle plugins to generate java files from Solidity contracts.

Please head to the Web3j Documentation for further instructions on using Web3j.

Maven

Java:

<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>4.12.1</version>
</dependency>

Note: The Web3j Java binaries are compiled using Java 17. Java 17 or a more recent version is required to use Web3j as a dependency.

Android:

<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>4.8.9-android</version>
</dependency>

Gradle

Java:

implementation ('org.web3j:core:4.12.1')

Android:

implementation ('org.web3j:core:4.8.9-android')

Build instructions

Web3j includes integration tests for running against a live Ethereum client. If you do not have a client running, you can exclude their execution as per the below instructions.

To run a full build (excluding integration tests):

$ ./gradlew check

To run the integration tests, you will need to set up these variables in order to pull the Docker images from the Docker Hub registry:

  • registry.username
  • registry.password

Then run the following command:

$ ./gradlew -Pintegration-tests=true :integration-tests:test

If you do not want the integration test to run:

$ ./gradlew -Pintegration-tests=false :test

Check the Docker client API for more information on configuration options.

Commercial support and training

Commercial support and training is available from web3labs.com.

License

Apache 2.0