@Test public void testEthSendTransaction() { buildResponse( "{\n" + " \"id\":1,\n" + " \"jsonrpc\": \"2.0\",\n" + " \"result\": " + "\"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331\"\n" + "}" ); EthSendTransaction ethSendTransaction = deserialiseResponse(EthSendTransaction.class); assertThat(ethSendTransaction.getTransactionHash(), is("0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331")); }
private TransactionReceipt processResponse(EthSendTransaction transactionResponse) throws IOException, TransactionException { if (transactionResponse.hasError()) { throw new RuntimeException("Error processing transaction request: " + transactionResponse.getError().getMessage()); } String transactionHash = transactionResponse.getTransactionHash(); return transactionReceiptProcessor.waitForTransactionReceipt(transactionHash); }
@SuppressWarnings("unchecked") void prepareTransactionRequest() throws IOException { EthSendTransaction ethSendTransaction = new EthSendTransaction(); ethSendTransaction.setResult(TRANSACTION_HASH); Request<?, EthSendTransaction> rawTransactionRequest = mock(Request.class); when(rawTransactionRequest.send()).thenReturn(ethSendTransaction); when(web3j.ethSendRawTransaction(any(String.class))) .thenReturn((Request) rawTransactionRequest); }
public EthSendTransaction signAndSend(RawTransaction rawTransaction) throws IOException { String hexValue = sign(rawTransaction); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).send(); if (ethSendTransaction != null && !ethSendTransaction.hasError()) { String txHashLocal = Hash.sha3(hexValue); String txHashRemote = ethSendTransaction.getTransactionHash(); if (!txHashVerifier.verify(txHashLocal, txHashRemote)) { throw new TxHashMismatchException(txHashLocal, txHashRemote); } } return ethSendTransaction; } }
private String successOrThrow(EthSendTransaction ethSendTransaction) throws TransactionFailedException { if (ethSendTransaction.getError() != null) { throw new TransactionFailedException(ethSendTransaction.getError() .getMessage()); } else { return ethSendTransaction.getTransactionHash(); } }
@Test(expected = RuntimeException.class) @SuppressWarnings("unchecked") public void testInvalidTransactionResponse() throws Throwable { prepareNonceRequest(); EthSendTransaction ethSendTransaction = new EthSendTransaction(); ethSendTransaction.setError(new Response.Error(1, "Invalid transaction")); Request<?, EthSendTransaction> rawTransactionRequest = mock(Request.class); when(rawTransactionRequest.sendAsync()).thenReturn(Async.run(() -> ethSendTransaction)); when(web3j.ethSendRawTransaction(any(String.class))) .thenReturn((Request) rawTransactionRequest); testErrorScenario(); }
private String sendTransaction( Credentials credentials, String contractAddress, BigInteger gas, String encodedFunction) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); Transaction transaction = Transaction.createFunctionCallTransaction( credentials.getAddress(), nonce, Transaction.DEFAULT_GAS, gas, contractAddress, encodedFunction); org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction).sendAsync().get(); assertFalse(transactionResponse.hasError()); return transactionResponse.getTransactionHash(); }
@Ignore // TODO: Once account unlock functionality is available @Test public void testEthSendTransaction() throws Exception { EthSendTransaction ethSendTransaction = web3j.ethSendTransaction( config.buildTransaction()).send(); assertFalse(ethSendTransaction.getTransactionHash().isEmpty()); }
@Override public Single<String> createTransaction(Wallet from, String toAddress, BigInteger subunitAmount, BigInteger gasPrice, BigInteger gasLimit, byte[] data, String password) { final Web3j web3j = Web3jFactory.build(new HttpService(networkRepository.getDefaultNetwork().rpcServerUrl)); return Single.fromCallable(() -> { EthGetTransactionCount ethGetTransactionCount = web3j .ethGetTransactionCount(from.address, DefaultBlockParameterName.LATEST) .send(); return ethGetTransactionCount.getTransactionCount(); }) .flatMap(nonce -> accountKeystoreService.signTransaction(from, password, toAddress, subunitAmount, gasPrice, gasLimit, nonce.longValue(), data, networkRepository.getDefaultNetwork().chainId)) .flatMap(signedMessage -> Single.fromCallable( () -> { EthSendTransaction raw = web3j .ethSendRawTransaction(Numeric.toHexString(signedMessage)) .send(); if (raw.hasError()) { throw new ServiceException(raw.getError().getMessage()); } return raw.getTransactionHash(); })).subscribeOn(Schedulers.io()); }
private String sendTransaction() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); Transaction transaction = Transaction.createContractTransaction( ALICE.getAddress(), nonce, GAS_PRICE, GAS_LIMIT, BigInteger.ZERO, getFibonacciSolidityBinary()); org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
private String sendCreateContractTransaction() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); String encodedConstructor = FunctionEncoder.encodeConstructor(Collections.singletonList(new Utf8String(VALUE))); Transaction transaction = Transaction.createContractTransaction( ALICE.getAddress(), nonce, GAS_PRICE, GAS_LIMIT, BigInteger.ZERO, getGreeterSolidityBinary() + encodedConstructor); org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse = web3j.ethSendTransaction(transaction) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
@Test public void testTransferEther() throws Exception { unlockAccount(); BigInteger nonce = getNonce(ALICE.getAddress()); BigInteger value = Convert.toWei("0.5", Convert.Unit.ETHER).toBigInteger(); Transaction transaction = Transaction.createEtherTransaction( ALICE.getAddress(), nonce, GAS_PRICE, GAS_LIMIT, BOB.getAddress(), value); EthSendTransaction ethSendTransaction = web3j.ethSendTransaction(transaction).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); }
@Test public void testTransferEther() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); RawTransaction rawTransaction = createEtherTransaction( nonce, BOB.getAddress()); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); }
@Test public void testDeploySmartContract() throws Exception { BigInteger nonce = getNonce(ALICE.getAddress()); RawTransaction rawTransaction = createSmartContractTransaction(nonce); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, ALICE); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get(); String transactionHash = ethSendTransaction.getTransactionHash(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); assertFalse("Contract execution ran out of gas", rawTransaction.getGasLimit().equals(transactionReceipt.getGasUsed())); }
private String execute( Credentials credentials, Function function, String contractAddress) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); String encodedFunction = FunctionEncoder.encode(function); RawTransaction rawTransaction = RawTransaction.createTransaction( nonce, GAS_PRICE, GAS_LIMIT, contractAddress, encodedFunction); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
private String sendCreateContractTransaction( Credentials credentials, BigInteger initialSupply) throws Exception { BigInteger nonce = getNonce(credentials.getAddress()); String encodedConstructor = FunctionEncoder.encodeConstructor( Arrays.asList( new Uint256(initialSupply), new Utf8String("web3j tokens"), new Uint8(BigInteger.TEN), new Utf8String("w3j$"))); RawTransaction rawTransaction = RawTransaction.createContractTransaction( nonce, GAS_PRICE, GAS_LIMIT, BigInteger.ZERO, getHumanStandardTokenBinary() + encodedConstructor); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); String hexValue = Numeric.toHexString(signedMessage); EthSendTransaction transactionResponse = web3j.ethSendRawTransaction(hexValue) .sendAsync().get(); return transactionResponse.getTransactionHash(); }
private void sendTransaction(String sign){ try { Request<?, EthSendTransaction> rs = web3j.ethSendRawTransaction(sign); EthSendTransaction eSendTransaction = rs.sendAsync().get(); String res = eSendTransaction.getTransactionHash(); System.out.println("send hash : " + res); } catch (Exception e) { System.out.println("" + e); } } }
@InvokeOnHeader(Web3jConstants.ETH_SEND_TRANSACTION) void ethSendTransaction(Message message) throws IOException { String fromAddress = message.getHeader(Web3jConstants.FROM_ADDRESS, configuration::getFromAddress, String.class); String toAddress = message.getHeader(Web3jConstants.TO_ADDRESS, configuration::getToAddress, String.class); BigInteger nonce = message.getHeader(Web3jConstants.NONCE, configuration::getNonce, BigInteger.class); BigInteger gasPrice = message.getHeader(Web3jConstants.GAS_PRICE, configuration::getGasPrice, BigInteger.class); BigInteger gasLimit = message.getHeader(Web3jConstants.GAS_LIMIT, configuration::getGasLimit, BigInteger.class); BigInteger value = message.getHeader(Web3jConstants.VALUE, configuration::getValue, BigInteger.class); String data = message.getHeader(Web3jConstants.DATA, configuration::getData, String.class); org.web3j.protocol.core.methods.request.Transaction transaction = new org.web3j.protocol.core.methods.request.Transaction( fromAddress, nonce, gasPrice, gasLimit, toAddress, value, data); Request<?, EthSendTransaction> request = web3j.ethSendTransaction(transaction); setRequestId(message, request); EthSendTransaction response = request.send(); boolean hasError = checkForError(message, response); if (!hasError) { message.setBody(response.getTransactionHash()); } }
@InvokeOnHeader(Web3jConstants.ETH_SEND_RAW_TRANSACTION) void ethSendRawTransaction(Message message) throws IOException { String signedTransactionData = message.getHeader(Web3jConstants.SIGNED_TRANSACTION_DATA, configuration::getSignedTransactionData, String.class); Request<?, EthSendTransaction> request = web3j.ethSendRawTransaction(signedTransactionData); setRequestId(message, request); EthSendTransaction response = request.send(); boolean hasError = checkForError(message, response); if (!hasError) { message.setBody(response.getTransactionHash()); } }
@InvokeOnHeader(Web3jConstants.QUORUM_ETH_SEND_TRANSACTION) void quorumEthSendTransaction(Message message) throws IOException { if (quorum == null) { setQuorumEndpointError(message); return; } //the same as a regular transaction, but there is no gasPrice, instead there is optional privateFor String fromAddress = message.getHeader(Web3jConstants.FROM_ADDRESS, configuration::getFromAddress, String.class); String toAddress = message.getHeader(Web3jConstants.TO_ADDRESS, configuration::getToAddress, String.class); BigInteger nonce = message.getHeader(Web3jConstants.NONCE, configuration::getNonce, BigInteger.class); BigInteger gasLimit = message.getHeader(Web3jConstants.GAS_LIMIT, configuration::getGasLimit, BigInteger.class); BigInteger value = message.getHeader(Web3jConstants.VALUE, configuration::getValue, BigInteger.class); String data = message.getHeader(Web3jConstants.DATA, configuration::getData, String.class); List<String> privateFor = message.getHeader(Web3jConstants.PRIVATE_FOR, configuration::getPrivateFor, List.class); PrivateTransaction transaction = new PrivateTransaction(fromAddress, nonce, gasLimit, toAddress, value, data, privateFor); Request<?, EthSendTransaction> request = quorum.ethSendTransaction(transaction); setRequestId(message, request); EthSendTransaction response = request.send(); boolean hasError = checkForError(message, response); if (!hasError) { message.setBody(response.getTransactionHash()); } }