@Override public String getTransactionHash() { return super.getTransactionHash(); }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof TransactionReceipt)) { return false; } TransactionReceipt that = (TransactionReceipt) o; return getTransactionHash() != null ? getTransactionHash().equals(that.getTransactionHash()) : that.getTransactionHash() == null; }
@Override public int hashCode() { int result = getTransactionHash() != null ? getTransactionHash().hashCode() : 0; result = 31 * result + (transactionIndex != null ? transactionIndex.hashCode() : 0); result = 31 * result + (getBlockHash() != null ? getBlockHash().hashCode() : 0); result = 31 * result + (blockNumber != null ? blockNumber.hashCode() : 0); result = 31 * result + (cumulativeGasUsed != null ? cumulativeGasUsed.hashCode() : 0); result = 31 * result + (gasUsed != null ? gasUsed.hashCode() : 0); result = 31 * result + (getContractAddress() != null ? getContractAddress().hashCode() : 0); result = 31 * result + (getRoot() != null ? getRoot().hashCode() : 0); result = 31 * result + (getStatus() != null ? getStatus().hashCode() : 0); result = 31 * result + (getFrom() != null ? getFrom().hashCode() : 0); result = 31 * result + (getTo() != null ? getTo().hashCode() : 0); result = 31 * result + (getLogs() != null ? getLogs().hashCode() : 0); result = 31 * result + (getLogsBloom() != null ? getLogsBloom().hashCode() : 0); return result; }
if (getTransactionHash() != null ? !getTransactionHash().equals(that.getTransactionHash()) : that.getTransactionHash() != null) { return false;
@Test public void testEthGetTransactionReceipt() throws Exception { EthGetTransactionReceipt ethGetTransactionReceipt = web3j.ethGetTransactionReceipt( config.validTransactionHash()).send(); assertTrue(ethGetTransactionReceipt.getTransactionReceipt().isPresent()); TransactionReceipt transactionReceipt = ethGetTransactionReceipt.getTransactionReceipt().get(); assertThat(transactionReceipt.getTransactionHash(), is(config.validTransactionHash())); }
pendingTransactions.put(transactionReceipt.getTransactionHash(), new Object()); for (TransactionReceipt transactionReceipt : transactionReceipts) { assertFalse(transactionReceipt.getBlockHash().isEmpty()); pendingTransactions.remove(transactionReceipt.getTransactionHash()); transactionReceipts.remove(transactionReceipt);
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; }
public void sendTransferFromTransaction( Credentials credentials, String from, String to, BigInteger value, String contractAddress) throws Exception { Function function = transferFrom(from, to, value); String functionHash = execute(credentials, function, contractAddress); TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash); assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash)); List<Log> logs = transferTransactionReceipt.getLogs(); assertFalse(logs.isEmpty()); Log log = logs.get(0); Event transferEvent = transferEvent(); List<String> topics = log.getTopics(); // check function signature - we only have a single topic our event signature, // there are no indexed parameters in this example String encodedEventSignature = EventEncoder.encode(transferEvent); assertThat(topics.get(0), is(encodedEventSignature)); assertThat(new Address(topics.get(1)), is(new Address(from))); assertThat(new Address(topics.get(2)), is(new Address(to))); // verify qty transferred List<Type> results = FunctionReturnDecoder.decode( log.getData(), transferEvent.getNonIndexedParameters()); assertThat(results, equalTo(Collections.singletonList(new Uint256(value)))); }
private void sendApproveTransaction( Credentials credentials, String spender, BigInteger value, String contractAddress) throws Exception { Function function = approve(spender, value); String functionHash = execute(credentials, function, contractAddress); TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash); assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash)); List<Log> logs = transferTransactionReceipt.getLogs(); assertFalse(logs.isEmpty()); Log log = logs.get(0); // verify the event was called with the function parameters List<String> topics = log.getTopics(); assertThat(topics.size(), is(3)); // event Transfer(address indexed _from, address indexed _to, uint256 _value); Event event = approvalEvent(); // check function signature - we only have a single topic our event signature, // there are no indexed parameters in this example String encodedEventSignature = EventEncoder.encode(event); assertThat(topics.get(0), is(encodedEventSignature)); assertThat(new Address(topics.get(1)), is(new Address(credentials.getAddress()))); assertThat(new Address(topics.get(2)), is(new Address(spender))); // verify our two event parameters List<Type> results = FunctionReturnDecoder.decode( log.getData(), event.getNonIndexedParameters()); assertThat(results, equalTo(Collections.singletonList(new Uint256(value)))); }
@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))); }
private void sendTransferTokensTransaction( Credentials credentials, String to, String contractAddress, BigInteger qty) throws Exception { Function function = transfer(to, qty); String functionHash = execute(credentials, function, contractAddress); TransactionReceipt transferTransactionReceipt = waitForTransactionReceipt(functionHash); assertThat(transferTransactionReceipt.getTransactionHash(), is(functionHash)); List<Log> logs = transferTransactionReceipt.getLogs(); assertFalse(logs.isEmpty()); Log log = logs.get(0); // verify the event was called with the function parameters List<String> topics = log.getTopics(); assertThat(topics.size(), is(3)); Event transferEvent = transferEvent(); // check function signature - we only have a single topic our event signature, // there are no indexed parameters in this example String encodedEventSignature = EventEncoder.encode(transferEvent); assertThat(topics.get(0), is(encodedEventSignature)); assertThat(new Address(topics.get(1)), is(new Address(credentials.getAddress()))); assertThat(new Address(topics.get(2)), is(new Address(to))); // verify qty transferred List<Type> results = FunctionReturnDecoder.decode( log.getData(), transferEvent.getNonIndexedParameters()); assertThat(results, equalTo(Collections.singletonList(new Uint256(qty)))); }
@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)); }
private void run(String walletFileLocation, String destinationAddress) { File walletFile = new File(walletFileLocation); Credentials credentials = getCredentials(walletFile); console.printf("Wallet for address " + credentials.getAddress() + " loaded\n"); if (!WalletUtils.isValidAddress(destinationAddress) && !EnsResolver.isValidEnsName(destinationAddress)) { exitError("Invalid destination address specified"); } Web3j web3j = getEthereumClient(); BigDecimal amountToTransfer = getAmountToTransfer(); Convert.Unit transferUnit = getTransferUnit(); BigDecimal amountInWei = Convert.toWei(amountToTransfer, transferUnit); confirmTransfer(amountToTransfer, transferUnit, amountInWei, destinationAddress); TransactionReceipt transactionReceipt = performTransfer( web3j, destinationAddress, credentials, amountInWei); console.printf("Funds have been successfully transferred from %s to %s%n" + "Transaction hash: %s%nMined block number: %s%n", credentials.getAddress(), destinationAddress, transactionReceipt.getTransactionHash(), transactionReceipt.getBlockNumber()); }
@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())); }
@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)); }
private <T, R> TransactionResponse<R> processEventResponse( List<T> eventResponses, TransactionReceipt transactionReceipt, Function<T, R> map) { if (!eventResponses.isEmpty()) { return new TransactionResponse<>( transactionReceipt.getTransactionHash(), map.apply(eventResponses.get(0))); } else { return new TransactionResponse<>( transactionReceipt.getTransactionHash()); } }
public String pay(String paymentToETHAddress, BigDecimal amount) throws Exception { LOG.debug("ETH: Sending funds to {}, amount {}.", paymentToETHAddress, amount.toPlainString()); BigDecimal amountInWei = Convert.toWei(amount, Convert.Unit.ETHER); TransactionReceipt transactionReceipt = Transfer.sendFunds( web3j, credentials, paymentToETHAddress, amountInWei, Convert.Unit.WEI).send(); return transactionReceipt.getTransactionHash(); }
public static Transaction fromEthGetTransactionReceipt( EthGetTransactionReceipt ethGetTransactionReceipt) { TransactionReceipt transactionReceipt = ethGetTransactionReceipt.getTransactionReceipt(); String hash = transactionReceipt.getTransactionHash(); String from = transactionReceipt.getFrom(); Log log = transactionReceipt.getLogs() .get(0); String to = log.getAddress(); String value = extractValueFromEthGetTransactionReceipt(log.getData()); Status status = parseStatus(transactionReceipt.getStatus()); String contractAddress = ethGetTransactionReceipt.getTransactionReceipt() .getTo(); return new Transaction(hash, from, to, value, status); }
assertThat(transactionReceipt.getTransactionHash()).isNotBlank(); assertThat(transactionReceipt.getTransactionIndex()).isNotNull(); assertThat(transactionReceipt.getTransactionHash()).isNotNull();