-
Notifications
You must be signed in to change notification settings - Fork 6
Case of Transmission from Ethereum DAPP to HPB blockchain
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.
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.
Enter Ethereum browser to query HPB ERC 20 tokens.
Enter HPB in the search box and select HPBCoin
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
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);
}
}
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());
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.
|中文(简体)版本 | |English Version|
Basics
R&D
- HPB-Consensus-Algorithm-Election-Mechanism
- HPB-BOE-Introduction
- HPB-Consensus-Algorithm--Compare
- HPB-Gas-Mechanism
- HPB-P2P-NetWork
- HPB-JSON-RPC-API-Index
- RLP-Agreement-Extended-Reading
- Interface-of-HPB-hardware-random-number-generator
- HPB-hardware-random-number-service
HPB Dapp Development
- Quick-Start-For-HPB-Developer
- HPB-Dapps-Introductions
- HPB-EVM-Contracts-Introductions
- HPB-Extended-Reading-on-EVM-Development
- An-Introduction-to-EVM-Virtual-Machines
- HPB-Smart-Contracts-Extended-Doc
- Best-Practice-of-Accessing-HPB-Main-Network-in-JS-version
- Best-Practice-of-HPB-Main-Network-Access-Java-Version
- Best-Practice-of-HPB-Smart-Contract
- Case-of-Transmission-from-Ethereum-DAPP-to-HPB-blockchain
- How-to-Write-a-Contract-Extended-Reading
- Development-Tool--Remix-Extended-Reading
- Solidity-Language-Extended-Reading
- Token-issuance-on-HPB-MainNet
- Migrating-a-Ethereum-DApp-to-the-HPB-MainNet
HPB Wallet Development
- An-Introduction-to-HPB-Wallet
- HPB-Wallet-for-Xcode-Integration
- Creating-the-HPB-Wallet
- Generating-Mnemonics-for-HPB Wallet
- HPB-Wallet-and-Middleware-Interface
HPB Mainnet/TestNet Entry
- HPB-Development-Node-Construction-Guild
- Guide-to-Server-Security-Reinforcement
- Build Node With Docker
- HPB-Private-Chain-Building-Guide
Developer Community