本文整理汇总了Java中org.web3j.protocol.core.methods.response.EthSendTransaction.getError方法的典型用法代码示例。如果您正苦于以下问题:Java EthSendTransaction.getError方法的具体用法?Java EthSendTransaction.getError怎么用?Java EthSendTransaction.getError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.web3j.protocol.core.methods.response.EthSendTransaction
的用法示例。
在下文中一共展示了EthSendTransaction.getError方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateAccountFromScratch
import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入方法依赖的package包/类
@Test
public void testCreateAccountFromScratch() throws Exception {
// create new private/public key pair
ECKeyPair keyPair = Keys.createEcKeyPair();
BigInteger publicKey = keyPair.getPublicKey();
String publicKeyHex = Numeric.toHexStringWithPrefix(publicKey);
BigInteger privateKey = keyPair.getPrivateKey();
String privateKeyHex = Numeric.toHexStringWithPrefix(privateKey);
// create credentials + address from private/public key pair
Credentials credentials = Credentials.create(new ECKeyPair(privateKey, publicKey));
String address = credentials.getAddress();
// print resulting data of new account
System.out.println("private key: '" + privateKeyHex + "'");
System.out.println("public key: '" + publicKeyHex + "'");
System.out.println("address: '" + address + "'\n");
// test (1) check if it's possible to transfer funds to new address
BigInteger amountWei = Convert.toWei("0.131313", Convert.Unit.ETHER).toBigInteger();
transferWei(getCoinbase(), address, amountWei);
BigInteger balanceWei = getBalanceWei(address);
BigInteger nonce = getNonce(address);
assertEquals("Unexpected nonce for 'to' address", BigInteger.ZERO, nonce);
assertEquals("Unexpected balance for 'to' address", amountWei, balanceWei);
// test (2) funds can be transferred out of the newly created account
BigInteger txFees = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);
RawTransaction txRaw = RawTransaction
.createEtherTransaction(
nonce,
Web3jConstants.GAS_PRICE,
Web3jConstants.GAS_LIMIT_ETHER_TX,
getCoinbase(),
amountWei.subtract(txFees));
// sign raw transaction using the sender's credentials
byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
String txSigned = Numeric.toHexString(txSignedBytes);
// send the signed transaction to the ethereum client
EthSendTransaction ethSendTx = web3j
.ethSendRawTransaction(txSigned)
.sendAsync()
.get();
Error error = ethSendTx.getError();
String txHash = ethSendTx.getTransactionHash();
assertNull(error);
assertFalse(txHash.isEmpty());
waitForReceipt(txHash);
assertEquals("Unexpected nonce for 'to' address", BigInteger.ONE, getNonce(address));
assertTrue("Balance for 'from' address too large: " + getBalanceWei(address), getBalanceWei(address).compareTo(txFees) < 0);
}
示例2: checkResponseFromSending
import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入方法依赖的package包/类
private void checkResponseFromSending(EthSendTransaction response) {
Error error = response.getError();
String result = response.getResult();
if (error != null) {
String message = "Failed to send transaction: " + error.getMessage();
LOG.error(message);
throw new ProcessingException(message);
}
else {
LOG.info("result:" + result);
}
}
示例3: testCreateSignAndSendTransaction
import org.web3j.protocol.core.methods.response.EthSendTransaction; //导入方法依赖的package包/类
/**
* Ether transfer tests using methods {@link RawTransaction#createEtherTransaction()}, {@link TransactionEncoder#signMessage()} and {@link Web3j#ethSendRawTransaction()}.
* Most complex transfer mechanism, but offers the highest flexibility.
*/
@Test
public void testCreateSignAndSendTransaction() throws Exception {
String from = Alice.ADDRESS;
Credentials credentials = Alice.CREDENTIALS;
BigInteger nonce = getNonce(from);
String to = Bob.ADDRESS;
BigInteger amountWei = Convert.toWei("0.789", Convert.Unit.ETHER).toBigInteger();
// create raw transaction
RawTransaction txRaw = RawTransaction
.createEtherTransaction(
nonce,
Web3jConstants.GAS_PRICE,
Web3jConstants.GAS_LIMIT_ETHER_TX,
to,
amountWei);
// sign raw transaction using the sender's credentials
byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
String txSigned = Numeric.toHexString(txSignedBytes);
BigInteger txFeeEstimate = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);
// make sure sender has sufficient funds
ensureFunds(Alice.ADDRESS, amountWei.add(txFeeEstimate));
// record balanances before the ether transfer
BigInteger fromBalanceBefore = getBalanceWei(Alice.ADDRESS);
BigInteger toBalanceBefore = getBalanceWei(Bob.ADDRESS);
// send the signed transaction to the ethereum client
EthSendTransaction ethSendTx = web3j
.ethSendRawTransaction(txSigned)
.sendAsync()
.get();
Error error = ethSendTx.getError();
assertTrue(error == null);
String txHash = ethSendTx.getTransactionHash();
assertFalse(txHash.isEmpty());
TransactionReceipt txReceipt = waitForReceipt(txHash);
BigInteger txFee = txReceipt.getCumulativeGasUsed().multiply(Web3jConstants.GAS_PRICE);
assertEquals("Unexected balance for 'from' address", fromBalanceBefore.subtract(amountWei.add(txFee)), getBalanceWei(from));
assertEquals("Unexected balance for 'to' address", toBalanceBefore.add(amountWei), getBalanceWei(to));
}