My Goal Better Explained:
I want to âWatch + Syncâ an address and wish to have a function fired in realtime to send i.e. âan emailâ when a tx in relation to that address happens.
I can currently achieve this whole aim, but the current method Iâm using breaks and appears not best practice (as weâve discussed).
Current Method:
The address provided by a user is saved into a class called âWatchedEthAddressâ.
âWatch + Syncâ cloud function initiatesâsaving latest txsâthat include the address in to âEthTransactionsâ (query monitors âto_addressâ in this example).
CloudFile.js:
Moralis.Cloud.run("watchEthAddress", {
address,
sync_historical: false,
});
// create query
const query = new Moralis.Query("EthTransactions");
query.equalTo("to_address", address);
// subscribe to query updates ** add this**
const subscription = await query.subscribe();
handleNewTransaction(subscription);
// run query
const results = await query.find();
logger.info("user transactions:", results);
async function handleNewTransaction(subscription) {
// log each new transaction
subscription.on("create", function (data) {
// log alert
logger.info("ALERT: New Transaction");
logger.info(data);
logger.info("---");
// send alert here: via email i.e. SendGrid
});
}
});
Main Question:
How do I âlisten/subscribeâ to the âEthTransactionsâ table when it adds a new entry so I can then utilise the new rowâs data in an asynchronous alert?
Let me know if this still isnât explained explicitly what I need.