Skip to content

Case of Transmission from Ethereum DAPP to HPB blockchain

Nicemanss edited this page Aug 20, 2019 · 4 revisions

1 Overview

1.1 Preface

This document mainly includes demo cases of HPB blockchain decentralization applications (hereinafter referred to as “DAPP”), including Web3js, JAVA, from a technical point of view.

Targeted readers: community developers, testers, operation and maintenance personnel.

1.2 Project Background

The DAPP project has been set up with the hope of promoting comprehensive construction of DAPP system, implementing specific requirements of software product line development and domain engineering, and forming the basic framework and of the independent property rights and domain component library.

2 Smoothly transmit HPB tokens based on ERC-20 from Ethereum to HPB blockchain

2.1 The transmission procedure is as follows:

2.2 Query HPB ERC20 Tokens in Ethereum Browsers.

Enter Ethereum browser to query HPB ERC 20 tokens.

https://etherscan.io/tokens

Enter HPB in the search box and select HPBCoin

2.3 Copy the source code of HPB ERC20 to remix

Click on HPBERC20’s smart contract address

Click on Code to check the source code of HPB ERC20

Click Copy to copy the source code

Open online recompiler for solidity smart contracts

https://remix.ethereum.org/#optimize=true&version=soljson-v0.4.11+commit.68ef5810.js

Paste the code into the recompiler,and set the recompiler’s version no. to be v0.4.11

2.4 Recompile Source Code of HPB ERC20 Token

Click for more details

2.5 Deploy the contract to the HPB blockchain via the HPB version of the javaSDK and then call the smart contract

Run smart contracts by generating java wrapper classes corresponding to the contracts.

Create new HPBToken.abi and HPBToken.bin files and copy the aforementioned abi and bin codes to corresponding files.

Introduce java SDk of HPB

<dependency>

    <groupId>io.hpb.web3</groupId>

    <artifactId>web3-hpb</artifactId>

    <version>1.2.0</version>

</dependency> 

//Creat new generation contract wrapper class GenContract

import java.util.Arrays;

import io.hpb.web3.codegen.SolidityFunctionWrapperGenerator;

public class GenContract {

                  public static void main(String[] args) {

                                    try {

                                    String SOLIDITY_TYPES_ARG = "--solidityTypes";

                                    String packageName = "io.hpb.web3.contracts";

                                    String outDirPath = "D:/contracts/java/";

                                    String binDirPath = "D:/contracts/HPBToken.bin";

                                    String abiDirPath = "D:/contracts/HPBToken.abi";

                                                      		                            SolidityFunctionWrapperGenerator.main(Arrays.asList(SOLIDITY_TYPES_ARG,
                                                                                   	binDirPath,
                                                                                 	abiDirPath,
                                                                             	         "-p", packageName, "-o", outDirPath).toArray(new String[0]));

                                    } catch (Exception e) {

                                                      e.printStackTrace();
                                    }

                  }

}

​ ​ ​ //Generated class being HPBToken.java ​

//Delete all construct methods

protected HPBToken(String contractAddress, Web3 web3, Credentials credentials,

            BigInteger gasPrice, BigInteger gasLimit) {

        super(BINARY, contractAddress, web3, credentials, gasPrice, gasLimit);

}

protected HPBToken(String contractAddress, Web3 web3, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) {

        super(BINARY, contractAddress, web3, transactionManager, gasPrice, gasLimit);

}

//Add new construc methods

protected HPBToken(String contractAddress, Web3 web3, Credentials credentials,

        BigInteger gasPrice, BigInteger gasLimit) {

     super(BINARY, contractAddress, web3, new RawTransactionManager(web3, credentials), new StaticGasProvider(gasPrice, gasLimit));

}

​ ​ ​ protected HPBToken(String contractAddress, Web3 web3, TransactionManager transactionManager,BigInteger gasPrice, BigInteger gasLimit) { ​

     super(BINARY, contractAddress, web3, transactionManager, new StaticGasProvider(gasPrice, gasLimit));

}

protected HPBToken(String contractAddress,

            Web3 web3, TransactionManager transactionManager,ContractGasProvider gasProvider) {

                  super(BINARY, contractAddress, web3, transactionManager, gasProvider);

}

Automatically create HPB developer nodes locally; release and call smart contracts. Codes involved are as follows:

private static final int WEB3J_TIMEOUT = 800;

                  public static void main(String[] args) throws Exception{

                                    BigInteger gasPrice = Convert.toWei("18", Convert.Unit.GWEI).toBigInteger();

                                    BigInteger gasLimit = new BigInteger("99000000");

                                    Credentials credentials = WalletUtils.loadCredentials(password,keyStorePath);

                                    Web3Service web3Service = buildService(clientAddress);

                                    Admin admin = Admin.build(web3Service);

                                    RawTransactionManager transactionManager=new RawTransactionManager(admin, credentials, ChainId.MAINNET);

                                    Address _target=new Address(address);

                                    HPBToken hpbTokenDeploy = HPBToken.deploy(admin, transactionManager, gasPrice, gasLimit, _target).

                                    sendAsync().get(WEB3J_TIMEOUT, TimeUnit.MINUTES);

                                    String contractAddress=hpbTokenDeploy.getContractAddress();

                                    log.info("publish the smart contract:"+contractAddress);

                                    HPBToken hpbToken = HPBToken.load(contractAddress, admin, transactionManager, gasPrice, gasLimit);

                                    Bool bool = hpbToken.saleStarted().sendAsync().get(WEB3J_TIMEOUT, TimeUnit.MINUTES);

                                    log.info(bool);

TransactionReceipt receipt = hpbToken.transfer(new Address("0x09fe745cff05b35cb06da6768586279018c08d7f"), 

                                                                        new Uint256(Convert.toWei("10", Convert.Unit.HPB).toBigInteger())).sendAsync()

                                    .get(WEB3J_TIMEOUT, TimeUnit.MINUTES);

                                    log.info(receipt.isStatusOK());

                  }

                  private static Web3Service buildService(String clientAddress) {

                                    Web3Service Web3Service;

                                    if (clientAddress == null || clientAddress.equals("")) {

                                    Web3Service = new HttpService(createOkHttpClient());

                                    } else if (clientAddress.startsWith("http")) {

                                    Web3Service = new HttpService(clientAddress,       createOkHttpClient(), false);

                                    } else if (System.getProperty("os.name").toLowerCase().startsWith("win")) {

                                    Web3Service = new WindowsIpcService(clientAddress);

                                    } else {

                                   Web3Service = new UnixIpcService(clientAddress);

                                    }
                                    return Web3Service;

                  }


                    private static OkHttpClient createOkHttpClient() {

                          OkHttpClient.Builder builder = new OkHttpClient.Builder();

                           configureLogging(builder);

                           configureTimeouts(builder);

                           return builder.build();

                  }
                  
                          private static void configureTimeouts(OkHttpClient.Builder builder) {

                           builder.connectTimeout(WEB3J_TIMEOUT, TimeUnit.SECONDS);

                           builder.readTimeout(WEB3J_TIMEOUT, TimeUnit.SECONDS); 
                           // Sets the socket timeout too

                           builder.writeTimeout(WEB3J_TIMEOUT, TimeUnit.SECONDS);

                  }
                  
                   private static void configureLogging(OkHttpClient.Builder builder) {

                       if (log.isDebugEnabled()) {

                         HttpLoggingInterceptor logging = new HttpLoggingInterceptor(log::debug);
                                                   logging.setLevel(HttpLoggingInterceptor.Level.BODY);

                        builder.addInterceptor(logging);

                                    }

                  }

2.6 Deploy smart contract to HPB blockchain via the PB version of Web3js and then call the contract

Codes needed for deployment and calling of Web3.js are as follows: var Web3ForHpb = require('web3_hpb');

var web3Hpb = new Web3ForHpb();

web3Hpb.setProvider(new web3Hpb.providers.HttpProvider("http://localhost:8545"));

web3Hpb.hpb.contract(abi).new({

    data: '0x' + bytecode,

    from: "",

    gas: 1000000

}, (err, res) => {

    if (err) {

        console.log(err);

        return;

    }

    console.log(res.transactionHash);

    // If we have an address property, the contract was deployed

    if (res.address) {

        console.log('Contract address: ' + res.address);

    }

});
 var hpbTestContract = web3Hpb.hpb.contract(abi).at(contractAddress);

console.log(hpbTestContract.getContractAddr.call());

3 Summary

Any contract coding based on Ethereum blockchain Dapp can be smoothly transmitted to HPB blockchain following the pretty much the same process: copy the source codes, recompile with remix, copy corresponding bin and abi files, and release and call the new smart contract of this particular DAPP via the HPB versions of javaSDK and web3js.

Clone this wiki locally