graph LR
A-->B
graph LR
A-->B
- 启动私链
geth --datadir data0 --networkid 10 --rpc --rpcaddr 172.17.81.217 --rpcport 8545 --rpcapi "web3,eth" --rpccorsdomain "*" console
- 代码段
private Web3j conectETHclient() throws IOException {
//连接方式1:使用infura 提供的客户端
//Web3j web3j = Web3j.build(new HttpService("https://rinkeby.infura.io/zmd7VgRt9go0x6qlJ2Mk"));// TODO: 2018/4/10 token更改为自己的
//连接方式2:使用本地客户端
Web3j web3j = Web3j.build(new HttpService("http://47.93.230.63:8545"));
//测试是否连接成功
String web3ClientVersion = web3j.web3ClientVersion().send().getWeb3ClientVersion();
log.info("version=" + web3ClientVersion);
return web3j;
}
private void creatAccount() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException, CipherException, IOException {
String walletFileName0 = "";//文件名
String walletFilePath0 = "C:\\Users\\commando\\AppData\\Roaming\\Ethereum\\testnet\\keystore";
//钱包文件保持路径,请替换位自己的某文件夹路径
walletFileName0 = WalletUtils.generateNewWalletFile("123456", new File(walletFilePath0), false);
//WalletUtils.generateFullNewWalletFile("password1",new File(walleFilePath1));
//WalletUtils.generateLightNewWalletFile("password2",new F
// ile(walleFilePath2));
log.info("walletName: " + walletFileName0);
}
private Credentials loadWallet() throws IOException, CipherException {
String walleFilePath = "C:\\Users\\commando\\AppData\\Roaming\\Ethereum\\testnet\\keystore\\UTC--2018-05-08T02-55-54.711051948Z--22149078fdf7178fd8f0ff89ce6a85d230e583d8.json";
String passWord = "huangl";
Credentials credentials = WalletUtils.loadCredentials(passWord, walleFilePath); //debug时间比较长需要几分钟,run时正常
String address = credentials.getAddress();
BigInteger publicKey = credentials.getEcKeyPair().getPublicKey();
BigInteger privateKey = credentials.getEcKeyPair().getPrivateKey();
log.info("address=" + address);
log.info("public key=" + publicKey);
log.info("private key=" + privateKey);
return credentials;
}
linux 上创建的keystore没有json后缀,可以拷贝到本机加上后缀
private void transferCoin(Web3j web3j, Credentials credentials) throws Exception {
TransactionReceipt transferReceipt = Transfer.sendFunds(
web3j, credentials,
"d14ff35010a22a9472cbc543d36c744a6cb8391d", // you can put any address here
BigDecimal.ONE, Convert.Unit.WEI) // 1 wei = 10^-18 Ether
.send();
log.info("Transaction complete, view it at https://rinkeby.etherscan.io/tx/" + transferReceipt.getTransactionHash());
}
credentials 可以设定为默认账户,有余额的账户
private void deployContract(Web3j web3j, Credentials credentials) throws Exception {
log.info("Deploying smart contract");
Greeter contract = Greeter.deploy(
web3j, credentials,
//ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT,
ManagedTransaction.GAS_PRICE, new BigInteger("2300000"),
"Hello blockchain world!").send();
String contractAddress = contract.getContractAddress();
log.info("Smart contract deployed to address " + contractAddress);
log.info("View contract at https://rinkeby.etherscan.io/address/" + contractAddress);
log.info("Value stored in remote smart contract: " + contract.greet().send());
// Lets modify the value in our smart contract
TransactionReceipt transactionReceipt = contract.newGreeting("Well hello again").send();
log.info("New value stored in remote smart contract: " + contract.greet().send());
// Events enable us to log specific events happening during the execution of our smart
// contract to the blockchain. Index events cannot be logged in their entirety.
// For Strings and arrays, the hash of values is provided, not the original value.
// For further information, refer to https://docs.web3j.io/filters.html#filters-and-events
for (Greeter.ModifiedEventResponse event : contract.getModifiedEvents(transactionReceipt)) {
log.info("Modify event fired, previous value: " + event.oldGreeting
+ ", new value: " + event.newGreeting);
log.info("Indexed event previous value: " + Numeric.toHexString(event.oldGreetingIdx)
+ ", new value: " + Numeric.toHexString(event.newGreetingIdx));
}
}
check current block gas limit by running - eth.getBlock(“latest”). If you supply more gas than this value, you will face ‘Exceeds block gas limit’ error Then check gas value in sendTransaction(). It should be atleast 21000. If it isn’t, you will get ‘intrinsic value too low’
gaslimit 超过了区块链浏览器中看到的gaslimit,那个数字应该可以从创世区块的参数中修改,需验证。经过验证创世区块gasLimit修改为:0xffffffff