操作指导 将以太坊 DApp 迁移到 HPB 主网
简介
这篇文档详细提供了一种将以太坊上面的应用迁移到HPB主网的方法,用户从而可以获得更高的性能,更好的安全性和更低的手续费。
这篇文档面向社区开发者,测试者,运营维护人员等等。
在这个例子中,我们将把HPB代币从以太坊迁移到HPB主网上,事实上任何合约都可以参考这个例子。
迁移过程包含以下的步骤:
- 在区块链浏览器中查询ERC20代币。
- 复制ERC 20代币的源码到Remix。
- 使用Remix重新编译智能合约。
- 方案一:使用myhpbwallet.com部署智能合约。
- 方案二:调用HPB的SDK部署智能合约。
- 发布并调用智能合约使用HPB的SDK。
- API手册
- 总结
1. 在区块链浏览器中查询ERC20代币。
访问以太坊浏览器并且搜索ERC 20合约。
在这个例子中,在搜索框中输入HPB并且选择HPB代币
2.复制ERC 20代币源代码到Remix
点击HPB ERC20代币的合约地址
点击Code查看HPB代币的源代码
点击Copy复制源代码
打开Remix合约编辑器
复制代码到编译器中,并且把编译器版本设置为0.4.11,版本号在合约中有说明。
3.编译HPB代币源代码
点击“详情”打开字节码窗口并且复制对象部分
4.使用myhpbwallet.com部署HPB智能合约
访问https://myhpbwallet.com,点击合约链接。继续点击“部署合约”,把字节码拷贝到窗口中。Gas limit设置成自动。请注意不要调低价格,否则合约可能会创建失败。选择签名的方式,使用ledger或者私钥。
一旦交易被传送到网络中,页面的底部会出现绿色的区域。确保你复制了地址,这个是后续用来和前端应用交互的地址。
5. 调用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);
}
6. 使用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);
}
}
7.API手册
API手册:http://open.hpb.io/
HPB和以太坊的API有一些区别,不过大多数情况下你只需要使用hpb 代替 eth。你可以使用如下的例子来查看当前的区块数。
Curl -H “Content-Type:application/json” -X POST –data ‘{“jsonrpc”:”2.0”,”method”:”hpb_blockNumber”,”params”:[],”id”:83}’ http://pub.node.hpb.io
8.总结
以太坊的DApp合约可以很轻松的转移到HPB主网上就如同你在以太坊上面部署合约一样。复制源码,使用Remix重新编译,复制相应的bin和abi文件,使用HPB的SDK发布并调用合约,按照这些步骤操作就可以完成。