- 第 1 章 区块链
- 第 2 章 以太坊
- 第 3 章 以太坊私链入门
- 第 4 章 以太坊网络
- 第 5 章 geth v1.8.16 命令详解
- 第 6 章 Wallet
- 第 7 章 Token
- 第 8 章 智能合约语言 Solidity v0.5.0
- 第 9 章 Truffle v4.1.8 开发框架
- 第 10 章 web3.js - 1.0.0
- 第 11 章 web3j v3.4.0 - Jave Client
- 11.2. 启动以太坊
- 11.3. Maven pom.xml 文件
- 11.4. Java 与 Solidity 数据类型映射关系
- 11.5. 常量
- 11.6. 连接到服务器获取版本号
- 11.7. 获得以太坊状态信息
- 11.8. 单位转换
- 11.9. 账号管理
- 11.10. Credentials
- 11.11. 交易
- 11.12. 钱包
- 11.13. 智能合约
- 11.14. ERC20合约
- 11.15. Infura
- 11.16. 助记词
- 11.17. 过滤器 (Filter)
- 11.18. Subscription
- 11.19. 解锁账号
- 11.20. IBAN (International Bank Account Number)
- 11.21. Springboot with Ethereum (web3j)
- 第 12 章 web3.py - A python interface for interacting with the Ethereum blockchain and ecosystem.
- 第 14 章 Ethereum Developer APIs
- 第 15 章 infura
- 第 16 章 以太坊案例
- 第 17 章 FAQ
- 17.3. Error: authentication needed: password or unlock
- 17.4. 新增节点后不生效
- 17.5. Unhandled rejection Error: Returned error: The method personal_unlockAccount does not exist/is not available
- 17.6. Error: exceeds block gas limit
- 17.7. Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(…) { … }" instead.
- 17.8. Exception in thread "main" rx.exceptions.OnErrorNotImplementedException: Invalid response received: okhttp3.internal.http.RealResponseBody@6c25e6c4
- 17.9. 旧版本 Remix(browser-solidity) 本地安装
- 第 18 章 Hyperledger Fabric v2.0.0
- 第 19 章 Hyperledger Fabric 运维
- 第 20 章 Chaincode 链码(智能合约)
- 第 21 章 Hyperledger Fabric Client SDK for Node.js
- 第 22 章 fabric-sdk-java
- 第 24 章 已知 Hyperledger 落地案例
- 第 25 章 Fabric Command
- 第 26 章 Fabric FAQ
- 第 27 章 IPFS(InterPlanetary File System,星际文件系统)
- 第 28 章 IPFS 命令
- 第 29 章 IPFS WebUI
- 第 30 章 IPFS 集群配置
- 第 31 章 IPFS API
- 第 32 章 IPFS Faq
- 第 33 章 EOS
- 第 34 章 EOS 安装
- 第 35 章 CLEOS
- 第 36 章 智能合约开发
- 第 37 章 EOS Dapp 开发
- 第 38 章 FAQ
- 第 39 章 BaaS (Blockchain as a Service) 平台
- 第 40 章 BitCoin
- 第 41 章 其他区块链相关
- 附录 1. 附录
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
11.11. 交易
11.11. 交易
11.11.1. 获取余额
public BigInteger getBalance(String account) throws IOException { EthGetBalance ethGetBalance = web3j.ethGetBalance(account, DefaultBlockParameterName.LATEST).send(); BigInteger balance = ethGetBalance.getBalance(); return balance; }
11.11.2. 通过 Keystore 转账
public void transfer(String account, float coin) throws InterruptedException, IOException, TransactionException, Exception { String password = ""; String walletfile = "/Users/neo/netkiller/UTC--2018-01-20T04-04-06.786586541Z--83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6"; Credentials credentials = WalletUtils.loadCredentials(password, walletfile); TransactionReceipt transactionReceipt = Transfer.sendFunds(web3j, credentials, account, BigDecimal.valueOf(coin), Unit.ETHER).send(); System.out.println(transactionReceipt.getStatus()); }
11.11.3. 通过私钥转账
package cn.netkiller.ethereum.transaction; import org.web3j.crypto.Credentials; 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 java.math.BigDecimal; public class TransactionTest { public static void main(String[] args) throws Exception { Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/CsS9shwaAab0z7B4LP2d")); String toAddress = "0xf56b81a2bcb964D2806071e9Be4289A5559BB0fA"; Credentials credentials = Credentials.create("16690967F2BADABE13A067066558537228D8AF63ECCB022FFBDC970EC717BC3A"); TransactionReceipt transactionReceipt = Transfer.sendFunds(web3j, credentials, toAddress, BigDecimal.valueOf(0.002), Convert.Unit.ETHER).send(); System.out.println(transactionReceipt.getTransactionHash()); } }
11.11.4. 指定 gas 费用
package cn.netkiller.ethereum.transaction; import org.web3j.crypto.Credentials; import org.web3j.crypto.TransactionEncoder; import org.web3j.crypto.RawTransaction; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.methods.response.EthGetTransactionCount; import org.web3j.protocol.core.methods.response.EthSendTransaction; import org.web3j.protocol.http.HttpService; import org.web3j.utils.Convert; import org.web3j.utils.Numeric; import java.math.BigInteger; import java.util.concurrent.ExecutionException; public class RawTransactionTest { public static void main(String[] args) throws InterruptedException, ExecutionException { // TODO Auto-generated method stub // 设置需要的矿工费 BigInteger gasPrice = BigInteger.valueOf(18_000_000_000L); BigInteger gasLimit = BigInteger.valueOf(4_300_000); // System.out.println(gasPrice); // 连接 ropsten测试环境,这里使用的是infura这个客户端 Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/CsS9shwaAab0z7B4LP2d")); // 转出账户地址 String fromAddress = "0x22c57F0537414FD95b9f0f08f1E51d8b96F14029"; // 接收账户地址 String toAddress = "0xf56b81a2bcb964D2806071e9Be4289A5559BB0fA"; // 转账人的私钥 Credentials credentials = Credentials.create("16697AC066558537CADABF68BDE13A06790967F2BC3A228DB022FF0EC717B3EC"); // Nonce 就相当于数据中PK主键,每次Nonce会做 +1 操作 EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST).sendAsync().get(); BigInteger nonce = ethGetTransactionCount.getTransactionCount(); // 创建交易并转0.05个以太币 BigInteger value = Convert.toWei("0.05", Convert.Unit.ETHER).toBigInteger(); RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, toAddress, value); // 对交易做签名 byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); // 发送交易 EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); // 获得到transactionHash后就可以到以太坊的网站上查询这笔交易的状态了 System.out.println("https://ropsten.etherscan.io/tx/" + transactionHash); } }
11.11.5. 查询 Transaction Information
public TransactionReceipt getTransactionReceipt(String transactionHash) throws InterruptedException, ExecutionException { EthGetTransactionReceipt ethGetTransactionReceipt = web3.ethGetTransactionReceipt(transactionHash).sendAsync().get(); return ethGetTransactionReceipt.getResult(); }
执行返回结果
TransactionReceipt{ transactionHash='0xece52bdbc6d4fa0c8eba7578a7c6e537883265199fa07ef8e5b1038e4bcdefb9', transactionIndex='0x13', blockHash='0x2642b35670872a0e024d30ab2393b6bd4f7dab449bf4fc3eac067e2677cbc085', blockNumber='0x344a79', cumulativeGasUsed='0x806f54', gasUsed='0x8fee', contractAddress='null', root='null', status='0x1', from='0x22c57f0537414fd95b9f0f08f1e51d8b96f14029', to='0xb3cedc76e75fcd278c988b22963c2f35c99c10b7', l ogs=[Log{removed=false, logIndex='0xa', transactionIndex='0x13', transactionHash='0xece52bdbc6d4fa0c8eba7578a7c6e537883265199fa07ef8e5b1038e4bcdefb9', blockHash='0x2642b35670872a0e024d30ab2393b6bd4f7dab449bf4fc3eac067e2677cbc085', blockNumber='0x344a79', address='0xb3cedc76e75fcd278c988b22963c2f35c99c10b7', data='0x000000000000000000000000000000000000000000000000000000000000000a', type='null', topics=[0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x00000000000000000000000022c57f0537414fd95b9f0f08f1e51d8b96f14029, 0x000000000000000000000000cdf0253d8362d6c3334c8f28a6bfd74c90d03d92]}], logsBloom='0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000008020000001000000000000000000000000000800000000000000000000000000000000000000000000000000000000010000000000000000000004000000000000000000000000000800000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000002000000000000000400000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' }
注意:该函数只能返回 TxReceipt Status: Success 状态数据,无法返回 Pending 状态的数据。
11.11.6. 交易结果查询
EthTransaction transaction = web3.ethGetTransactionByHash("TRANSACTION_HASH").sendAsync().get(); System.out.println(transaction.getResult());
11.11.7. RawTransaction 编码与解码
String hexTransaction = Numeric.toHexString(TransactionEncoder.encode(rawTransaction)); RawTransaction tx1 = TransactionDecoder.decode(hexTransaction);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论