The wrapper generator

Before using web3j, we need to build the Java wrapper class for our smart contract. The web3j command-line tool supports the autogeneration of Java smart contract function wrappers directly from Truffle's contract schema, as follows:

web3j truffle generate ~/FuturesWeb3j/build/contracts/CFutures.json -o ~/eclipse-workspace/futures/src/main/java/ -p org.chapter6.futures

The first argument is the build file compiled by Truffle for the CFutures contract, and the second is the path of your Java project destination where the class wrapper will be generated. In addition, the -p parameter should be the Java packaging structure for the generated class:

This will result in creating a CFutures.java file with a new class with the same name as the smart contract:

public class CFutures extends Contract {
private static final String BINARY = "0x60606040523415600e576...";
}

As you can see, the BINARY string represents your compiled contract (bytecode). Remove the and , if there are any. You'll find in this class a Java implementation for all of your smart contract methods.

Remember that if you make any changes, you'll have to compile the smart contract and then generate the new ABI, bytecode, and web3j wrapper as we did previously.

Now that we have our smart contract class wrapper, let's move on and write our Futures main class. We have to write a Java class that deploys and interacts with the Cfutures smart contract using the Futures class wrapper.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset