Skip to content

Migrating a Ethereum DApp to the HPB MainNet

BlockGeek edited this page Sep 12, 2019 · 3 revisions

Migrating an Ethereum DApp to the HPB MainNet

Introduction

In this document you will find an easy to follow step-by-step guide how to migrate an existing DApp from the Ethereum Network to the HPB MainNet in order to gain more transactions per second, higher security and lower fees.

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

In this example, we will migrate the HPBCoin from Etherum to HPB MainNet, but any contract can as easily be migrated.

The migration procedure consisst of the following steps:

  1. Query ERC20 token in a Ethereum Blockchain Browser
  2. Copy the source code of HPB Coin to Remix
  3. Recompile smart contract code with Remix
  4. Option 1: Deploy smart contract using myhpbwallet.com
  5. Option 2: Invoke HPB Java SDK or Web3js to deploy the HPB smart contract
  6. Release and call the new smart contract of this particular DApp via the HPB versions of JavaSDK or Web3js.
  7. API Reference
  8. Summary

1. Query ERC20 token in a Ethereum Blockchain Browser

Visit Ethereum browser to search for Ethereum ERC20 contracts.

In this example, type HPB in the search box and select HPB Coin

1564997503982

2. Copy the source code of HPB Coin to remix

Click on HPB ERC20’s smart contract address

1564997543072

Click on Code to check the source code of HPB ERC20

1564997555226

Click Copy to copy the source code

Open the online compiler for solidity smart contracts

Paste the code into the compiler,and set the compiler’s version no. to be v0.4.11, as stated in the contract.

3. Compile Source Code of HPB Coin

1564997567828

Click “Details”to bring up the BYTECODE window, and copy the object section (exclude “)

1564997579082

4. Deploy HPB smart contract using myhpbwallet.com

Visit https://myhpbwallet.com and click on the Contract links. Next click on “Deploy Contract”, and paste the Byte Code into the window. Gas Limit should be set automatically. Be careful not to alter it to a lower price, as the contract creation is then likely to fail. Select which way to sign the contract creation, for example with a Ledger or private key.

1564997589444

Once the transaction is transferred to the network, you will see a green section at the bottom of the webpage. Make sure you copy the address, as this is your contract address which you can interact with using your frontend application.

5. Deploy the contract to the HPB blockchain via the HPB version of the JavaSDK/Web3js 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);

}

6. Release and call the new smart contract of this particular DApp via the HPB versions of JavaSDK or Web3js.

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);

                 }

             }

7. API Reference

API Reference: http://open.hpb.io/#/about?id=9

There are some differences in the API between Ethereum and HPB, but in the vast majority of cases you simply need to change the API to use hpb_ instead of eth_, and it will work. You can see an example below which shows the current block number in Hex format, using the publicly available node:

curl -H "Content-Type:application/json" -X POST --data '{"jsonrpc":"2.0","method":"hpb_blockNumber","params":[],"id":83}' http://pub.node.hpb.io

8. Summary

Any DApp contract originally coded for the Ethereum blockchain can very easily be migrated to the HPB MainNet following a similar process as if you are deploying an DApp on the ERC20 network. Simply 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