Java Code Examples for org.web3j.protocol.core.methods.request.Transaction#createEtherTransaction()

The following examples show how to use org.web3j.protocol.core.methods.request.Transaction#createEtherTransaction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadTimeoutAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void submittingTransactionWithoutNonceReturnsAGatewayTimeoutError() {
  final String recipient = "0x1b00ba00ca00bb00aa00bc00be00ac00ca00da00";
  final BigInteger transferAmountWei = Convert.toWei("15.5", Unit.ETHER).toBigIntegerExact();

  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          recipient,
          transferAmountWei);
  final SignerResponse<JsonRpcErrorResponse> signerResponse =
      ethSigner.transactions().submitExceptional(transaction);
  assertThat(signerResponse.status()).isEqualTo(GATEWAY_TIMEOUT);
  assertThat(signerResponse.jsonRpc().getError())
      .isEqualTo(CONNECTION_TO_DOWNSTREAM_NODE_TIMED_OUT);
}
 
Example 2
Source File: ValueTransferWithAzureAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void valueTransfer() {
  final BigInteger transferAmountWei =
      Convert.toWei("1.75", Convert.Unit.ETHER).toBigIntegerExact();
  final BigInteger startBalance = ethNode().accounts().balance(RECIPIENT);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  final String hash = ethSigner().transactions().submit(transaction);
  ethNode().transactions().awaitBlockContaining(hash);

  final BigInteger expectedEndBalance = startBalance.add(transferAmountWei);
  final BigInteger actualEndBalance = ethNode().accounts().balance(RECIPIENT);
  assertThat(actualEndBalance).isEqualTo(expectedEndBalance);
}
 
Example 3
Source File: ValueTransferWithHashicorpOnTlsAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void valueTransfer() {
  final BigInteger transferAmountWei =
      Convert.toWei("1.75", Convert.Unit.ETHER).toBigIntegerExact();
  final BigInteger startBalance = ethNode().accounts().balance(RECIPIENT);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  final String hash = ethSigner().transactions().submit(transaction);
  ethNode().transactions().awaitBlockContaining(hash);

  final BigInteger expectedEndBalance = startBalance.add(transferAmountWei);
  final BigInteger actualEndBalance = ethNode().accounts().balance(RECIPIENT);
  assertThat(actualEndBalance).isEqualTo(expectedEndBalance);
}
 
Example 4
Source File: ValueTransferWithHashicorpAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void valueTransfer() {
  final BigInteger transferAmountWei =
      Convert.toWei("1.75", Convert.Unit.ETHER).toBigIntegerExact();
  final BigInteger startBalance = ethNode().accounts().balance(RECIPIENT);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  final String hash = ethSigner().transactions().submit(transaction);
  ethNode().transactions().awaitBlockContaining(hash);

  final BigInteger expectedEndBalance = startBalance.add(transferAmountWei);
  final BigInteger actualEndBalance = ethNode().accounts().balance(RECIPIENT);
  assertThat(actualEndBalance).isEqualTo(expectedEndBalance);
}
 
Example 5
Source File: ValueTransferAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void valueTransferNonceTooLow() {
  valueTransfer(); // call this test to increment the nonce
  final BigInteger transferAmountWei = Convert.toWei("15.5", Unit.ETHER).toBigIntegerExact();
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          BigInteger.ZERO,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  final SignerResponse<JsonRpcErrorResponse> jsonRpcErrorResponseSignerResponse =
      ethSigner().transactions().submitExceptional(transaction);

  assertThat(jsonRpcErrorResponseSignerResponse.jsonRpc().getError())
      .isEqualTo(JsonRpcError.NONCE_TOO_LOW);
}
 
Example 6
Source File: ValueTransferAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void multipleValueTransfers() {
  final BigInteger transferAmountWei = Convert.toWei("1", Unit.ETHER).toBigIntegerExact();
  final BigInteger startBalance = ethNode().accounts().balance(RECIPIENT);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  String hash = null;
  for (int i = 0; i < FIFTY_TRANSACTIONS; i++) {
    hash = ethSigner().transactions().submit(transaction);
  }
  ethNode().transactions().awaitBlockContaining(hash);

  final BigInteger endBalance = ethNode().accounts().balance(RECIPIENT);
  final BigInteger numberOfTransactions = BigInteger.valueOf(FIFTY_TRANSACTIONS);
  assertThat(endBalance)
      .isEqualTo(startBalance.add(transferAmountWei.multiply(numberOfTransactions)));
}
 
Example 7
Source File: ValueTransferAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void senderIsNotUnlockedAccount() {
  final Account sender = new Account("0x223b55228fb22b89f2216b7222e5522b8222bd22");
  final String recipientAddress = "0x1b22ba22ca22bb22aa22bc22be22ac22ca22da22";
  final BigInteger senderStartBalance = ethNode().accounts().balance(sender);
  final BigInteger recipientStartBalance = ethNode().accounts().balance(recipientAddress);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          sender.address(),
          sender.nextNonce(),
          GAS_PRICE,
          INTRINSIC_GAS,
          recipientAddress,
          senderStartBalance);

  final SignerResponse<JsonRpcErrorResponse> signerResponse =
      ethSigner().transactions().submitExceptional(transaction);
  assertThat(signerResponse.status()).isEqualTo(BAD_REQUEST);
  assertThat(signerResponse.jsonRpc().getError())
      .isEqualTo(SIGNING_FROM_IS_NOT_AN_UNLOCKED_ACCOUNT);

  final BigInteger senderEndBalance = ethNode().accounts().balance(sender);
  final BigInteger recipientEndBalance = ethNode().accounts().balance(recipientAddress);
  assertThat(senderEndBalance).isEqualTo(senderStartBalance);
  assertThat(recipientEndBalance).isEqualTo(recipientStartBalance);
}
 
Example 8
Source File: ValueTransferAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void valueTransferFromAccountWithInsufficientFunds() {
  final String recipientAddress = "0x1b11ba11ca11bb11aa11bc11be11ac11ca11da11";
  final BigInteger senderStartBalance = ethNode().accounts().balance(richBenefactor());
  final BigInteger recipientStartBalance = ethNode().accounts().balance(recipientAddress);
  final BigInteger transferAmountWei = senderStartBalance.add(BigInteger.ONE);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          richBenefactor().nextNonce(),
          GAS_PRICE,
          INTRINSIC_GAS,
          recipientAddress,
          transferAmountWei);

  final SignerResponse<JsonRpcErrorResponse> signerResponse =
      ethSigner().transactions().submitExceptional(transaction);
  assertThat(signerResponse.status()).isEqualTo(BAD_REQUEST);
  assertThat(signerResponse.jsonRpc().getError())
      .isEqualTo(TRANSACTION_UPFRONT_COST_EXCEEDS_BALANCE);

  final BigInteger senderEndBalance = ethNode().accounts().balance(richBenefactor());
  final BigInteger recipientEndBalance = ethNode().accounts().balance(recipientAddress);
  assertThat(senderEndBalance).isEqualTo(senderStartBalance);
  assertThat(recipientEndBalance).isEqualTo(recipientStartBalance);
}
 
Example 9
Source File: DataPathFeatureFlagAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void valueTransfer() {
  final BigInteger transferAmountWei = Convert.toWei("1.75", Unit.ETHER).toBigIntegerExact();
  final BigInteger startBalance = ethNode().accounts().balance(RECIPIENT);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  final String hash = ethSigner().transactions().submit(transaction);
  ethNode().transactions().awaitBlockContaining(hash);

  final BigInteger expectedEndBalance = startBalance.add(transferAmountWei);
  final BigInteger actualEndBalance = ethNode().accounts().balance(RECIPIENT);
  assertThat(actualEndBalance).isEqualTo(expectedEndBalance);
}
 
Example 10
Source File: MultiKeyTransactionSigningAcceptanceTestBase.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
void performTransaction() {
  final BigInteger transferAmountWei = Convert.toWei("1.75", Unit.ETHER).toBigIntegerExact();

  final BigInteger startBalance = ethNode.accounts().balance(RECIPIENT);
  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          RECIPIENT,
          transferAmountWei);

  final String hash = ethSigner.transactions().submit(transaction);
  ethNode.transactions().awaitBlockContaining(hash);

  final BigInteger expectedEndBalance = startBalance.add(transferAmountWei);
  final BigInteger actualEndBalance = ethNode.accounts().balance(RECIPIENT);
  assertThat(actualEndBalance).isEqualTo(expectedEndBalance);
}
 
Example 11
Source File: ConnectionTimeoutAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void submittingTransactionWithoutNonceReturnsAGatewayTimeoutError() {
  final String recipient = "0x1b00ba00ca00bb00aa00bc00be00ac00ca00da00";
  final BigInteger transferAmountWei = Convert.toWei("15.5", Unit.ETHER).toBigIntegerExact();

  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          null,
          GAS_PRICE,
          INTRINSIC_GAS,
          recipient,
          transferAmountWei);
  final SignerResponse<JsonRpcErrorResponse> signerResponse =
      ethSigner.transactions().submitExceptional(transaction);
  assertThat(signerResponse.status()).isEqualTo(GATEWAY_TIMEOUT);
  assertThat(signerResponse.jsonRpc().getError())
      .isEqualTo(CONNECTION_TO_DOWNSTREAM_NODE_TIMED_OUT);
}
 
Example 12
Source File: ReadTimeoutAcceptanceTest.java    From ethsigner with Apache License 2.0 6 votes vote down vote up
@Test
public void submittingTransactionReturnsAGatewayTimeoutError() {
  final String recipient = "0x1b00ba00ca00bb00aa00bc00be00ac00ca00da00";
  final BigInteger transferAmountWei = Convert.toWei("15.5", Unit.ETHER).toBigIntegerExact();

  final Transaction transaction =
      Transaction.createEtherTransaction(
          richBenefactor().address(),
          richBenefactor().nextNonceAndIncrement(),
          GAS_PRICE,
          INTRINSIC_GAS,
          recipient,
          transferAmountWei);
  final SignerResponse<JsonRpcErrorResponse> signerResponse =
      ethSigner.transactions().submitExceptional(transaction);
  assertThat(signerResponse.status()).isEqualTo(GATEWAY_TIMEOUT);
  assertThat(signerResponse.jsonRpc().getError())
      .isEqualTo(CONNECTION_TO_DOWNSTREAM_NODE_TIMED_OUT);
}
 
Example 13
Source File: PassThroughAcceptanceTest.java    From ethsigner with Apache License 2.0 5 votes vote down vote up
void submitTransactionAndWaitForBlock() {

    final Transaction transaction =
        Transaction.createEtherTransaction(
            richBenefactor().address(),
            null,
            GAS_PRICE,
            INTRINSIC_GAS,
            RECIPIENT,
            TRANSFER_AMOUNT_WEI);

    final String hash = ethSigner().transactions().submit(transaction);
    ethNode().transactions().awaitBlockContaining(hash);
  }
 
Example 14
Source File: AbstractEthereumTest.java    From web3j_demo with Apache License 2.0 5 votes vote down vote up
String transferWei(String from, String to, BigInteger amountWei) throws Exception {
	BigInteger nonce = getNonce(from);
	Transaction transaction = Transaction.createEtherTransaction(
			from, nonce, Web3jConstants.GAS_PRICE, Web3jConstants.GAS_LIMIT_ETHER_TX, to, amountWei);

	EthSendTransaction ethSendTransaction = web3j.ethSendTransaction(transaction).sendAsync().get();
	System.out.println("transferEther. nonce: " + nonce + " amount: " + amountWei + " to: " + to);

	String txHash = ethSendTransaction.getTransactionHash(); 
	waitForReceipt(txHash);
	
	return txHash;
}
 
Example 15
Source File: BaseIntegrationTest.java    From eventeum with Apache License 2.0 5 votes vote down vote up
protected String sendTransaction() throws ExecutionException, InterruptedException, IOException {
    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            CREDS.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();
    BigInteger nonce = ethGetTransactionCount.getTransactionCount();

    final Transaction tx = Transaction.createEtherTransaction(CREDS.getAddress(),
            nonce, GAS_PRICE, GAS_LIMIT, ZERO_ADDRESS, BigInteger.ONE);

    EthSendTransaction response = web3j.ethSendTransaction(tx).send();

    return response.getTransactionHash();
}
 
Example 16
Source File: SendTransaction.java    From BitcoinWallet with MIT License 5 votes vote down vote up
public void sendTransaction(String fromAddress, String toAddress, BigDecimal amount, String password, WalletFile walletfile) throws Exception {
    BigInteger mNonce = getNonce(fromAddress);
    BigInteger gasPrice = getGasPrice();
    BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
    Transaction transaction = Transaction.createEtherTransaction(fromAddress, null,
            null, null, toAddress, value);
    BigInteger gasLimit = getGasLimit(transaction);
    String sign = new TransactionManager().signedEthTransactionData(toAddress, mNonce, gasPrice,
            gasLimit, amount, walletfile, password);
    sendTransaction(sign);

}
 
Example 17
Source File: TransferEtherTest.java    From web3j_demo with Apache License 2.0 4 votes vote down vote up
/**
 * Ether transfer tests using methods {@link Transaction#createEtherTransaction()}, and {@link Web3j#ethSendTransaction()}.
 * Sending account needs to be unlocked for this to work.   
 */
@Test
public void testCreateAndSendTransaction() throws Exception {

	String from = getCoinbase();
	BigInteger nonce = getNonce(from);
	String to = Alice.ADDRESS;
	BigInteger amountWei = Convert.toWei("0.456", Convert.Unit.ETHER).toBigInteger();

	// this is the method to test here
	Transaction transaction = Transaction
			.createEtherTransaction(
					from, 
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					to, 
					amountWei);
	

	// record account balances before the transfer
	BigInteger fromBalanceBefore = getBalanceWei(from);
	BigInteger toBalanceBefore = getBalanceWei(to);

	// send the transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendTransaction(transaction)
			.sendAsync()
			.get();

	String txHash = ethSendTx.getTransactionHash();
	assertFalse(txHash.isEmpty());
	
	TransactionReceipt txReceipt = waitForReceipt(txHash);
	BigInteger txFee = txReceipt.getCumulativeGasUsed().multiply(Web3jConstants.GAS_PRICE);

	// coinbase might have gotten additional funds from mining
	BigInteger fromMinimumBalanceExpected = fromBalanceBefore.subtract(amountWei.add(txFee));
	BigInteger fromBalanceActual = getBalanceWei(from);
	BigInteger fromBalanceDelta = fromBalanceActual.subtract(fromMinimumBalanceExpected);
	
	System.out.println("testCreateAndSendTransaction balance difference=" + fromBalanceDelta + " likely cause block reward (5 Ethers) and tx fees (" + txFee + " Weis)");
	assertTrue("Unexected balance for 'from' address. difference=" + fromBalanceDelta, fromBalanceDelta.signum() >= 0);
	assertEquals("Unexected balance for 'to' address", toBalanceBefore.add(amountWei), getBalanceWei(to));		
}
 
Example 18
Source File: SendEtherIT.java    From web3j with Apache License 2.0 4 votes vote down vote up
@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);

    assertEquals(transactionReceipt.getTransactionHash(), (transactionHash));
}
 
Example 19
Source File: SendEtherIT.java    From etherscan-explorer with GNU General Public License v3.0 4 votes vote down vote up
@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));
}
 
Example 20
Source File: TransferDemo.java    From web3j_demo with Apache License 2.0 4 votes vote down vote up
/**
 * Implementation of the Ethers transfer.
 * <ol>
 *   <li>Get nonce for for the sending account</li>
 *   <li>Create the transaction object</li>
 *   <li>Send the transaction to the network</li>
 *   <li>Wait for the confirmation</li>
 * </ol>
 */
void demoTransfer(String fromAddress, String toAddress, BigInteger amountWei)
		throws Exception
{
	System.out.println("Accounts[1] (to address) " + toAddress + "\n" + 
			"Balance before Tx: " + Web3jUtils.getBalanceEther(web3j, toAddress) + "\n");

	System.out.println("Transfer " + Web3jUtils.weiToEther(amountWei) + " Ether to account");

	// step 1: get the nonce (tx count for sending address)
	EthGetTransactionCount transactionCount = web3j
			.ethGetTransactionCount(fromAddress, DefaultBlockParameterName.LATEST)
			.sendAsync()
			.get();

	BigInteger nonce = transactionCount.getTransactionCount();
	System.out.println("Nonce for sending address (coinbase): " + nonce);

	// step 2: create the transaction object
	Transaction transaction = Transaction
			.createEtherTransaction(
					fromAddress, 
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					toAddress, 
					amountWei);

	// step 3: send the tx to the network
	EthSendTransaction response = web3j
			.ethSendTransaction(transaction)
			.sendAsync()
			.get();

	String txHash = response.getTransactionHash();		
	System.out.println("Tx hash: " + txHash);

	// step 4: wait for the confirmation of the network
	TransactionReceipt receipt = Web3jUtils.waitForReceipt(web3j, txHash);
	
	BigInteger gasUsed = receipt.getCumulativeGasUsed();
	System.out.println("Tx cost: " + gasUsed + " Gas (" + 
			Web3jUtils.weiToEther(gasUsed.multiply(Web3jConstants.GAS_PRICE)) +" Ether)\n");

	System.out.println("Balance after Tx: " + Web3jUtils.getBalanceEther(web3j, toAddress));
}