Skip to content
Solidity Dependancy Management comes to Web3j
Alexandrou Rousou

Published On - December 11, 2020

Solidity Dependency Management comes to Web3j

At Web3 Labs, one of our core missions is making Ethereum development more accessible to the Java and Android communities. To this end, we’ve just added an awesome new feature to Web3j - it can now automatically resolve third-party contract dependencies for you. That’s right - no more copying and pasting code from OpenZepplin and other projects to your Java project environments - Web3j will do that for you! 
 
Following on from the success of the Solidity Gradle plugin which compiles the Solidity smart contracts in your Java project, earlier this year we added Sokt which eliminated the requirement of having the Solidity compiler installed on your system. Not only does Sokt install Solidity on your system and manage the installs, but it will also download the right version for your contract depending on the pragma version. 
 
We’ve now made it possible to import any Solidity libraries published as NPM packages. 
So, with this new feature, you will be able to compile a Solidity smart contract importing third-party libraries without the need to download them manually. For example, you wouldn’t need to download the OpenZeppelin library and do the correct mapping for the following to compile:

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract ERC20Token is ERC20 {
      constructor() ERC20("My Token", "MT") public {
      }
}

Then, using the Web3j plugin smart contract bindings can be generated to work with the contract from Java! 
 
The obvious question you may be asking is why we don’t just use Java‘s Maven dependency management. As it stands the majority of teams publishing smart contracts are using NPM, the most prominent example being OpenZeppelin. It simply wouldn’t be feasible to publish all of these artifacts as separate Java artifacts to Maven repositories, so the logical solution is to pull them down directly from NPM providing greater control for developers.

Quickstart

To apply the plugin in your project simply ensure you have the following line in your build.gradle file:

plugins {
    id "org.web3j.solidity" version "0.3.0"
}

Then you can incorporate code from another project using the following ‘@’ syntax.

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

If you're starting from scratch you can follow the example below.

Getting Started

  •  Create a new project by using epirus new or epirus import

Create a new project using epirus

  • Create a new smart contract under the main/solidity directory.

pragma solidity ^0.6.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyCollectible is ERC20 { 
      constructor() ERC20(“My Token”, “MT”) public {
      }
}

Create a new smart contract


  • Run the gradle build task. 

Gradle Build Task

 
That’s all, the plugin will take care of everything. Under the build directory, you should have all the dependencies you need. 
 

Dependencies under the build directory

 
Better yet, you could try using the OpenAPI plugin which would generate an OpenAPI compliant service without having to write any Java code yourself. 
 
We’d love to hear what you think of this new feature - it’s something that we’ve wanted for ages in Web3j and are really happy to have made it available. We hope it further supercharges your development with Web3j! 
Web3j, Solidity, Company News