@Override public int hashCode() { int result = getHash() != null ? getHash().hashCode() : 0; result = 31 * result + (getNonceRaw() != null ? getNonceRaw().hashCode() : 0); result = 31 * result + (getBlockHash() != null ? getBlockHash().hashCode() : 0); result = 31 * result + (getBlockNumberRaw() != null ? getBlockNumberRaw().hashCode() : 0); result = 31 * result + (getTransactionIndexRaw() != null ? getTransactionIndexRaw().hashCode() : 0); result = 31 * result + (getFrom() != null ? getFrom().hashCode() : 0); result = 31 * result + (getTo() != null ? getTo().hashCode() : 0); result = 31 * result + (getValueRaw() != null ? getValueRaw().hashCode() : 0); result = 31 * result + (getGasPriceRaw() != null ? getGasPriceRaw().hashCode() : 0); result = 31 * result + (getGasRaw() != null ? getGasRaw().hashCode() : 0); result = 31 * result + (getInput() != null ? getInput().hashCode() : 0); result = 31 * result + (getCreates() != null ? getCreates().hashCode() : 0); result = 31 * result + (getPublicKey() != null ? getPublicKey().hashCode() : 0); result = 31 * result + (getRaw() != null ? getRaw().hashCode() : 0); result = 31 * result + (getR() != null ? getR().hashCode() : 0); result = 31 * result + (getS() != null ? getS().hashCode() : 0); result = 31 * result + BigInteger.valueOf(getV()).hashCode(); return result; } }
@Test public void testTransactionChainId() { Transaction transaction = new Transaction(); transaction.setV(0x25); assertThat(transaction.getChainId(), equalTo(1L)); }
private Transaction createTransaction(String transactionHash) { Transaction transaction = new Transaction(); transaction.setHash(transactionHash); return transaction; } }
public static Transaction fromEthTransaction(EthTransaction ethTransaction, Status status) { org.web3j.protocol.core.methods.response.Transaction transaction = ethTransaction.getTransaction(); String hash = transaction.getHash(); String from = transaction.getFrom(); String input = ethTransaction.getTransaction() .getInput(); String to = extractToFromEthTransaction(input); String value = extractValueFromEthTransaction(input); String contractAddress = ethTransaction.getTransaction() .getTo(); return new Transaction(hash, from, to, value, status); }
private static void newTransactionFilter(Web3j web3j) { Subscription subscription = web3j. transactionObservable(). subscribe(transaction -> { System.out.println("transaction come in"); System.out.println("transaction txHash " + transaction.getHash()); }); }
@Override public String getReceivingAddress() throws MissingTransactionInformationException { try { String address = web3jTransaction.getTo(); if (address == null || address.isEmpty()) throw new NoSuchElementException(); return address; } catch (Exception e) { throw new MissingTransactionInformationException("Couldn't fetch receiving address.", e); } }
@Test public void testEthGetTransactionByBlockHashAndIndex() throws Exception { BigInteger index = BigInteger.ONE; EthTransaction ethTransaction = web3j.ethGetTransactionByBlockHashAndIndex( config.validBlockHash(), index).send(); assertTrue(ethTransaction.getTransaction().isPresent()); Transaction transaction = ethTransaction.getTransaction().get(); assertThat(transaction.getBlockHash(), is(config.validBlockHash())); assertThat(transaction.getTransactionIndex(), equalTo(index)); }
@Test public void testEthGetTransactionByHash() throws Exception { EthTransaction ethTransaction = web3j.ethGetTransactionByHash( config.validTransactionHash()).send(); assertTrue(ethTransaction.getTransaction().isPresent()); Transaction transaction = ethTransaction.getTransaction().get(); assertThat(transaction.getBlockHash(), is(config.validBlockHash())); }
@Override public Long getBlockHeight() throws MissingTransactionInformationException { try { return web3jTransaction.getBlockNumber().longValue(); } catch (Exception e) { throw new MissingTransactionInformationException("Couldn't fetch block height.", e); } }
+ "}" ); Transaction transaction = new Transaction( "0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", "0x",
@Override public String getTransactionId() throws MissingTransactionInformationException { try { String id = web3jTransaction.getHash(); if (id == null || id.isEmpty()) throw new NoSuchElementException(); return id; } catch (Exception e) { throw new MissingTransactionInformationException("Couldn't fetch transaction id.", e); } }
private void monitorPendingTransactions() { web3j.pendingTransactionObservable().subscribe(web3jTx -> { try { if (!monitoredAddresses.contains(web3jTx.getTo())) return; processPendingTransactions( new EthereumTransactionAdapter(web3jTx, web3j, investorService)); } catch (Throwable t) { LOG.error("Error while processing transaction.", t); } }, t -> LOG.error("Error during scanning of pending transactions.", t)); }
@Test public void testEthGetTransactionByBlockNumberAndIndex() throws Exception { BigInteger index = BigInteger.ONE; EthTransaction ethTransaction = web3j.ethGetTransactionByBlockNumberAndIndex( DefaultBlockParameter.valueOf(config.validBlock()), index).send(); assertTrue(ethTransaction.getTransaction().isPresent()); Transaction transaction = ethTransaction.getTransaction().get(); assertThat(transaction.getBlockHash(), is(config.validBlockHash())); assertThat(transaction.getTransactionIndex(), equalTo(index)); }
private boolean isConfirmed(EthereumTransactionAdapter tx, BigInteger currentBlockNr) { BigInteger goalDepth = BigInteger.valueOf(configHolder.getEthereumConfirmationBlockdepth()); BigInteger depth = currentBlockNr.subtract(tx.getWeb3jTransaction().getBlockNumber()); return depth.compareTo(goalDepth) >= 0; }
if (getV() != that.getV()) { return false; if (getHash() != null ? !getHash().equals(that.getHash()) : that.getHash() != null) { return false; if (getNonceRaw() != null ? !getNonceRaw().equals(that.getNonceRaw()) : that.getNonceRaw() != null) { return false; if (getBlockHash() != null ? !getBlockHash().equals(that.getBlockHash()) : that.getBlockHash() != null) { return false; if (getBlockNumberRaw() != null ? !getBlockNumberRaw().equals(that.getBlockNumberRaw()) : that.getBlockNumberRaw() != null) { return false; if (getTransactionIndexRaw() != null ? !getTransactionIndexRaw().equals(that.getTransactionIndexRaw()) : that.getTransactionIndexRaw() != null) { return false; if (getFrom() != null ? !getFrom().equals(that.getFrom()) : that.getFrom() != null) { return false; if (getTo() != null ? !getTo().equals(that.getTo()) : that.getTo() != null) { return false;
private static void catchUpFilter(Web3j web3j) { BigInteger startBlock = BigInteger.valueOf(2000000); /** * 遍历旧区块,监听新区块 */ Subscription subscription = web3j.catchUpToLatestAndSubscribeToNewBlocksObservable( DefaultBlockParameter.valueOf(startBlock), false) .subscribe(block -> { System.out.println("block"); System.out.println(block.getBlock().getNumber()); }); /** * 遍历旧交易,监听新交易 */ Subscription subscription2 = web3j.catchUpToLatestAndSubscribeToNewTransactionsObservable( DefaultBlockParameter.valueOf(startBlock)) .subscribe(tx -> { System.out.println("transaction"); System.out.println(tx.getHash()); }); } }
private void monitorBuildingTransactions() { Long startBlock = configHolder.getEthereumNodeStartBlock(); web3j.catchUpToLatestAndSubscribeToNewTransactionsObservable(new DefaultBlockParameterNumber(startBlock)) .subscribe(web3jTx -> { try { if (!monitoredAddresses.contains(web3jTx.getTo())) return; EthereumTransactionAdapter tx = new EthereumTransactionAdapter( web3jTx, web3j, investorService); processBuildingTransaction(tx); BigInteger currentBlockNr = web3j.ethBlockNumber().send().getBlockNumber(); if (isConfirmed(tx, currentBlockNr)) { confirmTransaction(tx); } else { unconfirmedTransactions.add(tx); } } catch (Throwable t) { LOG.error("Error while processing transaction.", t); } }, t -> LOG.error("Error during scanning of transactions.", t)); }
@Test public void testTransactionLongChainId() { Transaction transaction = new Transaction(); transaction.setV(0x4A817C823L); assertThat(transaction.getChainId(), equalTo(10000000000L)); }
@Override public Date getBlockTime() throws MissingTransactionInformationException { if (this.blockTime == null) { try { Request<?, EthBlock> ethBlockRequest = web3j.ethGetBlockByNumber( new DefaultBlockParameterNumber(web3jTransaction.getBlockNumber()), false); EthBlock blockRequest = ethBlockRequest.send(); this.blockTime = new Date(blockRequest.getBlock().getTimestamp().longValue() * 1000); } catch (Exception e) { throw new MissingTransactionInformationException("Couldn't fetch block time.", e); } } return this.blockTime; }
private static void replayFilter(Web3j web3j) { BigInteger startBlock = BigInteger.valueOf(2000000); BigInteger endBlock = BigInteger.valueOf(2010000); /** * 遍历旧区块 */ Subscription subscription = web3j. replayBlocksObservable( DefaultBlockParameter.valueOf(startBlock), DefaultBlockParameter.valueOf(endBlock), false). subscribe(ethBlock -> { System.out.println("replay block"); System.out.println(ethBlock.getBlock().getNumber()); }); /** * 遍历旧交易 */ Subscription subscription1 = web3j. replayTransactionsObservable( DefaultBlockParameter.valueOf(startBlock), DefaultBlockParameter.valueOf(endBlock)). subscribe(transaction -> { System.out.println("replay transaction"); System.out.println("txHash " + transaction.getHash()); }); }