public RawTransactionManager(Web3j web3j, Credentials credentials, byte chainId) { super(web3j, credentials.getAddress()); this.web3j = web3j; this.credentials = credentials; this.chainId = chainId; }
public RawTransactionManager( Web3j web3j, Credentials credentials, byte chainId, TransactionReceiptProcessor transactionReceiptProcessor) { super(transactionReceiptProcessor, credentials.getAddress()); this.web3j = web3j; this.credentials = credentials; this.chainId = chainId; }
public RawTransactionManager( Web3j web3j, Credentials credentials, byte chainId, int attempts, long sleepDuration) { super(web3j, attempts, sleepDuration, credentials.getAddress()); this.web3j = web3j; this.credentials = credentials; this.chainId = chainId; }
protected BigInteger getNonce() throws IOException { EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount( credentials.getAddress(), DefaultBlockParameterName.PENDING).send(); return ethGetTransactionCount.getTransactionCount(); }
private RemoteCall<TransactionReceipt> createTransaction( Transfer transfer, BigInteger gasPrice) { return transfer.sendFunds( BOB.getAddress(), BigDecimal.valueOf(1.0), Convert.Unit.KWEI, gasPrice, Transfer.GAS_LIMIT); } }
private void verify(Credentials credentials) { assertThat(credentials.getAddress(), is(SampleKeys.ADDRESS)); assertThat(credentials.getEcKeyPair(), is(SampleKeys.KEY_PAIR)); } }
private BigInteger estimateGas(String encodedFunction) throws Exception { EthEstimateGas ethEstimateGas = web3j.ethEstimateGas( Transaction.createEthCallTransaction(ALICE.getAddress(), null, encodedFunction)) .sendAsync().get(); // this was coming back as 50,000,000 which is > the block gas limit of 4,712,388 // see eth.getBlock("latest") return ethEstimateGas.getAmountUsed().divide(BigInteger.valueOf(100)); }
boolean unlockAccount() throws Exception { PersonalUnlockAccount personalUnlockAccount = web3j.personalUnlockAccount( ALICE.getAddress(), WALLET_PASSWORD, ACCOUNT_UNLOCK_DURATION) .sendAsync().get(); return personalUnlockAccount.accountUnlocked(); }
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(); }
@Test public void testTransfer() throws Exception { TransactionReceipt transactionReceipt = Transfer.sendFunds( web3j, ALICE, BOB.getAddress(), BigDecimal.valueOf(0.2), Convert.Unit.ETHER) .send(); assertFalse(transactionReceipt.getBlockHash().isEmpty()); } }
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(); }
@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)); }
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(); }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction( ALICE.getAddress(), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); } }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction( ALICE.getAddress(), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); }
private String callSmartContractFunction( Function function, String contractAddress) throws Exception { String encodedFunction = FunctionEncoder.encode(function); org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall( Transaction.createEthCallTransaction( ALICE.getAddress(), contractAddress, encodedFunction), DefaultBlockParameterName.LATEST) .sendAsync().get(); return response.getValue(); }
@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 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(); }
@Test public void testSignTransaction() throws Exception { boolean accountUnlocked = unlockAccount(); assertTrue(accountUnlocked); RawTransaction rawTransaction = createTransaction(); byte[] encoded = TransactionEncoder.encode(rawTransaction); byte[] hashed = Hash.sha3(encoded); EthSign ethSign = web3j.ethSign(ALICE.getAddress(), Numeric.toHexString(hashed)) .sendAsync().get(); String signature = ethSign.getSignature(); assertNotNull(signature); assertFalse(signature.isEmpty()); }
@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())); }