Human-Readable Push Notifications on Ethereum
ETHCC 7 · 11 Jul 2024 · 26:00
Anastasia Rodionova and Gheorghe Pinzaru lead a workshop on building human-readable push notifications for onchain transactions without relying heavily on a third-party API. They explain why raw transaction data is hard to read, since context must be gathered from receipts, events, traces and contract metadata, and why different contracts emit different events for the same action. The service they build has four steps: listen for events, decode the transaction, interpret it, and deliver the notification. They demo their open-source Loop Decoder library, Alchemy webhooks for the listening step and a Firebase task queue for delivery.
Speakers
Topics
From ETHCC 7, hosted by Ethereum France.
Transcript
Okay, are you ready? Before we start, this QR will link you to this presentation and some resources if you want to follow along. So let's get started. Uh hello, my name is George, and together with Anastasia, we will show you how to implement on-chain push notifications in a human-readable format without relying uh too much on a third-party API. So a quick overview. And in the end, we'll have some questions.
So the main motivation behind implementing push notifications for your app is to engage with your users. Push notifications are known to increase the engagement of the app because they prompt users to access the app once again, return to it, and interact basically with it again. And uh a good push notification should show the context in a such a format that user by looking at uh, for example, on their phone, they should understand what this transaction has done. And lately, we don't want to spend a lot of time and effort uh and money on maintaining such a system.
Uh, because of that, uh, yeah. Uh, for example, these are a couple of examples from some dabs that uh I've just looked up on Google, how uh apps present. I've changed the app, so nobody knows which app it is. But basically, you can see that some dApps they will show you that transaction 50 confirmed. What does it mean? What has actually happened to your account, you will not understand. So some users might be afraid that maybe it's uh somebody stole their assets, or who knows. So uh the problem uh with that is that blockchain is actually made in a way that it's optimized for writing.
Uh so when users look at transaction uh in on directly on the chain, they will see some random hacks. And even if they try to decode this uh let's assume they have the ABI, they decode it, and even at that point they might not understand what has happened. Because to actually understand the transactions, you need to pull the context from multiple data sources and aggregate them into one. And in the end, all this big data you need to basically interpret in a way to make it readable. Uh, the second problem is that there are so many contracts, and all these contracts implement the same thing, like for example, at DEX, they do one swap.
But when you look at the events that they emit, each will emit different types of events, a different order of arguments, and you basically cannot get one interpretation and apply to all the DEXs to understand that this was actually a swap. Yeah, so as you already maybe know, the just starting recording blockchain data requires some knowledge, nuances about blockchain itself and the contracts behind the transaction. One solution that a lot of developers are using is uh using third-party APIs.
But uh third-party APIs also bring some problems such as uh uh high dependency of the SDKs, maybe some uh breaking changes that uh they force you to implement. Uh also often phonetications, or it's often the pricing is obscure, you don't cannot well predict how much it will cost, or and even if you have a small usage, it also can be quite costly. And uh the most sometimes important problem is that uh they might do not support your specific chain because as you know, we have everyday new L2, now L3s, and uh it's sometimes quite difficult to find API provider who would support your DGN chain, let's say. And especially if you have some your own new contract on unverified contract, they can also do not support it.
Um so yeah, often third-party APIs, even if they have some solution for you, it's often a Trojan horse that's later, control your some other obstacles and problems. And today we will show you how to make a human readable notification service step by step. For the demo, we've created a repository with uh web push notifications, and we will explain some nuances behind and how we made it. And we also we're gonna use some uh we are not going to use third party API providers for data decoding.
Uh so the high-level architecture of our service is the following. Uh first we have to listen Ethereum events uh just to have real-time uh real-time uh hashes of our for our notifications. Um the second step would be decoding raw transaction data. So we have a hash and we need to make it to get all the information like uh from this hash. Third step is uh interpreting the transaction.
Uh, what do we mean by interpretation? It's like uh actually making it human readable, uh getting the most important information from the whole decoded data. And the last step it's the delivering notification for your end users. Um let's start with the first step: listening Ethereum events. Uh there are several ways how you can listen generally Ethereum events. You can just watch all the blocks on a RPC. If you have RPC URL, you can watch all the blocks.
Then you can the blocks will have just the list of all the new transactions. And you have to also get some additional information about each transaction and to apply your filters, your user filters to find which transaction is for you. And third solution, if in the case you have really like one million users or filters, alert, you might uh want to you might have to run even your own monitoring node.
Currently it's maybe possible with GEST or RETH, but still it's the hardest way, and only in case you will really need because you will have to do some infrastructure problems and support, etc. And for the demo, we will use just third-party API provider like Alchemy. It's really like a few lines of code to start listening to Ethereum events. You just need to specify chain, uh, address, uh, IPK, and then you will get notifications about all the address activities. Um but okay, we the second step would be decoding the transaction.
Okay, we have uh hashes for about every new transaction from the specific address that we are interested in, and so what we can do with it. Um so many developers think that they just need an ABI for to decode transaction, but you might quickly face some uh problems like proxy contracts, multicalls, uh different uh GRPC types of uh APIs uh and etc. So uh actually to decode the transactions you need to fetch some information like receipts, events, traces, ABIs, not just from the contract itself, but also from the interacted contracts.
You also need to have some uh a lot of tokens metadata because if you have some swap contract uh and you support a lot of tokens, plus NFTs uh and yeah, and also you need to have a way to resolve the contracts, uh proxy contracts. So uh uh that's why we created an open source library uh that's called loop decoder, which uh which actually makes fetching all the important information about your transactions much easier. And uh here on the right, just example of how decoded transactions, transactions looks like, and I will show you more details now.
So we have a playground where uh let's uh we have a few examples like Ava V2, and if you press repay, you will see here in the button a decoded transaction object. And if if you will check how what's how this object look like looks like. So it's just a big big JSON, but it has all the methods call information with all decoded details like all the params in human readable way plus trace calls, also all decoded and interactions. Interactions are the events.
And we also have interacted addresses. Okay. And we have uh also we we transform it uh we attach some also extra information such as transfers. For example, in this transaction happened the following transfers like from and and to address and which token it was. So with the help of the loop decoder library, you can get this uh object. No, okay.
It's it would be a step two. Uh and how in the code it looks like uh you can just uh set up an instance of transaction decoder, uh you can provide your JRPC client, you uh set up uh A B store or and contract metastore for caching, and just uh invoke the function decode transaction with chain ID and transaction hash, and you will get this big uh JSON objects with all the decoded information about transaction. And what it actually does under the hood, uh so yeah, it slots all the ABIs from multiple data sources like if scan for bytes, like uh everything where you can find EBIs.
Um it uh resolves metadata for NFTs and ERC2 tokens. Um it can provide caching with customizable storage, maybe you want only in memory caching or your own database. It batches the requests, uh decodes errors, and also handles all this all the nuances like multi-calls or proxy contracts. Um step three. Uh after we have this uh big JSON decoded object, we want actually something like uh in if your scan when we have the action string, uh what actually happened during this transaction to describe the to send the user size notification. And uh in this in uh in loop decoder library, we call it this step also interpretation, and how we do it, we create uh JavaScript transformation functions that uh takes uh such JavaScript transformation functions that uh receives uh transaction decoder uh decoder event uh like decoded transaction event, and it takes the most important information about transaction and returns a new event with the smaller one that has the action stream.
For example, for this transaction, the action was just user repaid the following amount of USDT. Yeah, and you can see, for example, you can modify it here. Just change the action, press interpret, and you will receive like another action string. Yeah, for the demo, we for the demo created like interpretations for several contracts, but we are going to have a library for the most popular contracts, which is uh just the JavaScript transformation functions that you can apply. Or if you have your own contract, you can easily write your own transformation.
Yeah, so now that we already know what a transaction does, it's time to deliver it to the users. So uh a push notification is not uh most of the people will think about sending it to uh your app on Android, iOS or web app. But actually, push notifications can be uh also telegram or or Slack alerts. Basically, you can set up an alert that if you have users that interact with a bigger amount of money, you may maybe want to be alerted to know of who these users are. Uh you can have uh posts, for example, in Farcaster, like you maybe you are familiar with while alerts every time there is a transaction that is maybe 100k or swap for a new token, you want to get alerted fast, or you can alert the your users that will want to follow you.
But for the purpose of this uh demo, we will look at uh sending them directly to the devices. And for that, we will use uh Firebase. Uh, Firebase actually provides uh a really good backend for delivering uh push notifications. So if you would like to send uh notification to iOS devices, you will need to have a specific format uh of JSON that you will need to sign and send to the Apple push notification service. And only after that, Apple will deliver it to your users. Uh if you want to send to Android, you will anyway you'll need to use uh Firebase Cloud Messaging because this is the way Google uh does that.
You can actually implement your own push notifications, but it might drain the battery of your users. And uh the third method that Firebase allows is sending to uh web pushes. With web push, you can send to directly to websites if users enable notifications, or to PWAs if they uh also uh install this PWA as an application on their home screen. Here uh we've modified a little bit this diagram to describe how uh high-level uh it might look for you. So initially you will have uh your own backend that's uh in our case, because we use a web hook, it will uh be triggered and uh we'll do these three steps.
Firstly, it will decode transaction, then it will interpret transactions, and it gets uh we will get all the subscribers that subscribe to a specific address. A subscriber is basically when a user registers to receive push notifications, they will give you a push notification token, basically a permission to send them uh these notifications. You will store it in our database, and with this token you can send. So a couple of suggestions that we have is to when sending push notification is to use a dispatcher pattern. And then you will dispatch it as a separate task of in Firebase to have a separate cloud functions.
Let's assume you have 500 users subscribe to one uh to one uh address. At this point, you will need to create this uh 500 objects, and if you have a small memory on the cloud function, the default one will be 250 megabytes. Then you will run out of memory and uh you will decode transactions, you will handle it as decoded, but the messages will not be delivered because of that. So you can uh with a task or if you use uh AWS Lambdas, you can create a pub uh with a pub sub or a queue, you will uh basically decode, submit to the queue, and the queue will actually ensure that your message is delivered. If it's uh the your function will fail multiple times, you will be notified and you can uh uh inspect what's uh expect inspect and understand what's the issue.
Uh then we recommend to clean up push notification tokens. Uh, usually a push notification token may expire in 90 days. So you can assume that if a push notification was not updated in 90 days, you can delete it. Uh also in the front end, you can when user does a logout, you can uh basically delete this notification token. And the third way to understand that a push notification token is expired. When you will try to deliver a push notification, you will receive an error message that token is expired or unavailable. If you will clean all these notification tokens in long term, you uh it will be very easy to manage.
Uh otherwise, every time you will receive a new transaction, you will fetch all the tokens and it will be a big table of tokens. You will try to process them and you'll spend a lot of time on execution and money on storing these tokens while they are invalid and you will never be able to send uh push notifications. And uh next, if you use decide to use web hooks, it's important to authorize requests. Uh Alchemy provides uh a request signing so you can verify the signature to ensure that uh request comes from Alchemy. But you can also set up some authorization, it's uh you can put just a header with uh unique token.
Uh it's uh in Web3, it's even more important to actually authorize these transactions because then uh attacker can send uh fake notifications to your users, and then they can basically call them on the phone and say that you are getting drained. Uh please send us your seed phrase. Uh yeah, so front end is actually not a part of our workshop, but we have it in our uh repository to for you to be able to test this service and see how it works and how it fits for you. Uh let me show uh do a quick uh let me quickly show you how our repository is structured.
Yeah, so if you scanned QR code in the beginning, we will also show you the QR code at the end. You will see a link to this repository. This repository uh contains the code for this workshop. Uh we have separated it into two folders. Uh the hosting folder is the one that hosts this minimal UI. What it does is just uh shows an input. You can enable push notification. Uh push notifications will basically register a service worker that will allow your browser to receive push notifications, and uh you have this input UI where you can put an address and subscribe to notifications to start receiving this. And second, uh, there is functions.
This is the cloud functions that are deployed directly on Firebase uh cloud functions. You can basically replace them with any of your services, it can be any Node.js service that runs or even a VS Lambdas. For um, yeah, let me quickly show you what functions we have. So for a minimal working demo, it's enough to have three functions. Uh first one is handle watch. Basically, when a user uh wants to watch an address, uh the only thing that we do is we just uh persist this address into database and create a new as you've seen in the previous uh example. We create a new webhook for Alchemy.
That's it. Uh then when uh Alchemy sends us a new request, we just handle a transaction. Uh handle a transaction will uh register a HTTP get request that can be called by uh HT post request that can be called by Alchemy. We will store only the hash, and then uh as Anastasia uh mentioned before, we will decode this transaction. And um to send uh push notifications, uh as I mentioned, we will use a queue. Uh the good part is that actually Firebase provides us with a queue mechanism.
It's uh we can create a task queue and just enqueue any new notification into it. And the third function would be just delivering of this push notifications. Uh some resources for this workshop. Uh the first link is uh the link to repository that I've just showed. Uh you have uh documentation for our decoder, the playground where you can play and see how you can decode your contract directly. Uh our socials, and if you have some feedback, you can join uh our telegram channel where we provide support for anyone who wants to use loop decoder to decode transactions.
Yeah, uh that's it from us. Yeah. Thank you. Uh going going back to the very first slide, I think where you had the MetaMask uh transaction 50, the one we all know when we make a transaction in the browser, right? The push notification. Lisa. It's it's not possible to replace that one, right? Like I'm just thinking if I had an app and I also wanted to show a user, hey, you just deposited I don't know two ether and two Aave, it they would see two push notifications, right?
One the MetaMask one and then the one I sent myself. Yes. Uh the if your user will use the MetaMask, then uh that's the case. Uh or any wallet, I guess most browser extended wallets. Yeah, uh most wallets uh can have uh the only thing is that this uh transactions are uh uh showed by MetaMask only when your user explicitly will send the transaction. Uh it might be not that important for them to know that transaction was confirmed. Uh but uh with approach that we showed, you uh use your users will also receive notifications when some transaction happened to their address uh without them executing them.
So at this point they will receive only your notification and the system. Yeah. Got it. Thank you. Anyone else? No, okay. Thank you for your call. Thank you.
Related talks
- AMA with Evgeny Yurtaev, Co-Founder of Zerion
- Illia Polosukhin: Near Protocol – From AI to High-Throughput Blockchain. Ep. 529
- Zero To Dapp
- Cyfrin Updraft: Full-Stack Web3 Development Crash Course
- Covalent 🛠 Building in Web3 with One Unified API
- Indexing blockchain data fast
- Sourcify: Towards Safer Contract Interactions for Humans
- Building Augur - Lessons Learned by Scott Bigelow (Devcon4)
- BuidlGuidl - Intro to Ethereum Development, Build & Deploy your first dApp