Encode Club x ETH Safari Hackathon: Tenderly Technical Workshop Part 2
Encode Club x ETHSafari Hackathon 2022 · 9 Sept 2022 · 46:23
Nenad Vitrović from Tenderly continues the hackathon workshop series with a code-focused look at monitoring smart contracts. Using a simple multisig wallet, the session sets up alerts for transactions, function calls, events and event parameters that notify a Discord channel, then adds serverless JavaScript reactions that run when something happens onchain. It also shows manual triggers and running those reactions locally.
Speakers
Topics
From Encode Club x ETHSafari Hackathon 2022, hosted by Encode.
Transcript
hi everyone welcome to the tenderly part 2 workshop event um we have naynard here giving us some more um knowledge and more specifically about the code um while he sets up and gets ready why don't you box what you're building um we'd love to see what you're all working on um also if i could please ask that you all put your um microphones on mute um just so it's there's less distractions and if possible please could you put your cameras on because it's really nice chatting when we can see your faces um that would be very much appreciated again welcome to this workshop i hope
you get some good um knowledge out of it and if you have any questions pop it in the chat box nanad would be happy to answer it in the end um and yeah i'll be posting some links there too so keep your eyes on the chat box i hope you enjoy and i'll hand over to nanad oh yeah i thought i immediately so as i said hi everybody welcome to the second generally workshop last time we attempted showing how tenderly features work from the perspective of the dashboard and we went through some of the most important ones and uh through a critical step of contract verification and today we'll see what
what additional value can we have and what can we actually do are in the area of monitoring smart contracts um the goal for today would be to go through a multi-sig wallet smart contract just briefly and then build a monitoring monitoring around it so we could be aware when somebody sends a transaction or executes a transaction approves the transaction and on top of that we'll build another layer which is a serverless vacuum for that this means that we'll write some javascript code and that javascript code
will execute when somebody submits a new transaction approves that executed and so on and that means we can react when something happens on the chain so when a smart contract is called we can have our our specific javascript code running in response to those events uh so this is just a brief overview and we'll go through all of this in far more detail uh in a moment and uh i'll continue continue where i left off the last time so just a brief overview of what we covered last time we covered
uh the smart contract registry should we call it where you can add the smart contracts we're interested in uh you can observe the transactions that uh happen uh on chain uh of course involving those smart contracts you're interested in and uh we've seen the debugger so the tool that allows you to inspect how transaction went uh that state changes it incurred events that were emitted and so on and then we took it further with simulation so taking any simulation happening on chain and uh any transaction happening on chain and simulating its execution of course while changing some of the parameters or
changing the the the values of storage slots within the smart contracts and uh today we're going to deal uh today we're going to deal uh with so we developed this today we're dealing with alerts and web reactions so if you think of it the these three three things that we saw first they allow you to actually observe what's happening they allow you to dig in and dig deep to understand the behavior of your smart contracts the interactions with other smart contracts and how transactions play out
um so it's that it gives you that kind of understanding and this part is more oriented in you being more passive less inquisitive but setting up ways uh that says setting up tools that allow you to be well well alert when something that's interesting to you happens and interesting to you that may be a particular address from a list of addresses called your smart contract or your smart contract received a value that's higher than some amount and so on or your smart contract is being drained which is something that you
would also uh want to be alert as it happens um so this is what alerts are for and we see them in action really soon and the second thing is alerting is fine you'll get an alert every single time calls your smart contract but we can we can be even more lazy so being a lazy programmer you're going to write some code that will respond to any event happening on chain so you can actually automate uh your processes you can for example pause a smart contract or when somebody initiates a process you encode it within the smart contract you can react to it
by collecting some data by sending messages with aggregated data that you collect to slack to discord to telegram uh you can even tweet about it it's your code and you pretty much can do anything from it so these are the two things that we're going to cover this second part as i said puts you in a more passive position as things happen on shade but of course it requires you to write some code set up some some things so you can actually you know reduce anxiety and relax a little bit in that regard all right so here i have visual studio code and
please let me know if if this is if the font is big enough uh just go a thumbs up if this is big enough or you'd like to put thumbs down if needs to get bigger anybody i'll keep it here all right so uh today we'll just deal with a very rudimentary version of the multisig wallet and multiplicity wallet is a thing that's happening it's actually a thing you can use to submit transactions that are pending to be executed and they get executed when enough people
let's say uh approved the execution of the transaction so your regular wallet uh you want to do something you click a button or do something like that or just use ethers and the transaction goes to the network but if you're in an organization one way uh you can use to have agreement from the people within the organization is by using the same wallet it's not the only way but it is one of the the ways good enough for the examples we're going to do here so the idea is that you submit the transaction to the multisig wallet and this is what events tx submission stands for and then people who are owners of
the wallet so it's people with special addresses that belong to the organization or foreign organization they can confirm a transaction because uh for example executing it will yield uh some results that you like or you can skip confirming uh if you think it's a bad idea and when there's enough of confirmations then you can execute the transaction where is transaction stored well the whole transaction is actually stored within the the wallet itself the multi-state wallet this is the transaction array so the transaction thingy uh that we see here it's
um yeah this structure over here and it just has the address of the recipient the value carries the data it carries so it can be used to invoke a second smart contract and we're also keeping track whether or not it's executed and the number of confirmations it got from the owners of the multi-c wallet so just to recap multisig wallet it's got owners people who somehow form the organization and whose opinion on transactions matter uh we have an array of transactions and the way you use the multisig wallet you submit a transaction to the multisig wallet and over the course of time you have to ask
people hey can you approve my transaction can you upload my transactions so maybe two or three approve and if you have enough approvals then you can safely execute the transaction so what we want to do here we don't really need to go through the code because i think i explained the dynamics and the behavior of the the the wallet but we have functions that are used to well uh submit a transaction and if we open it up it just pushes it to the array and then it's the event confirmed transaction so this one gets the transaction from the transactions array in contract storage increments the
number of confirmations and emits the event that tx confirmation happened and of course we have execute transaction and here we can see that okay we're getting the transaction from the storage we're fetching it and we're checking whether or not it has the requirement that the number of information you've got is greater or equal to the number of required confirmations so if it's five of you within your organization using democracy you can configure it so two approvals are good enough three approvals are good enough or if you're really strict four or even five approvals are good enough so this is something configurable and this is the contract we're going to
to work with all right so first let's set up an alert and for alerts i created this little discord server that i use for for these testing purposes and here's a demo in code so let me go back to tenderly and go back to alerting and i'm going to create an alert for every single successful transaction that happens on a contract multi-c contract so i deployed this one previously it's one of the contracts within the project so i'll select that one and i'm gonna go next and for destinations
uh well we do have other discord that i used and another discord that i used so let me try adding a new one over here so this one will be eth safari and the discord webhook i'll copy it here demo code integrations and i do have a webhook that is already present here just copy the the url come back to the dashboard and paste it over here and when i add the webhook it's present it just takes a moment to enable it and if i save it all right we got it now
what we can do is we could either issue a transaction right now or of course you're trying to figure out if this works without waiting for transactions to happen you can test this alert and in order to test it you will need a reference to a transaction that already happened so we need to have already a transaction and we need its cash so we have to back off from the screen and i'm going to go back to transactions so here we can see a bunch of transactions involving this multi-c wallet so i'll just take this one submit transaction and i will copy
the hash of that one okay so far so good going back to alerts and entering the one that we just created and let me take this chord and pull it back here all right so if i go trigger test alert paste the address and we know that it's robson and trigger test here we go we got an alert and you can see from here that it's not really rich because it says hey you've got a new alert from your demo in code etw safari this is the name of my project
and the says it's a it's a successful transaction in multisig and this is the transaction that actually triggered this alert so that's the transaction cache i actually passed so this gives us enough confidence that when a real transaction happens you can actually you will actually get this sort of message all right now before we move on uh i think it's a good idea to see other alert types that we have in here so you can really do a lot of stuff successful and failed transactions okay those are sort of expected you can go
function called function call alerts so whenever a function in your contract gets called and let's just see how this one looks so you can pick an address this is actually taking a contract a network so uh the ones from all the addresses happening on that network or alerts for every project uh every address in the project that you're following and so on i'll stick to you to this one and uh i'll take this contract's address and you can pick here any of these functions that are present within the multisig so you can go really really you can set up
monitoring and improve your observability of the contracts using this and i'll just go through the other ones so event admitted so this one happens whenever your contract actually emits an event so here you get a list of all the events that are defined within the contracts within the smart contract you just picked so our multisig wallet and then we have event parameter so this one goes even crazier you can set up an alert that goes off when a particular parameter of the event uh for example is bigger than some value or less than some value so you can use the data in the events to
set up alerting around your smart contracts in that manner the token transfer white listed callers so addresses that are allowed to call the contract that are not allowed to call the contract eph balance transit a transaction value uh uh of course these refer to changing balance of the contract or the value that's been transferred uh the states change which allows you to observe when the state variable changes its value the next one is the view function which alerts you whenever a view function changes value so a view function in solidity it
gives you uh it returns values that are calculated using the other fields the other storage slot slots within the contract so it's a non-mutating function but when some of those variables change then the return of the viewing function might change and this may be a good indicator to you oh something interesting happened so you get a notification about it and there is more coming soon this will just give you some space to write some of your own ideas what is useful for you uh that you would like to monitor so this is all about the alerts and i will come back to oops welcome
back to this one and when when you look at this um hopefully you can see it well um we don't get too much information we do get this link and when i click the link yeah it's opening up the tenderly dashboard so you get to see that transaction as they've executed let me just expand this so the thing that we saw the last time it gives you the debugger and you can see the state changes and all that kind of stuff all right so can we do better uh we can do better with wet reactions and you can think of
webs reactions like alerts with some code that can be overrun so alerts just just send you messages with links but web reactions they run some code for you so there's a way to configure tenderly with some code and specifying some conditions when you want your web reaction run and there's a way to do it using using the ui so when you click add action again you can pick several different trigger types so events happening on chain or off chain that will cause your code to run now we have a block trigger so every
time a block is mined your web reaction code gets invoked and just like 20 minutes ago we we published a new post on the blog about uh counting blocks until the emerge so if this sounds uh fun or if you're looking for some inspiration check that out uh there's the periodic which executes well every five ten minutes whatever the web hook trigger that allows you to create an action that exposes a web hook you can use and for example you can use it and make a curl request or put it in an external system or ci cd something like that so it allows you to
use http to invoke connection on tenderly and most commonly it's used for continuous integration so uh the continuous building of your project prime tests and stuff like that and another trigger type here is alert so this helps you let's just see how it goes let's get this one so it's the code that we have to write this is where we where we go smart and the trigger well alert actually yeah i'm not sure yeah the one that we've seen the one that we created should have
appeared here um not sure why not i'm not gonna go there today but one thing that's disabled is transaction so if you want to monitor the transactions that are happening um for example just monitor monitor every transaction that goes into your smart contract or monitor events that were triggered that were emitted from your smart contract then you can't really use the dashboard so the the ui and to do that we have to go to code so here i am in visual studio code and here
this is the multisig wallet and this whole thing let me just zoom out for a second uh this whole thing is a hard hat project so in the package.json over here you have uh not this one uh and this one over here you have all the hard hat dependencies and it's it's pretty much there um so the the question that comes up okay how do i do that from code well there are a couple of things the first one is that we have uh tenderly cli that you can download and install on your machine so you can find it on github there's there's an instruction manual here
and when you when you install it you have to log in in a way so when you run internally who am i it should give you uh your email and your username okay fine unlock that so how do we get going with but three actions to get going i'll just create a time folder here [Music] yeah and change to it so i don't make too much too much stuff over here and we can go tenderly actions in it when you do this uh you get to pick
um you get to pick a project and uh yeah i'll go with this one i called blank and then you get a prompt uh asking about a folder you're adding your actions to and stopping that and it generates a scaffolding so you have a folder and if you look closely it's an npm repository so this whole thing is uh contains the code of your web reaction the action function to be precise to call it in proper terms and if you go to example ts you you can see here that it's block hello world function so it indicates that it's an action function and to help with development we also
added these types of types within a tender reaction package so yeah this is something that comes from running the command tenderly in it and here you can define anything you want uh so uh you can just log to console or you can install axios and do http requests to the outside to the web tool or you could even if you're brave enough store your private key intendedly secrets and do requests that go back to change so you can even trigger transactions that go back to chain in response to
something happening on check um so this was the quick start with uh the code but there's one case missing how do we tell generally that this should be invoked whenever a new block is mined to do that you can see that we have this gentle yaml over here and this is actually the place where we have to be specific about it we we have this actions part then this is my username a project a couple of things that are pretty standard runtime sources about the sources this value to the right this is the name of the directory that
holds your action functions code coming back to the directory structure so here i am in temp this is where tenderly ammo is and this is the actions folder so this actions folder just next to the template yaml this is the place where we are searching for sources and then we go with specifications of the actions so this one we call the example and you can add some description so you can tell it apart from the rest the function now this is uh this is a reference to the javascript function it it consists of two parts the first part is the path
to the file where your action is defined within this folder defined by sources so this means that we should go to actions example.ts so it's this file over here and then sorry and then after the colon over here we specify the actual function's name so when we go back to the example we can see that yeah so this is sort of making a link and you have to do it yourself manually and this very nice thing we call the angle the last part the last missing part is
specifying what triggers the execution of this action function and in our case if we want to monitor whenever a new block is mined we need to specify that the trigger is of type block so this means uh that every every single time the block is mined this function will get called so there you have it we just uh explained how to set up a basic very basic width reaction and i'm leaving the temp directory and i'm deleting it hopefully not deleting anything else yeah i'll just remove the force flag to be sure
to delete something that i really care about uh all right so let's see how we can actually how how do we actually uh do all of this but for a contract that's deployed on shape so the goal was and let me just go back to the multi-c wallet for a moment the goal is whenever somebody submits a transaction into the wallet or whenever somebody confirmed the transaction or executes the transaction we want to run an action doing something smart for example it sends some message to discord it can be transaction a new transaction arrived
was submitted was executed or it can be something even more uh more interesting for now let's just see how this went and when i configure this tenderly action stingy let's first check out the tenderly ammo i decided to put all my sources into a different folder called web3 actions so this is where i changed the name of the folder and as you can see i have a web reactions folder over here now if we go there we can see that we have the multi-cts and we have some helping libraries over here as well
i'll skip the helper libraries and the advanced steps we did here for now and come back to them in a moment um so let's start by let's start with the code and this looks pretty cumbersome if you decide to use web reactions in your project you can of course use this as a starter so if you do want to use it you would have to of course change the username to your own you would have to um you would have to set the project slug to to be the project slug of your own and just a little break what is the project slug so the project slug is um you can find it
if you go to settings and here this will be your username and this would be your project slot so if you have some weird things with caps capital cases question marks and so on that's not your project slug this is so we're going for that okay back to code so what we did here is that we deployed a we deploy these two contracts let me just take a look at this one yeah so these are deployed and we wrote one function that will react every single time an event happens
on chain and this event as i said can be either transaction submitted executed approved so how do we get that how do we know which one it is uh the second argument of the action function is called event and it contains information that's appropriate to your trigger so in our case since it's about a transaction we can cast it into transaction event coming from the terminal reactions package and then here we can extract some information about we can extract some information about the transaction itself fields well known so it's block hash number sender input
logs uh network nons and so on and so on and what we do here we're getting this in raw form so nothing is decoded and if you want to use it in a humanly uh friendly form that you would have to decode all that stuff in order to do that we use ether's interface so this class over here allows you to actually create an object that can be used to transform these raw form data that come within the transaction event into something more humanly readable or in other words it's
transforming it into something that's a json object that you can use and do stuff with it now the question may be where do i get this what to give to the interface well we give it the api of the contract that you're decoding data that that immediate events for example uh that you're trying to decode of course we have to import it somewhere and we can do that like this so when i go back to code you can see an api directory so here you would have to put the compiler output from your hardhead project or wherever you compile your smart contract and uh just place
this json in here and it's really important that you don't try to to do something like import a from uh dash dash artifacts uh contracts and so on and so on so if you do that your web reaction will you will struggle deploying it because this folder doesn't belong to the web reaction project so this thing over here it should be self-contained not referencing anything outside of it it's a common error so i'm just laying it out here so when we create the interface we can nicely take the event logs and we can
transform it into something humanly readable by calling interfaces parsed log and it had this thing over here has enough data so ethers can parse this for us so in case of submitting a transaction we'll take the first and the only pretty much event log and this is the tdx event let's get this apart for a second and we go back here so here we have to figure out which of these events had happened so if it is a transaction submission we want to do one thing if it's confirmation we want to do another executing transaction we want to do a whole different thing
so let's go over the simplest one and that's the execute transaction so i'll explain here just how it is to process a transaction coming in and just sending the message to discord so here let's take a look at what the transaction event has within the fields and if you do this uh yeah the the the code editor will not really help you this comes from github's pilot co-pilot so we would need to know what were the names of the arguments
and to come back here i'll just this one to the right maybe it's useful so here's execute transaction the first argument is owner the second argument is tx index so this is the one that we that we're looking for and we're going to log out transaction executed and then we're going to uh we're going to well skipping this one because it depends on processing it when it first arrived and the last thing that we wanted to do is uh send a message to discord uh the message to this court saying the transaction was confirmed by and then this particular address
so far so good so the important things we received an event from the network we check we uh cast that to a transaction event so we can actually access this in a friendly way using typescript we mapped it into a json object practically the parse logs into the first entry and then we uh by using the switch concluded yeah we're having the execute transaction case and in here we just wanted to log that it's executed and send also a discord message informing discord that this had happened uh okay
another big piece of information here is how do we do that discord so we need couple of things the first is well just to write down uh that that write down the message so this is the body that we want to send and this is sometimes an object sometimes it's a string so we're passing it through jason's stringify we're also we also need to have the discord's webhook url similarly like we did with actions now here comes another important part and that's secrets so secrets are one of the things and what three reactions let me just go back here
for a second secrets allow you to store some information that you may need in the execution but it's sensitive so from here i can just update it change it to something different or remove it the only place i can read it from is here so even if i wrote even if i took this and wrote console log discord url it would it wouldn't fail but it would output something like this so we're uh really really keeping the secret secret and the last bit we're using axios to post the information to discord that this thing had happened
all right again so far so good um let me go back over here so what reaction is nothing is in there and i am going to just take a brief look at this and this should be fine all right so what we want to do when we finish the writing of the action is to go standardly actions i apologize actions deploy it's pretty simple and
when i issue this command it'll build the web reactions project and it will deploy all the actions too tenderly now you may remember that we had to do something in a tenderly yaml file and while we wait for these actions to actually get deployed and here they are spending getting activated fine um let's take a look at some of the animal so in this case we're also we also have all of these settings that we've seen and we have our first action multi-sync and it executes whenever an event takes place so here we're invoking a function called on action event coming from
multisig.ts file and this is precisely the file that we've just uh seen second thing that's really important so yes we have the code that gets executed and the second thing is what are the conditions it gets executed under so we have to specify what triggers it and this particular function gets triggered by a transaction and it can if you stop here that means any single transaction will run in it but we want to be very specific about this and set it up in such a way that it runs only when an event gets emitted from the contracts so this is why we use filters for and you can do all sorts of crazy
things here um that are somewhat in the docs so we have event emitted this is the one that we want and we want to catch it when the contract on this this address zero xce yaya gets uh emits actually the event and which event then the one with the name tx submission if you remember we have this very event in our solidity file and the last bit of information which network and that's it so we have to say when a contract on this address deployed to the network three robsten
emits an event transaction submission then we want to call the function maltese on an event so this is just me reading all the the same thing uh our way around and this also shows an example how you can use the same action for mal for multiple um multiple event types so this one is event emitted from this conflict but this time it's transaction confirmation so when somebody says fine we like the transaction this one is event emitted from this contract when uh the confirmation is revoked changed my mind and the last one is when when somebody actually succeeds
executing this transaction and of course we have the network and an interesting thing is that you can also configure your uh periodic ones so you can have every single day or hear it it dropped down to five minutes and it is periodic or every on a web hook so when somebody calls your web reaction and wants it specifically to get executed so you can play around all of these that we saw seen in the dashboard you can do all of this from the code as well so this was the last bit and here we go we have these deployed and since we have this periodic one
you can go in here and you can see the execution history so it executed two minutes ago while i talked and if you go here it gives you a list well not a list because there were no transactions in it a list of daily simulations um we also have we deployed other actions as well this is this is the one that we analyzed the bit that covers um the the execution of the transaction and this one over here is invoking from the web hook so you could wait for for a transaction to actually execute
but in order to test all of this you could also go for a manual trigger what you need to trigger a transaction action is actually uh the transaction cache so we can go back to uh to our transactions screen this may be useful we can go back to the transaction screen and let me make this slightly bigger and i'll take this one execute transaction the one that we analyzed and i'm gonna copy and i'll just go manual trigger so here you would just need to specify the network for this transaction happened on and i know it drops them because
i did this so many times it's here and here's the transaction and if you hit pay catch fetch payload it will show you all this data over here and if i go trigger i'm not sure if it will succeed with an empty storage let's see all right it failed so this means that running out of order running what reactions out of order can fail so we have to actually run it properly to do this let me go one by one and i'll take this submit transaction so i'm taking this one i'm
gonna trigger this one manually and paste this payload first so we can see that it's an actual thing that somebody tried to put in our in our multisig wallet and um i'll skip this explanation for now hit trigger and hopefully everything goes fine uh no not really yeah but you get to just uh there is a slight issue over here that i don't really see um another interesting piece of information that i want to give before wrapping this up is the ability to run these locally
so you can actually uh you can actually set it all up and you can go uh you can actually run all of these as you develop them in a local runtime so you can use tenderly access tests so here you can put the discord url access keys whatever is required by your web reactions and just call the test runtime and execute the action of course submitting the uh submitting the data that it requires to get executed i'll just run this one
and i think we have to wrap it up here so here we have some daily simulations and i guess that we have an issue with a webhook all right but the time is up pretty much and unfortunately i'm not gonna be able to answer the questions i think so alex do you think we have more time here to um um no unfortunately we don't have any more time um now because we've got a coinbase workshop starting up now but thank you so much um for any of your
questions please post them in the discord is in the discord and i'm sure he'd be happy to help you there um sorry that we had to wrap it up so quickly um but we really appreciate your time nina this was an awesome workshop and to those of you who attended thank you for coming i recognize a lot of faces from some of the um boot camps as well so it's awesome to see you all again um for those of you that are here we've got a coin base wallet workshop happening now i've just dropped the zoom link in the chat so please feel free to join and if you do join i will see you there and if you don't join have a good day awesome [Music]
you
