本次要分享的是使用传入的私钥或者json文件的方式进行ETH交易,交易中可以使用默认的矿工费用和燃料限制,也可以使用自定义燃料费用的方式。
/**
* 使用默认的矿工费用,传入生成的钱包文件当交易凭证,进行ETH交易
*
* @param Web3j web3j对象
* @param String walleFilePath 钱包地址
* @param String password 钱包密码
* @param String address_to 发送交易的地址
* @param String value 发送交易的币数目
* @param Unit unit 发送交易的币单位
*/
Credentials credentials = WalletUtils.loadCredentials(password, walleFilePath);
// 开始发送交易到指定地址
TransactionReceipt send = Transfer.sendFunds(web3j, credentials, address_to, new BigDecimal(value), unit)
.send();
/**
* 自定义矿工费进行交易
* @param web3j Web3j对象
* @param credentials Credentials对象
* @param GAS_PRICE 自定义的燃料费用
* @param GAS_LIMIT 自定义燃料最高限制
* @param toAddress 接受交易者钱包地址
* @param value 发送交易ether金额
* @return String TxHash 交易号
*/
public static Object transTo(Web3j web3j, Credentials credentials, BigInteger GAS_PRICE, BigInteger GAS_LIMIT,
String toAddress, BigDecimal value) {
TransactionManager transactionManager = new RawTransactionManager(web3j, credentials);
Transfer transfer = new Transfer(web3j, transactionManager);
TransactionReceipt send = null;
try {
send = transfer.sendFunds(toAddress, value, Convert.Unit.ETHER, GAS_PRICE, GAS_LIMIT).send();
} catch (Exception exception) {
exception.printStackTrace();
}
return send.getTransactionHash();
}
这种方式,在交易成功之后,可以取得如下信息。
返回TransactionReceipt对象 可以获取交易的哈希值,发送和接受者,交易状态等
System.out.println("trans hash=" + send.getTransactionHash());
System.out.println("from :" + send.getFrom());
System.out.println("to:" + send.getTo());
System.out.println("gas used=" + send.getGasUsed());
System.out.println("status: " + send.getStatus());
还有一种,使用签名,并可以自定义矿工费用的方式进行ETH交易。
/**
* (可自定义矿工费用)带签名的交易
* @param web3j Web3j对象
* @param credentials Credentials对象
* @param GAS_PRICE 自定义的燃料费用
* @param GAS_LIMIT 自定义燃料最高限制
* @param fromAddress 发送交易者钱包地址
* @param toAddress 接受交易者钱包地址
* @param value 发送交易ether金额
* @return String TxHash 交易号
*/
public static Object transTo(Web3j web3j, Credentials credentials, BigInteger GAS_PRICE, BigInteger GAS_LIMIT,
String fromAddress, String toAddress, String value) {
// getNonce
EthGetTransactionCount ethGetTransactionCount = null;
try {
ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST)
.sendAsync().get();
} catch (Exception exception) {
exception.printStackTrace();
}
BigInteger nonce = ethGetTransactionCount.getTransactionCount();
// 创建交易
BigInteger value_BigInteger = Convert.toWei(value, Convert.Unit.ETHER).toBigInteger();
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, GAS_PRICE, GAS_LIMIT, toAddress,
value_BigInteger);
// 签名Transaction,这里要对交易做签名
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);
// 发送交易
EthSendTransaction ethSendTransaction = null;
try {
ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
} catch (Exception exception) {
exception.printStackTrace();
}
String transactionHash = ethSendTransaction.getTransactionHash();
return transactionHash;
}
利用私钥进行ETH交易,可自定义矿工费用
注意:传入的私钥要转成16进制哦,不然签名的时候会报错。
// 利用私钥进行签名
public static String signTransaction(BigInteger nonce, BigInteger GAS_PRICE, BigInteger GAS_LIMIT, String to, BigInteger value, String data, byte chainId, String privateKey) throws IOException {
byte[] signedMessage;
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, GAS_PRICE, GAS_LIMIT, to, value, data);
if (privateKey.startsWith("0x")) {
privateKey = privateKey.substring(2);
}
ECKeyPair ecKeyPair = ECKeyPair.create(new BigInteger(privateKey, 16));
Credentials credentials = Credentials.create(ecKeyPair);
if (chainId > ChainId.NONE) {
signedMessage = TransactionEncoder.signMessage(rawTransaction, chainId, credentials);
} else {
signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
}
String hexValue = Numeric.toHexString(signedMessage);
return hexValue;
}
/***
* 利用私钥发送交易
* @param web3j Web3j对象
* @param GAS_PRICE 燃料价格
* @param GAS_LIMIT 燃料最大限度
* @param fromAddress 发送交易者钱包地址
* @param toAddress 接受交易者钱包地址
* @param value 交易的eth金额
* @param privateKey 发送者私钥
*/
public static void transToByPrivateKey(Web3j web3j, BigInteger GAS_PRICE, BigInteger GAS_LIMIT,
String fromAddress, String toAddress, String value,String privateKey) {
BigInteger nonce = null;
EthGetTransactionCount ethGetTransactionCount = null;
ethGetTransactionCount = web3j.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.PENDING).send();
if (ethGetTransactionCount == null)
return;
nonce = ethGetTransactionCount.getTransactionCount();
BigInteger valueEther = Convert.toWei(new BigDecimal(value), Convert.Unit.ETHER).toBigInteger();
String data = "";
byte chainId = ChainId.MAINNET;//主网络,可以切换测试网络
String signedData;
signedData = signTransaction(nonce, GAS_PRICE, GAS_LIMIT, toAddress, valueEther, data, chainId, privateKey);
if (signedData != null) {
EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(signedData).send();
System.out.println("txHash=" + ethSendTransaction.getTransactionHash());
}