Monitoring Large Deposits with Zephyr and Sending Web Alerts
In this tutorial, we'll create a Zephyr program that monitors deposit events for a specific contract and sends a web request when a deposit exceeds a certain threshold.
Prerequisites:
A Mercury account
Zephyr SDK and Mercury CLI installed
Basic knowledge of Rust and Soroban
Step 1: Set up the project
Create a new Zephyr project using the Mercury CLI:
the --key ""
flag is temporarily needed as we're updating the Mercury tooling.
Step 2: Configure the project
Open the src/lib.rs
file and add the following imports that we will use and constants at the top:
Replace YOUR_CONTRACT_ID_HERE
with the actual contract ID you want to monitor, and update WEBHOOK_URL
with your desired endpoint for receiving alerts.
Step 3: Implement the main logic
Add the following on_close
function to your lib.rs
file:
This function does the following:
Initializes the Zephyr environment.
Converts the contract ID to the required format, in order to compare it to the
PrettyContractEvent.contract
filed.Filters events for the specific contract we're monitoring. Notice that we're using
env.reader().pretty()
to directly access the event fields.Checks each event to see if it's a "deposit" event and if the amount exceeds our threshold.
If conditions are met, it calls a function to send an alert.
Step 4: Implement helper functions
Add these helper functions to your lib.rs
file:
The send_large_deposit_alert
function sends a web request with the alert information, while format_amount
helps format the XLM amount properly.
Step 5: Deploy and test
Deploy your Zephyr program using the Mercury CLI (testnet deploy):
To test your program:
Set up a webhook receiver (e.g., using a service like RequestBin or a simple server you control).
Make sure your contract is emitting "deposit" events.
Trigger a deposit larger than the threshold (100 XLM in this case) to the monitored contract.
Check your webhook receiver for incoming alerts.
Review the Zephyr program logs in the Mercury dashboard for any debug messages or errors.
Conclusion
This tutorial demonstrated how to use Zephyr to:
Monitor specific events from a Stellar smart contract.
Filter and process these events efficiently.
Send web alerts for large deposits.
Use Zephyr's logging capabilities for debugging and monitoring.
You can extend this concept to monitor various types of events or implement more complex logic based on your specific needs. Remember to handle potential network issues and implement retries if necessary in a production environment.
Last updated