/** * 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) throws TransactionException, IOException { TransactionReceipt receipt = send(contractAddress, data, weiValue, gasProvider.getGasPrice(funcName), gasProvider.getGasLimit(funcName)); 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; }
private String createContract( Credentials credentials, BigInteger initialSupply) throws Exception { String createTransactionHash = sendCreateContractTransaction(credentials, initialSupply); assertFalse(createTransactionHash.isEmpty()); TransactionReceipt createTransactionReceipt = waitForTransactionReceipt(createTransactionHash); assertThat(createTransactionReceipt.getTransactionHash(), is(createTransactionHash)); assertFalse("Contract execution ran out of gas", createTransactionReceipt.getGasUsed().equals(GAS_LIMIT)); String contractAddress = createTransactionReceipt.getContractAddress(); assertNotNull(contractAddress); return contractAddress; }
@Test public void testContractCreation() throws Exception { boolean accountUnlocked = unlockAccount(); assertTrue(accountUnlocked); String transactionHash = sendTransaction(); assertFalse(transactionHash.isEmpty()); TransactionReceipt transactionReceipt = waitForTransactionReceipt(transactionHash); assertThat(transactionReceipt.getTransactionHash(), is(transactionHash)); assertFalse("Contract execution ran out of gas", transactionReceipt.getGasUsed().equals(GAS_LIMIT)); String contractAddress = transactionReceipt.getContractAddress(); assertNotNull(contractAddress); Function function = createFibonacciFunction(); String responseValue = callSmartContractFunction(function, contractAddress); assertFalse(responseValue.isEmpty()); List<Type> uint = FunctionReturnDecoder.decode( responseValue, function.getOutputParameters()); assertThat(uint.size(), is(1)); assertThat(uint.get(0).getValue(), equalTo(BigInteger.valueOf(13))); }
@Test public void testGreeterContract() throws Exception { boolean accountUnlocked = unlockAccount(); assertTrue(accountUnlocked); // create our smart contract String createTransactionHash = sendCreateContractTransaction(); assertFalse(createTransactionHash.isEmpty()); TransactionReceipt createTransactionReceipt = waitForTransactionReceipt(createTransactionHash); assertThat(createTransactionReceipt.getTransactionHash(), is(createTransactionHash)); assertFalse("Contract execution ran out of gas", createTransactionReceipt.getGasUsed().equals(GAS_LIMIT)); String contractAddress = createTransactionReceipt.getContractAddress(); assertNotNull(contractAddress); // call our getter Function getFunction = createGreetFunction(); String responseValue = callSmartContractFunction(getFunction, contractAddress); assertFalse(responseValue.isEmpty()); List<Type> response = FunctionReturnDecoder.decode( responseValue, getFunction.getOutputParameters()); assertThat(response.size(), is(1)); assertThat(response.get(0).getValue(), is(VALUE)); }
gas.equals(transactionReceipt.getGasUsed()));
@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())); }
subDetailsTextView.setText("Gas Used: " + transactionReceipt.getGasUsed() + "\n" + "Block Hash: " + transactionReceipt.getBlockHash());
private TransactionReceipt toReceipt(EthBlock.TransactionObject tx) { org.web3j.protocol.core.methods.response.TransactionReceipt receipt = web3JFacade.getReceipt(EthHash.of(tx.getHash())); boolean successful = !receipt.getGasUsed().equals(tx.getGas()); String error = ""; if(!successful) { error = "Error fromSeed RPC, all the gas was used"; } return new TransactionReceipt(EthHash.of(tx.getHash()), EthAddress.of(tx.getFrom()),EthAddress.of(tx.getTo()), EthAddress.of(receipt.getContractAddress()), error, EthData.empty(), successful); }