Skip to main content

Use Case: Reactive Network Demo

Overview

This article focuses on building and deploying a reactive contract, using the basic Reactive Network Demo that provides:

  • Low-latency monitoring of logs emitted by contracts on the origin chain (Sepolia testnet).
  • Executing calls from the Reactive Network to contracts on the destination chain, also on Sepolia.

Basic Demo Smart Contract

Contracts

  • Origin Chain Contract: BasicDemoL1Contract receives Ether and returns it to the sender, emitting a Received event with transaction details.

  • Reactive Contract: BasicDemoReactiveContract subscribes to events on Sepolia, emits logs, and triggers callbacks when conditions are met, such as topic_3 being at least 0.1 Ether. It manages event subscriptions and tracks processed events.

function react(
uint256 chain_id,
address _contract,
uint256 topic_0,
uint256 topic_1,
uint256 topic_2,
uint256 topic_3,
bytes calldata data,
uint256 /* block_number */,
uint256 /* op_code */
) external vmOnly {
emit Event(chain_id, _contract, topic_0, topic_1, topic_2, topic_3, data, ++counter);
if (topic_3 >= 0.1 ether) {
bytes memory payload = abi.encodeWithSignature("callback(address)", address(0));
emit Callback(chain_id, _callback, GAS_LIMIT, payload);
}
}
  • Destination Chain Contract: BasicDemoL1Callback logs callback details upon receiving a call, capturing the origin, sender, and reactive sender addresses. It could also be a third-party contract.

Further Considerations

The demo highlights just a subset of Reactive Network's features. Potential improvements include:

  • Enhanced Event Subscriptions: Subscribing to multiple event origins, including callback logs, to maintain consistency.
  • Dynamic Subscriptions: Allowing real-time adjustments to subscriptions based on conditions.
  • State Management: Introducing persistent state handling for more complex, context-aware reactions.
  • Flexible Callbacks: Supporting arbitrary transaction payloads to increase adaptability.

Deployment & Testing

To deploy the contracts to Ethereum Sepolia, clone the project and follow these steps. Replace the relevant keys, addresses, and endpoints as needed. Make sure the following environment variables are correctly configured before proceeding:

Note: To receive REACT, send SepETH to the Reactive faucet on Ethereum Sepolia (0x9b9BB25f1A81078C544C829c5EB7822d747Cf434). An equivalent amount will be sent to your address.

Step 1

Deploy the BasicDemoL1Contract (origin chain contract) and assign the Deployed to address from the response to ORIGIN_ADDR.

forge create --rpc-url $SEPOLIA_RPC --private-key $SEPOLIA_PRIVATE_KEY src/demos/basic/BasicDemoL1Contract.sol:BasicDemoL1Contract

Step 2

Deploy the BasicDemoL1Callback (destination chain contract) and assign the Deployed to address from the response to CALLBACK_ADDR.

forge create --rpc-url $SEPOLIA_RPC --private-key $SEPOLIA_PRIVATE_KEY src/demos/basic/BasicDemoL1Callback.sol:BasicDemoL1Callback

Callback Payment

To ensure a successful callback, the callback contract must have an ETH balance. Find more details here. To fund the contract, run the following command:

cast send $CALLBACK_ADDR --rpc-url $SEPOLIA_RPC --private-key $SEPOLIA_PRIVATE_KEY --value 0.1ether

To cover the debt of the callback contact, run this command:

cast send --rpc-url $SEPOLIA_RPC --private-key $SEPOLIA_PRIVATE_KEY $CALLBACK_ADDR "coverDebt()"

Alternatively, you can deposit funds into the Callback Proxy contract on Sepolia, using the command below. The EOA address whose private key signs the transaction pays the fee.

cast send --rpc-url $SEPOLIA_RPC --private-key $SEPOLIA_PRIVATE_KEY $SEPOLIA_CALLBACK_PROXY_ADDR "depositTo(address)" $CALLBACK_ADDR --value 0.1ether

Step 3

Deploy the BasicDemoReactiveContract (reactive contract), configuring it to listen to ORIGIN_ADDR and to send callbacks to CALLBACK_ADDR. The Received event on the origin chain contract has a topic 0 value of 0x8cabf31d2b1b11ba52dbb302817a3c9c83e4b2a5194d35121ab1354d69f6a4cb, which we are monitoring.

forge create --rpc-url $REACTIVE_RPC --private-key $REACTIVE_PRIVATE_KEY src/demos/basic/BasicDemoReactiveContract.sol:BasicDemoReactiveContract --constructor-args $KOPLI_CALLBACK_PROXY_ADDR $ORIGIN_ADDR 0x8cabf31d2b1b11ba52dbb302817a3c9c83e4b2a5194d35121ab1354d69f6a4cb $CALLBACK_ADDR

Step 4

Test the whole setup by sending some ether to ORIGIN_ADDR:

cast send $ORIGIN_ADDR --rpc-url $SEPOLIA_RPC --private-key $SEPOLIA_PRIVATE_KEY --value 0.1ether

Ensure that the value sent is greater than or equal to 0.1 ether, as this is the minimum required value to trigger the process, which should eventually result in a callback transaction to CALLBACK_ADDR being initiated by the Reactive Network.

Conclusion

In this article, we explored how reactive contracts operate and deployed a simple system using one. We examined the process step by step, including event emission, tracking, and performing post-actions on the destination Ethereum contract through the reactive one. While this example doesn't include practical logic, it demonstrates the technology's functionality and sets the stage for real-world applications.