启动私链 相关内容见之前笔记记录
导入Jar包
- Java调用以太坊账户需要依赖
web3j
导入方式一:Maven
org.web3j core 4.2.0
导入方式二:下载jar包添加到项目library中
- 导入日志包(同上)
log4j log4j 1.2.17 >compileorg.slf4j slf4j-api 1.7.18 >compileorg.slf4j slf4j-log4j12 1.7.18 >compile
否则运行可能会出现如下情况:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
import java.math.BigDecimal;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Transfer;
import org.web3j.utils.Convert;
import com.mao.utils.Consts;
public class MainTest { public static void main(String[] argv) throws Exception {
// 1.连接到以太坊网络上节点
// 默认参数为http://localhost:8545
// 下面两个语句等同
// Web3j web3j = Web3j.build(new HttpService());
Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
// 2.加载以太坊钱包 参数说明: 密码 账户文件路径
// loadCredentials(String password, String source)
Credentials credentials = WalletUtils.loadCredentials(Consts.PASSWORD, Consts.PATH);
// 3.创建交易 参数列表: 网络 钱包 接收方地址 数值 单位
// sendFunds(Web3j web3j, Credentials credentials, String toAddress, BigDecimal
// value, Unit unit)
TransactionReceipt transferReceipt = Transfer.sendFunds(web3j, credentials,
"0x118C56BAADdC4DA63aDD3B4bcf241681608ead71", BigDecimal.ONE, Convert.Unit.WEI).send();
// 4.输出交易的Hash值
System.out.println(transferReceipt.getTransactionHash());
}
}
钱包加载说明:
password
: 创建以太坊账户时设置的密码source
: 账户存储路径例如:
E:/BlockChain/node/data/keystore/UTC--2020-03-07T04-19-25.882222900Z--05a264bc64d6dd81492b1f5a4c93240d6ab15164
文章图片
文章图片
运行程序
- 运行程序
可以发现控制台处于阻塞状态
文章图片
- 命令行的变化
可以发现提交了一个交易( transaction)
文章图片
- 用
txpool.status
命令可以看到本地交易池中有一个待确认的交易
> txpool.status { pending: 1, queued: 0 } >
- 可以使用
eth.getBlock("pending", true).transactions
查看当前待确认交易
> eth.getBlock("pending", true).transactions [{ blockHash: "0x05a1680dd4304acb5d5df4de5c6963fe8449764afee5bfe452a309180ae66234", blockNumber: 172, from: "0x05a264bc64d6dd81492b1f5a4c93240d6ab15164", gas: 21000, gasPrice: 1000000000, hash: "0xf841f29ff171685e36b527f039e09ca65282b643e5ce92e678449ef1d24ab815", input: "0x", nonce: 11, r: "0x629941aa58684b4dab5ef5f233bbc5c9360facf6cad615fbdaedf0d22493e0c7", s: "0x2892bd6e78f4f23db4f6a7e52eb1b8f186224fa7944c45543d41bf391b7d3bce", to: "0x118c56baaddc4da63add3b4bcf241681608ead71", transactionIndex: 0, v: "0x1c", value: 1 }] >
- 启动挖矿
miner.start(1)
- 控制台输出
文章图片
【Java如何调用以太坊账户】注意:当600秒内没有挖矿,将记录写入区块链,则会抛出异常
Exception in thread "main" org.web3j.protocol.exceptions.TransactionException: Transaction receipt was not generated after 600 seconds for transaction: 0xf841f29ff171685e36b527f039e09ca65282b643e5ce92e678449ef1d24ab815 at org.web3j.tx.response.PollingTransactionReceiptProcessor.getTransactionReceipt(PollingTransactionReceiptProcessor.java:51) at org.web3j.tx.response.PollingTransactionReceiptProcessor.waitForTransactionReceipt(PollingTransactionReceiptProcessor.java:29) at org.web3j.tx.TransactionManager.processResponse(TransactionManager.java:72) at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:51) at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:87) at org.web3j.tx.Transfer.send(Transfer.java:65) at org.web3j.tx.Transfer.send(Transfer.java:49) at org.web3j.tx.Transfer.lambda$sendFunds$0(Transfer.java:76) at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30) at com.mao.main.MainTest.main(MainTest.java:28)
推荐阅读
- 以太坊|King of the Ether
- 区块链|以太坊(ETH)挖矿教程,minerOS系统
- 区块链|以太坊中的iban地址是什么()
- 区块链|2018年最新XMR挖矿教程,CPU、GPU同时挖矿【有图有真相】
- 区块链|JAVA开发区块链1
- 区块链|【以太坊钱包_ETH钱包_以太坊ERC20代币钱包】
- 以太坊|以太坊将成为新互联网的支柱
- 比特币|【加精】一键代币创建,让以太坊ERC-20代币创建简化
- 以太坊|eth 创建钱包,转账 web3j
- 比特币|【精华】5分钟学会创建属于自己的以太坊ERC20代币