操作指导 HPB 主网代币发行

优质
小牛编辑
137浏览
2023-12-01

简介

本文详细提供了一种将DApp应用部署到HPB主网的方法。

该方法分为四个步骤

  1. 开发ERC20智能合约
  2. 使用Remix编译智能合约
  3. 使用myhpbwallet.com部署智能合约
  4. 使用HPB SDK调用智能合约

1.开发ERC20智能合约

开发者可以参考HPB开发手册: HPB DApp development guide

2.编译ERC20代币源代码

你可以从0开始开发自己的ERC20合约,也可以自定义一个模板:https://etherscan.io/address/0x1c95b093d6c236d3ef7c796fe33f9cc6b8606714

代码写好之后就可以开始编译,编译结束后点击“详情”、

img

在该窗口中复制对象部分

3.使用myhpbwallet.com部署HPB智能合约

访问https://myhpbwallet.com,点击合约链接。继续点击“部署合约”,把字节码拷贝到窗口中。Gas limit设置成自动。请注意不要调低价格,否则合约可能会创建失败。选择签名的方式,使用ledger或者私钥。

img

一旦交易被传送到网络中,页面的底部会出现绿色的区域。确保你复制了地址,这个是后续用来和前端应用交互的地址。下一步点击 “Access Wallet” 链接,并用钱包认证。然后你可以点击 “增加自定义代币“。输入之前复制的合约地址,代币符号以及小数位数。如果有问题可以访问https://hpbscan.org并且查看上一笔交易是否成功。(很多交易因为Gas Limit太低失败)

img

你现在可以在你钱包的右侧看到代币的数量

img

4.调用HPB的SDK部署智能合约

通过生成Java包装类来运行合约。创建HPBToken.abi和HPBToken.bin文件并且把他们复制到相应的文件中。

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

}

5. 使用HPB的SDK发布并调用智能合约。


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

                 }

             }

总结

在HPB主网上可以很容易地创建基于合约的Dapp应用,和在以太坊上创建类似。开发源码,使用Remix重新编译,复制相应的bin和abi文件,使用HPB的SDK发布并调用合约,按照这些步骤操作就可以完成。