Skip to content

Watch ETH Events on Java SDK

Zhou Zhiqiang edited this page Jan 10, 2023 · 16 revisions

Introduction

When depositing and withdrawing assets between ETH and L2, it emits "events" when the transaction is completed. The event could be used as the signal for a transaction to be completed successfully.

Example

The following example assumes that you have Java and Maven installed.

The following example contains a demo application for watching events of depositing NTF from ETH to L2.

Step 1: Create a maven project and import dependency

Create new maven project by:

mvn archetype:generate -DgroupId=com.example.app -DartifactId=reddio-example-watch-events -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false -DarchetypeVersion=1.4

Then add the reddio-api dependency:

<dependency>
    <groupId>com.reddio</groupId>
    <artifactId>reddio-api</artifactId>
    <version>0.0.15</version>
</dependency>

Step 2: codes about watching all of Deposit events

There are example codes for watching Deposit events:

public class App 
{
    public static void main( String[] args )
    {
        DefaultReddioRestClient restClient = DefaultReddioRestClient.testnet();
        DefaultEthereumInteraction ethereumInteraction = DefaultEthereumInteraction.build(
                restClient,
                DefaultEthereumInteraction.GOERIL_ID,
                // replace with your eth node address
                "https://eth-goerli.g.alchemy.com/v2/<your-api-key>",
                // we do not need private key for this example
                "0x0"
        );
        // object mapper for JSON serialization
        ObjectMapper om = new ObjectMapper();
        // notice the method watchDeposit would not block the thread, it runs in background, and returns Disposable for cancellation
        Disposable disposable = ethereumInteraction.watchDeposit((it) -> {
            try {
                // once received the event, print it
                String asJson = om.writeValueAsString(it);
                System.out.println(asJson);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        });


        try {
            Thread.sleep(Duration.ofSeconds(600).toMillis());

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // stop watching
        disposable.dispose();
    }
}

Please notice that the default behavior of watchDeposit is:

  • watching events from the latest block
  • emit events with at least 16 block confirmation

To change the behavior, you could use other overloads of watchDeposit, and change the parameters: startBlockNumber and requiredBlockConfirmation.

Step 3: run the application and give it a try

You could run the application, then deposit some assets on https://demos.reddio.com. As the transaction completes, you could see the output like this on the console:

{"log":{"removed":false,"logIndex":72,"transactionIndex":23,"transactionHash":"0x93df32e6a5adc801580f00a9f0681986314059c63fd0134f522ae94859b59b38","blockHash":"0x85036bf550a8caca902ef3b43499e53cf2788fee36c2130e4041e6cc2ca01a44","blockNumber":8281301,"address":"0x8eb82154f314ec687957ce1e9c1a5dc3a3234df9","data":"0x00000000000000000000000076f2fc7ed90039d986e3eb4db294f05e160c8f0301c2847406b96310a32c379536374ec034b732633e8675860f20f4141e701ff4000000000000000000000000000000000000000000000000000000000165153c0352f9ffd821a525051de2d71126113505a7b0a73d98dbc0ac0ff343cfbdef5e00000000000000000000000000000000000000000000000000005af3107a40000000000000000000000000000000000000000000000000000000000000000064","type":null,"topics":["0x06724742ccc8c330a39a641ef02a0b419bd09248360680bb38159b0a8c2635d6"],"logIndexRaw":"0x48","transactionIndexRaw":"0x17","blockNumberRaw":"0x7e5cd5"},"depositorEthKey":"0x76f2fc7ed90039d986e3eb4db294f05e160c8f03","starkKey":795995337730000219295083279675166181323994377230540967472507439971862323188,"vaultId":23401788,"assetType":1503545437449673444103803151627333814355897720185181380161647770114038034270,"nonQuantizedAmount":100000000000000,"quantizedAmount":100}