org.web3j.protocol.exceptions.TransactionException Java Examples
The following examples show how to use
org.web3j.protocol.exceptions.TransactionException.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrivacyRequestFactory.java From besu with Apache License 2.0 | 6 votes |
public String privxAddToPrivacyGroup( final Base64String privacyGroupId, final PrivacyNode adder, final List<String> addresses) throws IOException, TransactionException { final BigInteger nonce = besuClient .privGetTransactionCount(adder.getAddress().toHexString(), privacyGroupId) .send() .getTransactionCount(); final Bytes payload = encodeAddToGroupFunctionCall( Bytes.fromBase64String(adder.getEnclaveKey()), addresses.stream().map(Bytes::fromBase64String).collect(Collectors.toList())); final RawPrivateTransaction privateTransaction = RawPrivateTransaction.createTransaction( nonce, BigInteger.valueOf(1000), BigInteger.valueOf(3000000), Address.ONCHAIN_PRIVACY_PROXY.toHexString(), payload.toHexString(), Base64String.wrap(adder.getEnclaveKey()), privacyGroupId, org.web3j.utils.Restriction.RESTRICTED); return besuClient .eeaSendRawTransaction( Numeric.toHexString( PrivateTransactionEncoder.signMessage( privateTransaction, Credentials.create(adder.getTransactionSigningKey())))) .send() .getTransactionHash(); }
Example #2
Source File: PollingTransactionReceiptProcessor.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
private TransactionReceipt getTransactionReceipt( String transactionHash, long sleepDuration, int attempts) throws IOException, TransactionException { Optional<TransactionReceipt> receiptOptional = sendTransactionReceiptRequest(transactionHash); for (int i = 0; i < attempts; i++) { if (!receiptOptional.isPresent()) { try { Thread.sleep(sleepDuration); } catch (InterruptedException e) { throw new TransactionException(e); } receiptOptional = sendTransactionReceiptRequest(transactionHash); } else { return receiptOptional.get(); } } throw new TransactionException("Transaction receipt was not generated after " + ((sleepDuration * attempts) / 1000 + " seconds for transaction: " + transactionHash)); }
Example #3
Source File: Transfer.java From web3j with Apache License 2.0 | 6 votes |
private TransactionReceipt send( String toAddress, BigDecimal value, Convert.Unit unit, BigInteger gasPrice, BigInteger gasLimit) throws IOException, InterruptedException, TransactionException { BigDecimal weiValue = Convert.toWei(value, unit); if (!Numeric.isIntegerValue(weiValue)) { throw new UnsupportedOperationException( "Non decimal Wei value provided: " + value + " " + unit.toString() + " = " + weiValue + " Wei"); } String resolvedAddress = ensResolver.resolve(toAddress); return send(resolvedAddress, "", weiValue.toBigIntegerExact(), gasPrice, gasLimit); }
Example #4
Source File: PollingTransactionReceiptProcessor.java From client-sdk-java with Apache License 2.0 | 6 votes |
private TransactionReceipt getTransactionReceipt( String transactionHash, long sleepDuration, int attempts) throws IOException, TransactionException { Optional<TransactionReceipt> receiptOptional = sendTransactionReceiptRequest(transactionHash); for (int i = 0; i < attempts; i++) { if (!receiptOptional.isPresent()) { try { Thread.sleep(sleepDuration); } catch (InterruptedException e) { throw new TransactionException(e); } receiptOptional = sendTransactionReceiptRequest(transactionHash); } else { return receiptOptional.get(); } } throw new NoTransactionReceiptException("Transaction receipt was not generated after " + ((sleepDuration * attempts) / 1000 + " seconds for transaction: " + transactionHash)); }
Example #5
Source File: QueuingTransactionReceiptProcessor.java From client-sdk-java with Apache License 2.0 | 6 votes |
private void sendTransactionReceiptRequests() { for (RequestWrapper requestWrapper : pendingTransactions) { try { String transactionHash = requestWrapper.getTransactionHash(); Optional<TransactionReceipt> transactionReceipt = sendTransactionReceiptRequest(transactionHash); if (transactionReceipt.isPresent()) { callback.accept(transactionReceipt.get()); pendingTransactions.remove(requestWrapper); } else { if (requestWrapper.getCount() == pollingAttemptsPerTxHash) { throw new TransactionException( "No transaction receipt for txHash: " + transactionHash + "received after " + pollingAttemptsPerTxHash + " attempts"); } else { requestWrapper.incrementCount(); } } } catch (IOException | TransactionException e) { pendingTransactions.remove(requestWrapper); callback.exception(e); } } }
Example #6
Source File: WalletSendFunds.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
private TransactionReceipt performTransfer( Web3j web3j, String destinationAddress, Credentials credentials, BigDecimal amountInWei) { console.printf("Commencing transfer (this may take a few minutes) "); try { Future<TransactionReceipt> future = Transfer.sendFunds( web3j, credentials, destinationAddress, amountInWei, Convert.Unit.WEI) .sendAsync(); while (!future.isDone()) { console.printf("."); Thread.sleep(500); } console.printf("$%n%n"); return future.get(); } catch (InterruptedException | ExecutionException | TransactionException | IOException e) { exitError("Problem encountered transferring funds: \n" + e.getMessage()); } throw new RuntimeException("Application exit failure"); }
Example #7
Source File: Contract.java From etherscan-explorer with GNU General Public License v3.0 | 6 votes |
/** * Given the duration required to execute a transaction. * * @param data to send in transaction * @param weiValue in Wei to send in transaction * @return {@link Optional} containing our transaction receipt * @throws IOException if the call to the node fails * @throws TransactionException if the transaction was not mined while waiting */ TransactionReceipt executeTransaction( String data, BigInteger weiValue) throws TransactionException, IOException { TransactionReceipt receipt = send(contractAddress, data, weiValue, gasPrice, gasLimit); if (!receipt.isStatusOK()) { throw new TransactionException( String.format( "Transaction has failed with status: %s. " + "Gas used: %d. (not-enough gas?)", receipt.getStatus(), receipt.getGasUsed())); } return receipt; }
Example #8
Source File: ContractTest.java From client-sdk-java with Apache License 2.0 | 6 votes |
@Test public void testStaticGasProvider() throws IOException, TransactionException { ContractGasProvider gasProvider = new ContractGasProvider(BigInteger.TEN, BigInteger.ONE); TransactionManager txManager = mock(TransactionManager.class); when(txManager.executeTransaction(any(), any(), any(), any(), any())) .thenReturn(new TransactionReceipt()); contract = new TestContract(ADDRESS, web3j, txManager, gasProvider); Function func = new Function("test", Arrays.<Type>asList(), Collections.<TypeReference<?>>emptyList()); contract.executeTransaction(func); verify(txManager).executeTransaction(eq(BigInteger.TEN), eq(BigInteger.ONE), any(), any(), any()); }
Example #9
Source File: Transfer.java From web3j with Apache License 2.0 | 6 votes |
private TransactionReceipt sendEIP1559( String toAddress, BigDecimal value, Convert.Unit unit, BigInteger gasLimit, BigInteger gasPremium, BigInteger feeCap) throws IOException, InterruptedException, TransactionException { BigDecimal weiValue = Convert.toWei(value, unit); if (!Numeric.isIntegerValue(weiValue)) { throw new UnsupportedOperationException( "Non decimal Wei value provided: " + value + " " + unit.toString() + " = " + weiValue + " Wei"); } String resolvedAddress = ensResolver.resolve(toAddress); return sendEIP1559( resolvedAddress, "", weiValue.toBigIntegerExact(), gasLimit, gasPremium, feeCap); }
Example #10
Source File: Contract.java From web3j with Apache License 2.0 | 6 votes |
@Deprecated protected static <T extends Contract> T deploy( Class<T> type, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String binary, String encodedConstructor, BigInteger value) throws RuntimeException, TransactionException { return deploy( type, web3j, transactionManager, new StaticGasProvider(gasPrice, gasLimit), binary, encodedConstructor, value); }
Example #11
Source File: WalletSendFunds.java From client-sdk-java with Apache License 2.0 | 6 votes |
private TransactionReceipt performTransfer( Web3j web3j, String destinationAddress,String chainId, Credentials credentials, BigDecimal amountInWei) { console.printf("Commencing transfer (this may take a few minutes) "); try { Future<TransactionReceipt> future = Transfer.sendFunds( web3j, credentials, destinationAddress,chainId, amountInWei, Convert.Unit.LAT) .sendAsync(); while (!future.isDone()) { console.printf("."); Thread.sleep(500); } console.printf("$%n%n"); return future.get(); } catch (InterruptedException | ExecutionException | TransactionException | IOException e) { exitError("Problem encountered transferring funds: \n" + e.getMessage()); } throw new RuntimeException("Application exit failure"); }
Example #12
Source File: ContractTest.java From web3j with Apache License 2.0 | 6 votes |
@Test public void testTransactionFailedWithRevertReason() throws Exception { TransactionReceipt transactionReceipt = createFailedTransactionReceipt(); prepareCall(OWNER_REVERT_MSG_HASH); TransactionException thrown = assertThrows( TransactionException.class, () -> { prepareTransaction(transactionReceipt); contract.performTransaction( new Address(BigInteger.TEN), new Uint256(BigInteger.ONE)) .send(); }); assertEquals( String.format( "Transaction %s has failed with status: %s. Gas used: 1. Revert reason: '%s'.", TRANSACTION_HASH, TXN_FAIL_STATUS, OWNER_REVERT_MSG_STR), thrown.getMessage()); assertEquals(transactionReceipt, thrown.getTransactionReceipt().get()); }
Example #13
Source File: DelegateContract.java From client-sdk-java with Apache License 2.0 | 6 votes |
/** * 获得解除委托时所提取的委托收益(当减持/撤销委托成功时调用) * * @param transactionReceipt * @return * @throws TransactionException */ public BigInteger decodeUnDelegateLog(TransactionReceipt transactionReceipt) throws TransactionException { List<Log> logs = transactionReceipt.getLogs(); if(logs==null||logs.isEmpty()){ throw new TransactionException("TransactionReceipt logs is empty"); } String logData = logs.get(0).getData(); if(null == logData || "".equals(logData) ){ throw new TransactionException("TransactionReceipt logs[0].data is empty"); } RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData)); List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues(); String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes()); int statusCode = Integer.parseInt(decodedStatus); if(statusCode != ErrorCode.SUCCESS){ throw new TransactionException("TransactionResponse code is 0"); } return ((RlpString)((RlpList)RlpDecoder.decode(((RlpString)rlpList.get(1)).getBytes())).getValues().get(0)).asPositiveBigInteger(); }
Example #14
Source File: BaseContract.java From client-sdk-java with Apache License 2.0 | 6 votes |
private TransactionResponse getResponseFromTransactionReceipt(TransactionReceipt transactionReceipt) throws TransactionException { List<Log> logs = transactionReceipt.getLogs(); if(logs==null||logs.isEmpty()){ throw new TransactionException("TransactionReceipt logs is empty"); } String logData = logs.get(0).getData(); if(null == logData || "".equals(logData) ){ throw new TransactionException("TransactionReceipt log data is empty"); } RlpList rlp = RlpDecoder.decode(Numeric.hexStringToByteArray(logData)); List<RlpType> rlpList = ((RlpList)(rlp.getValues().get(0))).getValues(); String decodedStatus = new String(((RlpString)rlpList.get(0)).getBytes()); int statusCode = Integer.parseInt(decodedStatus); TransactionResponse transactionResponse = new TransactionResponse(); transactionResponse.setCode(statusCode); transactionResponse.setTransactionReceipt(transactionReceipt); return transactionResponse; }
Example #15
Source File: BesuOnChainPrivacyIntegrationTest.java From web3j with Apache License 2.0 | 5 votes |
@Test public void testCannotAddToAlreadyLockedGroup() throws Exception { Base64String privacyGroupId = Base64String.wrap(generateRandomBytes(32)); final String createTxHash = nodeAlice .privOnChainCreatePrivacyGroup( privacyGroupId, ALICE, ENCLAVE_KEY_ALICE, Collections.singletonList(ENCLAVE_KEY_BOB)) .send() .getTransactionHash(); TransactionReceipt createReceipt = processor.waitForTransactionReceipt(createTxHash); assertTrue(createReceipt.isStatusOK()); final String lockTxHash = nodeAlice .privOnChainSetGroupLockState( privacyGroupId, ALICE, ENCLAVE_KEY_ALICE, true) .send() .getTransactionHash(); TransactionReceipt lockReceipt = processor.waitForTransactionReceipt(lockTxHash); assertTrue(lockReceipt.isStatusOK()); assertThrows( TransactionException.class, () -> nodeBob.privOnChainAddToPrivacyGroup( privacyGroupId, BOB, ENCLAVE_KEY_BOB, Collections.singletonList(ENCLAVE_KEY_CHARLIE)) .send()); }
Example #16
Source File: QueuingTransactionReceiptProcessor.java From web3j with Apache License 2.0 | 5 votes |
@Override public TransactionReceipt waitForTransactionReceipt(String transactionHash) throws IOException, TransactionException { pendingTransactions.add(new RequestWrapper(transactionHash)); return new EmptyTransactionReceipt(transactionHash); }
Example #17
Source File: TransactionManager.java From web3j with Apache License 2.0 | 5 votes |
protected TransactionReceipt executeTransaction( BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value, boolean constructor) throws IOException, TransactionException { EthSendTransaction ethSendTransaction = sendTransaction(gasPrice, gasLimit, to, data, value, constructor); return processResponse(ethSendTransaction); }
Example #18
Source File: ContractTest.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Test public void testTransactionFailed() throws Exception { thrown.expect(TransactionException.class); thrown.expectMessage( "Transaction has failed with status: 0x0. Gas used: 1. (not-enough gas?)"); TransactionReceipt transactionReceipt = new TransactionReceipt(); transactionReceipt.setTransactionHash(TRANSACTION_HASH); transactionReceipt.setStatus("0x0"); transactionReceipt.setGasUsed("0x1"); prepareTransaction(transactionReceipt); contract.performTransaction( new Address(BigInteger.TEN), new Uint256(BigInteger.ONE)).send(); }
Example #19
Source File: ContractTest.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Test public void testContractDeployFails() throws Exception { thrown.expect(TransactionException.class); thrown.expectMessage( "Transaction has failed with status: 0x0. Gas used: 1. (not-enough gas?)"); TransactionReceipt transactionReceipt = createFailedTransactionReceipt(); deployContract(transactionReceipt); }
Example #20
Source File: ContractTest.java From web3j with Apache License 2.0 | 5 votes |
@Test public void testTimeout() throws IOException { prepareTransaction(null); TransactionManager transactionManager = getVerifiedTransactionManager(SampleKeys.CREDENTIALS, 1, 1); contract = new TestContract(ADDRESS, web3j, transactionManager, new DefaultGasProvider()); assertThrows(TransactionException.class, this::testErrorScenario); }
Example #21
Source File: PollingTransactionReceiptProcessorTest.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
@Test public void throwsTransactionExceptionWhenReceiptIsNotAvailableInTime() throws Exception { doReturn(requestReturning(response(null))) .when(web3j).ethGetTransactionReceipt(TRANSACTION_HASH); try { processor.waitForTransactionReceipt(TRANSACTION_HASH); fail("call should fail with TransactionException"); } catch (TransactionException e) { // this is expected } }
Example #22
Source File: PrivateTransactionManager.java From web3j with Apache License 2.0 | 5 votes |
@Override protected TransactionReceipt executeTransaction( BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value) throws IOException, TransactionException { EthSendTransaction ethSendTransaction = sendTransaction(gasPrice, gasLimit, to, data, value); return processResponse(ethSendTransaction); }
Example #23
Source File: QueuingTransactionReceiptProcessor.java From web3j with Apache License 2.0 | 5 votes |
private void sendTransactionReceiptRequests() { for (RequestWrapper requestWrapper : pendingTransactions) { try { String transactionHash = requestWrapper.getTransactionHash(); Optional<? extends TransactionReceipt> transactionReceipt = sendTransactionReceiptRequest(transactionHash); if (transactionReceipt.isPresent()) { callback.accept(transactionReceipt.get()); pendingTransactions.remove(requestWrapper); } else { if (requestWrapper.getCount() == pollingAttemptsPerTxHash) { throw new TransactionException( "No transaction receipt for txHash: " + transactionHash + "received after " + pollingAttemptsPerTxHash + " attempts", transactionHash); } else { requestWrapper.incrementCount(); } } } catch (IOException | TransactionException e) { pendingTransactions.remove(requestWrapper); callback.exception(e); } } }
Example #24
Source File: TransactionReceiptProcessor.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
Optional<TransactionReceipt> sendTransactionReceiptRequest( String transactionHash) throws IOException, TransactionException { EthGetTransactionReceipt transactionReceipt = web3j.ethGetTransactionReceipt(transactionHash).send(); if (transactionReceipt.hasError()) { throw new TransactionException("Error processing request: " + transactionReceipt.getError().getMessage()); } return transactionReceipt.getTransactionReceipt(); }
Example #25
Source File: Contract.java From web3j with Apache License 2.0 | 5 votes |
/** * Given the duration required to execute a transaction. * * @param data to send in transaction * @param weiValue in Wei to send in transaction * @return {@link Optional} containing our transaction receipt * @throws IOException if the call to the node fails * @throws TransactionException if the transaction was not mined while waiting */ TransactionReceipt executeTransaction( String data, BigInteger weiValue, String funcName, boolean constructor) throws TransactionException, IOException { TransactionReceipt receipt = send( contractAddress, data, weiValue, gasProvider.getGasPrice(funcName), gasProvider.getGasLimit(funcName), constructor); if (!receipt.isStatusOK()) { throw new TransactionException( String.format( "Transaction %s has failed with status: %s. " + "Gas used: %s. " + "Revert reason: '%s'.", receipt.getTransactionHash(), receipt.getStatus(), receipt.getGasUsedRaw() != null ? receipt.getGasUsed().toString() : "unknown", extractRevertReason(receipt, data, web3j, true)), receipt); } return receipt; }
Example #26
Source File: TransactionManager.java From etherscan-explorer with GNU General Public License v3.0 | 5 votes |
protected TransactionReceipt executeTransaction( BigInteger gasPrice, BigInteger gasLimit, String to, String data, BigInteger value) throws IOException, TransactionException { EthSendTransaction ethSendTransaction = sendTransaction( gasPrice, gasLimit, to, data, value); return processResponse(ethSendTransaction); }
Example #27
Source File: ContractTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Test public void testTransactionFailed() throws Exception { thrown.expect(TransactionException.class); thrown.expectMessage( "Transaction has failed with status: 0x0. Gas used: 1. (not-enough gas?)"); TransactionReceipt transactionReceipt = new TransactionReceipt(); transactionReceipt.setTransactionHash(TRANSACTION_HASH); transactionReceipt.setStatus("0x0"); transactionReceipt.setGasUsed("0x1"); prepareTransaction(transactionReceipt); contract.performTransaction( new Address(BigInteger.TEN), new Uint256(BigInteger.ONE)).send(); }
Example #28
Source File: ContractTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Test public void testContractDeployFails() throws Exception { thrown.expect(TransactionException.class); thrown.expectMessage( "Transaction has failed with status: 0x0. Gas used: 1. (not-enough gas?)"); TransactionReceipt transactionReceipt = createFailedTransactionReceipt(); deployContract(transactionReceipt); }
Example #29
Source File: PollingTransactionReceiptProcessorTest.java From client-sdk-java with Apache License 2.0 | 5 votes |
@Test public void throwsTransactionExceptionWhenReceiptIsNotAvailableInTime() throws Exception { doReturn(requestReturning(response(null))) .when(web3j).platonGetTransactionReceipt(TRANSACTION_HASH); try { processor.waitForTransactionReceipt(TRANSACTION_HASH); fail("call should fail with TransactionException"); } catch (TransactionException e) { // this is expected } }
Example #30
Source File: PrivacyRequestFactory.java From besu with Apache License 2.0 | 5 votes |
public String privxRemoveFromPrivacyGroup( final Base64String privacyGroupId, final PrivacyNode remover, final String toRemove) throws IOException, TransactionException { final BigInteger nonce = besuClient .privGetTransactionCount(remover.getAddress().toHexString(), privacyGroupId) .send() .getTransactionCount(); final Bytes payload = encodeRemoveFromGroupFunctionCall( Bytes.fromBase64String(remover.getEnclaveKey()), Bytes.fromBase64String(toRemove)); final RawPrivateTransaction privateTransaction = RawPrivateTransaction.createTransaction( nonce, BigInteger.valueOf(1000), BigInteger.valueOf(3000000), Address.ONCHAIN_PRIVACY_PROXY.toHexString(), payload.toHexString(), Base64String.wrap(remover.getEnclaveKey()), privacyGroupId, org.web3j.utils.Restriction.RESTRICTED); return besuClient .eeaSendRawTransaction( Numeric.toHexString( PrivateTransactionEncoder.signMessage( privateTransaction, Credentials.create(remover.getTransactionSigningKey())))) .send() .getTransactionHash(); }