hey folks welcome to another video where we're going to take a closer look at event handlers
as always let's jump into the documentation here
on the left hand side let's click on create project jumping into the mapping section
now in the past we did cover block handlers so let's now turn our attention to event handlers
you can use event handlers to capture information when certain events are included in a new block
the events are part of the substrate default runtime and remember that a block can contain
multiple events and we'll see this shortly now note that any type of event will trigger the
mapping allowing the data source to be captured because of this we should use a mapping filter
in our manifest file and this was covered earlier in a previous video to reduce the time it takes to
index data and improve performance so what does this all mean first of all let's jump into the
likes of a subscan and let's go and examine a particular block subscan is a regular blockchain
explorer and if i scroll down let's check out this block here you can actually see there are
five events and three extrinsits or loosely known as transactions so if i click on this
and jump into events these are the events that are associated with this block so you can see
we've got different types of events balances dust loss balances transfer balances deposit etc so we
actually don't want to grab all of this and this is what our mapping filter does for us
the mapping filter allows us to say only grab balance deposits and not only can we grab it from
just this particular block but across all blocks that is how we can go ahead and reduce the
time and the data it takes thereby improving performance you can also jump into the polkadot
documentation another very useful website here we'll jump into substrate metadata
click on events and these are all the events that you can go ahead and examine
so just like before if we jump into balances you'll see that we had dust lost for instance
we can find balances transferred also balances deposit as well so now that we've
got some background on this let's as always jump into the code and see how this works
let's now jump into vs code and we'll create the startup project as always sub ql init
and then we'll just run the standard yarn install and while that is running
let's open up the source code and you can see we've got the standard three function
handlers we've got the handle block event and the call we want to turn our
attention to just the handle event so let's go ahead and remove the likes of handle call
and I'm actually going to comment out handle block
then in the schema file let's go ahead and delete the handling call
fields and also handle block as well and note that we've got the filter for balance dot deposit
as mentioned earlier now if we were to go ahead and try and run this it wouldn't work
because there are a few things we do need to change first of all in the block we're actually
defining a new instance of our starter entity so I need to copy this and paste it down here
because down here is where we are actually using it before I delete this though I want to go ahead
and grab the argument here and place it into the starter entity argument this is because this new
object here needs the id as a constructor and you'll see this if I open up once I do code-gen
so now I can go ahead and get rid of this like so and now if I go and run yarn code-gen you'll
see the starter entity typescript appear and again this is the constructor that I mentioned earlier
let's go and build this
and get this running in our docker container
okay so you can see that the node now has started but there's an error saying that
null value and column field one violates not-null constraint now the reason for this is if we look
at our schema file you'll see that field one is of type integer with an exclamation mark
meaning that it's mandatory our field one was assigned up here in our handle block
and we're not using that anymore so let's go ahead and stop the node we'll go back to our schema and
we'll delete the mandatory requirement now because we've updated the schema let's rerun code gen
and then we'll rebuild it just for good luck even
though we don't really need to because we didn't change the code
and then we'll run docker again
okay so now what's happened is that the same error has occurred because field one violates
the constraint and the reason for this is that let me stop the node first of all that the first
time we ran it we actually created the database and you'll see here the Postgres database on the
left-hand side if I do make a change to the schema and I already have created my database
then I actually need to delete the database what this means is that when I run docker-compose
it will recreate all the tables in the database again thereby removing the
constraint of the non-null that was applied to a particular column in the database table
okay so now the node has started as you can see
and we're starting to fetch blocks so that's a really good sign and note that we're not
starting from zero as you may have seen in the past and that's because we're using a dictionary
so it's jumped straight to the various block height of concern that's a really good sign
so now that we've got this up and running the next question you might be asking is what information
can we extract from the event object so what we can do is let's go to the definition here
and you can see that the event interface has IDX and then it also relates or links to extrinsic
and a block and the reason why this is neat is that remember how we go from event extrinsic and
then we grab out the block header we could also grab out the block number as well which I'll do
this is because we can jump so to speak from our event interface into our substrate block
interface so the first thing that I want to do and this is always very useful is let me
stop the node and I'm going to go let's use the logger and I'll go info
we'll grab up the block number so what I'll do is go event dot block dot header dot number
dot to numbers like so and that's just following from above here where again this is accessing the
block number from the block or substrate block interface but here I'm actually going through
event back into the block so effectively just copying this and putting the event field in front
so let's save this and give this a whirl I'll first of all build this
and there we go the node is up and we're actually displaying the block number so that's great to see
let's now dive into the substrate event interface this extends event records so if I go there you'll
see we've got an event field let's jump into the event which extends generic event and then it's
here where you can see we can grab data index metadata and there's also a brief explanation
on what these mean so, for example, the meta this grabs the event metadata with the documentation
method grabs the method from the event the section grabs the section the description and type
definition so as an example I can grab let's try the method in the section so let's copy this out
and it's here where I want to go
event dot event dot method
and this one I'll make this section
let's rebuild this and start the node again
there we go now if i expand this up a bit you should be able to see i've got a block number
and then the method and the section equates to deposit and imbalances and that doesn't change
and that's actually a good thing because remember that we're actually asking this
project to filter all events that are balances and deposits
so if there was anything different here this would mean that the filter is not working and in
fact if i turn off the filter you'll see that the method and the section will display all the events
related to a block so that's a project for you to try out and to prove for yourself
but again just going back to the code here to understand what you can extract from the event
you can go ahead and explore the various interfaces and again in data in an index i'll
leave that for you to explore to understand what other information you can extract
just before we finish up here it's always good to jump into the sub query explorer
again to see how other people are using this and if i go into the likes of staking some here
and jump into the github repository of examples this is the sum reward project
if i go into the source file look at the mappings and look at the rewards you can see here we've got
a handle bond event we've got a handle rewarded event handle reward
handle slashed etc so these are all events that the handler is looking for
so this particular project is really neat to help you understand events
because it's monitoring for the events and then in the code down below for example this one here
it's summing all the rewards for a particular account for instance so there we have it how to
understand the event handler and the information that you can extract in order to make use of
in your front end application hope you found this useful and i'll catch you on the next video
you