| 知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
public BigInteger getBalance(String account) throws IOException {
EthGetBalance ethGetBalance = web3j.ethGetBalance(account, DefaultBlockParameterName.LATEST).send();
BigInteger balance = ethGetBalance.getBalance();
return balance;
}
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());
}
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());
}
}
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);
}
}
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 狀態的數據。
EthTransaction transaction = web3.ethGetTransactionByHash("TRANSACTION_HASH").sendAsync().get();
System.out.println(transaction.getResult());