-
Notifications
You must be signed in to change notification settings - Fork 249
/
TransactionClient.java
143 lines (131 loc) · 4.66 KB
/
TransactionClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.ethjava;
import com.ethjava.utils.Environment;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.admin.Admin;
import org.web3j.protocol.admin.methods.response.PersonalUnlockAccount;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.core.methods.response.EthEstimateGas;
import org.web3j.protocol.core.methods.response.EthGetBalance;
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 java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
public class TransactionClient {
private static Web3j web3j;
private static Admin admin;
private static String fromAddress = "0x7b1cc408fcb2de1d510c1bf46a329e9027db4112";
private static String toAddress = "0x05f50cd5a97d9b3fec35df3d0c6c8234e6793bdf";
private static BigDecimal defaultGasPrice = BigDecimal.valueOf(5);
public static void main(String[] args) {
web3j = Web3j.build(new HttpService(Environment.RPC_URL));
admin = Admin.build(new HttpService(Environment.RPC_URL));
getBalance(fromAddress);
sendTransaction();
}
/**
* 获取余额
*
* @param address 钱包地址
* @return 余额
*/
private static BigInteger getBalance(String address) {
BigInteger balance = null;
try {
EthGetBalance ethGetBalance = web3j.ethGetBalance(address, DefaultBlockParameterName.LATEST).send();
balance = ethGetBalance.getBalance();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("address " + address + " balance " + balance + "wei");
return balance;
}
/**
* 生成一个普通交易对象
*
* @param fromAddress 放款方
* @param toAddress 收款方
* @param nonce 交易序号
* @param gasPrice gas 价格
* @param gasLimit gas 数量
* @param value 金额
* @return 交易对象
*/
private static Transaction makeTransaction(String fromAddress, String toAddress,
BigInteger nonce, BigInteger gasPrice,
BigInteger gasLimit, BigInteger value) {
Transaction transaction;
transaction = Transaction.createEtherTransaction(fromAddress, nonce, gasPrice, gasLimit, toAddress, value);
return transaction;
}
/**
* 获取普通交易的gas上限
*
* @param transaction 交易对象
* @return gas 上限
*/
private static BigInteger getTransactionGasLimit(Transaction transaction) {
BigInteger gasLimit = BigInteger.ZERO;
try {
EthEstimateGas ethEstimateGas = web3j.ethEstimateGas(transaction).send();
gasLimit = ethEstimateGas.getAmountUsed();
} catch (IOException e) {
e.printStackTrace();
}
return gasLimit;
}
/**
* 获取账号交易次数 nonce
*
* @param address 钱包地址
* @return nonce
*/
private static BigInteger getTransactionNonce(String address) {
BigInteger nonce = BigInteger.ZERO;
try {
EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(address, DefaultBlockParameterName.PENDING).send();
nonce = ethGetTransactionCount.getTransactionCount();
} catch (IOException e) {
e.printStackTrace();
}
return nonce;
}
/**
* 发送一个普通交易
*
* @return 交易 Hash
*/
private static String sendTransaction() {
String password = "yzw";
BigInteger unlockDuration = BigInteger.valueOf(60L);
BigDecimal amount = new BigDecimal("0.01");
String txHash = null;
try {
PersonalUnlockAccount personalUnlockAccount = admin.personalUnlockAccount(fromAddress, password, unlockDuration).send();
if (personalUnlockAccount.accountUnlocked()) {
BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
Transaction transaction = makeTransaction(fromAddress, toAddress,
null, null, null, value);
//不是必须的 可以使用默认值
BigInteger gasLimit = getTransactionGasLimit(transaction);
//不是必须的 缺省值就是正确的值
BigInteger nonce = getTransactionNonce(fromAddress);
//该值为大部分矿工可接受的gasPrice
BigInteger gasPrice = Convert.toWei(defaultGasPrice, Convert.Unit.GWEI).toBigInteger();
transaction = makeTransaction(fromAddress, toAddress,
nonce, gasPrice,
gasLimit, value);
EthSendTransaction ethSendTransaction = web3j.ethSendTransaction(transaction).send();
txHash = ethSendTransaction.getTransactionHash();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("tx hash " + txHash);
return txHash;
}
//使用 web3j.ethSendRawTransaction() 发送交易 需要用私钥自签名交易 详见ColdWallet.java
}