-
Notifications
You must be signed in to change notification settings - Fork 1
Watch ETH Events on Java SDK
Zhou Zhiqiang edited this page Jan 9, 2023
·
16 revisions
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.
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.
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>
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();
}
}