Generate Java interface files for smart contracts

Tools are provided in the console and java-sdk-demo. You can call the java tool class of the ‘solidity’ contract。In this example, use “console” to generate the Solidity contract to call the contract ‘java’ tool class as an example。

To experience the webankblockchain-liquid (WBC-Liquid), please refer to Section 5。

For an example of using “java-sdk-demo,” see Section 7。

1. Download Console

$ mkdir -p ~/fisco && cd ~/fisco
# Get Console
$ curl -#LO https://github.com/FISCO-BCOS/console/releases/download/v3.6.0/download_console.sh

# If the preceding command cannot be executed for a long time due to network problems, try the following command:
$ curl -#LO https://gitee.com/FISCO-BCOS/console/raw/master/tools/download_console.sh

$ bash download_console.sh
$ cd ~/fisco/console

2. Place the contract in the contract directory of the console

Then, place the Solidity smart contract you want to use in the “~ / fisco / console / contracts / solidity” directory。This time we use HelloWorld.sol in the console as an example。Ensure that HelloWorld.sol is in the specified directory。

# Current Directory ~ / fisco / console
$ ls contracts/solidity 

get back

HelloWorld.sol  KVTableTest.sol ShaTest.sol KVTable.sol ...

3. Generate the Java class that calls the smart contract

# Current Directory ~ / fisco / console
$ bash contract2java.sh solidity -p org.com.fisco -s ./contracts/solidity/HelloWorld.sol
# The parameter "org.com.fisco" in the above command is the package name to which the generated java class belongs。
# The command. / contract2java.sh -h allows you to view how the script is used

get back

*** Compile solidity HelloWorld.sol*** 
INFO: Compile for solidity HelloWorld.sol success.
*** Convert solidity to java  for HelloWorld.sol success ***

The use of ‘contract2java.sh’ will be described in detail in Appendix 2。

View Compilation Results

$ ls contracts/sdk/java/org/com/fisco 
# get back
# HelloWorld.java

After running successfully, the java, abi, and bin directories will be generated in the ‘console / contracts / sdk’ directory, as shown below。

|-- abi # The abi directory generated by compilation, which stores the abi file compiled by the solidity contract
|   |-- HelloWorld.abi
|-- bin # The bin directory generated by compilation, which stores the bin file compiled by the Solidity contract
|   |-- HelloWorld.bin
|-- java  # Store the compiled package path and Java contract file
|   |-- org
|       |-- com
|           |-- fisco
|               |-- HelloWorld.java # Solidity Compiled HelloWorld Java File

The ‘org / com / fisco /’ package path directory is generated in the Java directory。The Java contract file ‘HelloWorld.java’ will be generated in the package path directory。where ‘HelloWorld.java’ is the Java contract file required by the Java application。

4. The generated Java file code structure

The following takes the generated interface list of ‘HelloWorld.java’ as an example to briefly explain the code structure。

public class HelloWorld extends Contract {
    / / constructor
    protected HelloWorld(String contractAddress, Client client, CryptoKeyPair credential);
    / / Obtain the code of the contract according to the CryptoSuite. If the code is the national secret, return the code of the national secret
    public static String getBinary(CryptoSuite cryptoSuite);
    / / Get the ABI json string of the contract
    public static String getABI();
    / / HelloWorld contract get interface
    public String get() throws ContractException;
    / / The Function class of the HelloWorld contract get interface, which records the input and return types and can be used for ABI parsing
    public Function getMethodGetRawFunction() throws ContractException;
    / / The HelloWorld contract set interface. Enter the string type and return the transaction receipt
    public TransactionReceipt set(String n);
    / / The Function class of the HelloWorld contract set interface, which records the input and return types and can be used for ABI parsing
    public Function getMethodSetRawFunction(String n) throws ContractException;
    / / Obtain the signed transaction calling the set interface, which can be sent directly to the chain
    public String getSignedTransactionForSet(String n);
    / / The HelloWorld contract set asynchronous interface. Enter the string type and return the transaction hash
    public String set(String n, TransactionCallback callback);
    / / Input parsing of the HelloWorld contract set
    public Tuple1<String> getSetInput(TransactionReceipt transactionReceipt);
    / / If there is a known HelloWorld contract on the chain, you can directly load the Java HelloWorld class using the modified interface。Note: ABI must be the same, otherwise the call fails
    public static HelloWorld load(String contractAddress, Client client, CryptoKeyPair credential);
    / / Initiate the deployment contract operation on the chain and return the Java HelloWorld class。
    public static HelloWorld deploy(Client client, CryptoKeyPair credential) throws ContractException;
}

5. Generate the Java interface file for the WBC-Liquid contract

Similar to the Solidity contract above, if you want to experience the deployment of webankblockchain-liquid (hereinafter referred to as WBC-Liquid), the console also provides you with examples。

Before using it, ensure the compilation environment of the cargo liquid. For details about how to use it, see: https://liquid-doc.readthedocs.io/。

5.1 Compilation of the WBC-Liquid Contract

You can view it under contracts / liquid in the dist directory of the console. The following is an example of hello _ world:

$ ls contracts/liquid
asset_test    hello_world   kv_table_test

$ cd contracts/liquid/hello_world

# Compiling with cargo liquid
$ cargo liquid build
[1/4] 🔍  Collecting crate metadata
[2/4] 🚚  Building cargo project
[3/4] 🔗  Optimizing Wasm bytecode
[4/4] 📃  Generating ABI file

✨ Done in 1 minute, your project is ready now:
Binary: ~/fisco/contracts/liquid/hello_world/target/hello_world.wasm
   ABI: ~/fisco/console/dist/contracts/liquid/hello_world/target/hello_world.abi

Generate ‘hello _ world.wasm’ and ‘hello _ world.abi’ files

5.2 WBC-Liquid contract generates Java files

# Current Directory ~ / fisco / console
$ bash contract2java.sh liquid -b ./contracts/liquid/hello_world/hello_world.wasm -a ./contracts/liquid/hello_world/hello_world.abi -s ./contracts/liquid/hello_world/hello_world_sm.wasm -p org.com.fisco
# The command. / contract2java.sh -h allows you to view how the script is used

$ ls contracts/sdk/java/org/com/fisco 
# get back
HelloWorld.java

6. contract2java.sh script parsing

The console provides a special tool for generating Java contracts, which allows developers to compile Solidity and WBC-Liquid contract files into Java contract files。

The current contract generation tool supports automatic compilation and generation of Java files by Solidity, WASM files compiled by specified WBC-Liquid, and Java files generated by ABI files。

6.1 Solidity Contract Use

bash contract2java.sh solidity -h
usage: contract2java.sh <solidity|liquid> [OPTIONS...]
 -e,--enable-async-call           [Optional] Enable generate async
                                  interfaces for constant call, java file
                                  only compilable when java-sdk >= 3.3.0.
 -h,--help
 -l,--libraries <arg>             [Optional] Set library address
                                  information built into the solidity
                                  contract
                                  eg:
                                  --libraries lib1:lib1_address
                                  lib2:lib2_address
 -n,--no-analysis                 [Optional] NOT use evm static
                                  parallel-able analysis. It will not
                                  active DAG analysis, but will speedup
                                  compile speed.
 -o,--output <arg>                [Optional] The file path of the
                                  generated java code, default is
                                  contracts/sdk/java/
 -p,--package <arg>               [Optional] The package name of the
                                  generated java code, default is com
 -s,--sol <arg>                   [Optional] The solidity file path or the
                                  solidity directory path, default is
                                  contracts/solidity/
 -t,--transaction-version <arg>   [Optional] Specify transaction version
                                  interface, default is 0; If you want to
                                  use the latest transaction interface,
                                  please specify 1.

Detailed parameters:

  • package: Generate the package name of the ‘Java’ file。

  • sol: (Optional)The path of the ‘solidity’ file. Two methods are supported: file path and directory path. When the parameter is a directory, all the ‘solidity’ files in the directory are compiled and converted。The default directory is’ contracts / solidity’。

  • output: (Optional)The directory where the ‘Java’ file is generated. By default, it is generated in the ‘contracts / sdk / java’ directory。

  • ‘no-analysis’: (optional) Skip the static analysis of solidity compilation, which can effectively reduce the compilation speed。Static analysis can analyze the parallel feasibility of the contract interface and put the results of the analysis into the abi file。

  • ‘enable-async-call’: (Optional) You can generate a Java file with an asynchronous call interface>= 3.3.0 when used。

  • ‘transaction-version’: (Optional) Specifies the version number of the generated Java file for sending transactions. The default value is 0, which is compatible with all versions of nodes;When the transaction version is 1, transactions with fields such as value, gasLimit, gasPrice, and EIP1559 can only be sent to nodes of 3.6.0 and above。

6.2 WBC-Liquid Contract Use

$ bash contract2java.sh liquid -h
usage: contract2java.sh <solidity|liquid> [OPTIONS...]
 -a,--abi <arg>       [Required] The ABI file path of WBC-Liquid contract.
 -b,--bin <arg>       [Required] The binary file path of WBC-Liquid contract.
 -h,--help
 -o,--output <arg>    [Optional] The file path of the generated java code,
                      default is contracts/sdk/java/
 -p,--package <arg>   [Optional] The package name of the generated java
                      code, default is com
 -s,--sm-bin <arg>    [Required] The SM binary file path of WBC-Liquid
                      contract.

Detailed parameters:

  • ‘abi’: (Required) The path of the WBC-Liquid contract ‘ABI’ file, which is generated in the target folder after using the ‘cargo liquid build’ command。

  • ‘bin’: (Required) The path of the WBC-Liquid contract ‘wasm bin’ file, which is generated in the target folder after using the ‘cargo liquid build’ command。

  • ‘package’: (Optional) The package name of the generated ‘Java’ file, which is’ org ‘by default。

  • ‘sm-bin’: (Required) The path of the WBC-Liquid contract ‘wasm sm bin’ file, which is generated in the target folder after using the ‘cargo liquid build -g’ command。

7. Use “java-sdk-demo” to generate the Java tool class that calls the smart contract

$ mkdir -p ~/fisco && cd ~/fisco
# get java-sdk code
$ git clone https://github.com/FISCO-BCOS/java-sdk-demo

# If the preceding command cannot be executed for a long time due to network problems, try the following command:
$ git clone https://gitee.com/FISCO-BCOS/java-sdk-demo

$ cd java-sdk-demo
# Compile
$ ./gradlew clean build -x test
# Enter the sdk-demo / dist directory and create a contract storage directory
$ cd dist && mkdir -p contracts/solidity
# Copy the sol file that needs to be converted into java code to the path ~ / fisco / java-sdk / dist / contracts / consolidation
# Convert sol, where ${packageName}is the generated java code package path
# The generated java code is located in the ~ / fisco / java-sdk / dist / contracts / sdk / java directory
java -cp "apps/*:lib/*:conf/" org.fisco.bcos.sdk.demo.codegen.DemoSolcToJava ${packageName}