Ethereum Transaction Sending Method Difference between send and sendAsync

Article content

There are send method and sendAsync method to send a signed transaction using Ethereum's Java library "Web3j". In this article, I described how to send a transaction using send, but I also implemented the method using sendAsync, so I will keep it as a record. Send signed transaction using Web3j

environment

Implementation

The code.

SendTransaction.java


 public TransactionReceipt sendTransactionAsync(Credentials credentials, String password, String toAddress, long value) {

    CompletableFuture<TransactionReceipt> receipt = null;
      try {
        //Transaction generation
        // "personal_unlockAccount"Send a request and receive a response
        PersonalUnlockAccount unlockAccountResponse = web3j.personalUnlockAccount(
              credentials.getAddress(), //address
              password  //password
              ).send();

        //If the unlock is successful, send Ether
        if (unlockAccountResponse.getResult()) {
          //Send a Transaction.
          receipt = Transfer.sendFunds(web3j, credentials, toAddress, BigDecimal.valueOf(value), Unit.ETHER).sendAsync();
        }

          return receipt.get();
        }catch(IOException | TransactionException ex) {
          ex.printStackTrace();
        }catch(Exception ex) {
          ex.printStackTrace();
        }
      return null;
    }

The differences are as follows.

SendTransaction.java


receipt = Transfer.sendFunds(web3j, credentials, toAddress, BigDecimal.valueOf(value), Unit.ETHER).send();

SendTransaction.java


receipt = Transfer.sendFunds(web3j, credentials, toAddress, BigDecimal.valueOf(value), Unit.ETHER).sendAsync();

Send does not return a response until the transaction is included in the block. sendAsync returns a response when a transaction is sent, so you don't have to wait for it to be captured in a block.

However, when using the get method of "CompletableFuture " obtained as the result of executing sendAsync, when getting TransactionReceipt, no response is returned until it is captured in the block.

It seems better to use sendAsync when you don't have to wait for a transaction to get into a block.

Recommended Posts

Ethereum Transaction Sending Method Difference between send and sendAsync
Difference between instance method and class method
Difference between render method and redirect_to
Difference between == operator and eqals method
[Rails] Difference between create method and new + save method
Difference between vh and%
Difference between i ++ and ++ i
Difference between product and variant
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is the difference between an action and an instance method?
Let's override the difference between == (identity) and equals method (equivalence)
[Ruby] Difference between get and post
Difference between interface and abstract class
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
Easy to understand the difference between Ruby instance method and class method.
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Rails] I studied the difference between new method, save method, build method and create method.
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable
Difference between pop () and peek () in stack
[For beginners] Difference between Java and Kotlin
Difference between isEmpty and isBlank of StringUtils
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between addPanel and presentModally of FloatingPanel
[Ruby] Difference between print, puts and p