-
Notifications
You must be signed in to change notification settings - Fork 1
Watch ETH Events on Java SDK
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.18</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 {
final Deposits.LogDepositEventResponse event = it.component1();
final EthBlock ethBlock = it.component2();
// once received the event, print it
String asJson = om.writeValueAsString(event);
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 start from the 16 blocks ahead of the latest block
- emit events with at least 16 block confirmations
To change the behavior, you could use other overloads of watchDeposit
, and change the parameters: startBlockNumber
and requiredBlockConfirmation
.
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
}
watchDeposit
is the method for watching LogDeposit
events. watchDeposit
is a non-blocking method, it runs in background, and returns Disposable
for cancellation.
There are 3 overload for watchDeposit
:
Disposable watchDeposit(Consumer<Tuple2<Deposits.LogDepositEventResponse, EthBlock>> consumer);
Disposable watchDeposit(Consumer<Tuple2<Deposits.LogDepositEventResponse, EthBlock>> consumer, BigInteger startBlockNumber);
Disposable watchDeposit(Consumer<Tuple2<Deposits.LogDepositEventResponse, EthBlock>> consumer, BigInteger startBlockNumber, Long requiredBlockConfirmation);
Parameter consumer
should resolve the event, implement your own logic here.
The first overload is the default behavior, it watches events from the 16 blocks ahead of the latest block, and emit events with at least 16 block confirmations. The rest two overloads are for customizing the behavior.
Deposits.LogDepositEventResponse
is the class for the event, it contains all the information of the event:
- field
log
includes the raw log data, including metadata like transaction hash, block number, and so on. - field
depositorEthKey
is the ethereum address of the depositor. - field
starkKey
is the stark key of the depositor, the type isBigInteger
, you could convert it to hex string withstarkKey.toString(16)
. - field
assetId
,assetType
andvaultId
identify the asset on L2, ref: https://docs.starkware.co/starkex/spot/shared/starkex-specific-concepts.html#assetinfo_assettype_and_assetid_spot - field
nonQuantizedAmount
andquantizedAmount
are the amount of the asset.
watchNftDeposit
is the method for watching LogNftDeposit
events. watchNftDeposit
is a non-blocking method, it runs in background, and returns Disposable
for cancellation.
There are 3 overload for watchNftDeposit
:
Disposable watchNftDeposit(Consumer<Tuple2<Deposits.LogNftDepositEventResponse, EthBlock>> consumer);
Disposable watchNftDeposit(Consumer<Tuple2<Deposits.LogNftDepositEventResponse, EthBlock>> consumer, BigInteger startBlockNumber);
Disposable watchNftDeposit(Consumer<Tuple2<Deposits.LogNftDepositEventResponse, EthBlock>> consumer, BigInteger startBlockNumber, Long requiredBlockConfirmation);
Parameter consumer
should resolve the event, implement your own logic here.
The first overload is the default behavior, it watches events from the 16 blocks ahead of the latest block, and emit events with at least 16 block confirmations. The rest two overloads are for customizing the behavior.
Deposits.LogNftDepositEventResponse
is the class for the event, it contains all the information of the event:
- field
log
includes the raw log data, including metadata like transaction hash, block number, and so on. - field
depositorEthKey
is the ethereum address of the depositor. - field
tokenId
is the token id of the NFT. - field
starkKey
is the stark key of the depositor, the type isBigInteger
, you could convert it to hex string withstarkKey.toString(16)
. - field
assetId
,assetType
andvaultId
identify the asset on L2, ref: https://docs.starkware.co/starkex/spot/shared/starkex-specific-concepts.html#assetinfo_assettype_and_assetid_spot