Hey, folks have you ever wondered how network filters work in SubQuery?
Well stick around and I'll show you how
the first thing I want to do is let's jump into vs code and we'll create the starter project
we run subql init let's call this network filter example
accept all the defaults and there we have it initialized on the left-hand side
let's jump into the project and I'll just run yarn install first while that is running let me open up
the manifest file here and you can see we've got our data source and this is pointing to the polka
dot mainnet endpoint now what if you want to point to say Kusama network or another endpoint well you
can do this quite easily by just replacing this line if we go into the documentation for example
in the faq section we can see what is the endpoint for Kusama so I can copy this out
and then I can paste it in here
but this is a bit troublesome it takes a bit of time and things could be improved so
that is why inside sub query projects
if i open up any project here so this one for example and go into settings deploy a new version
this is why you see we've got this option to override the network
endpoint if you leave it blank it takes the default from the project yemo file so
this file we saw here this line otherwise if i paste something like this the kusama testnet
then this will be used instead and this is pretty neat because it allows me to deploy different
versions pointing to different endpoints for different purposes testing for instance however
let's take this to the next level because if we look at the code what if i want to provide
slightly different parameters such as a different starting block for the two different endpoints
well it turns out i can do this as well let me go into the documentation here
and on the left hand side under create a project
click on the manifest file and then on the right hand side here let's jump to network filters
so it's here where you can as a user deploy your project to either test net or mainnet environments
however between the networks various options can be different as mentioned the start block
so what we can do is define different details for these data sources in the manifest file
that we can use across different networks and here is the example here where we go ahead and provide
one data source called polka dot another one called kusama and then effectively we
have a definition file up above pointing our mappings to the definition but inside our data
source we can provide different parameters so let's jump in and take a look at this
what i'll do is let's go and copy this data source here and then i'll paste it down here
below and then i'll call this say polka dot endpoint and i'll call this one here
summer
now what I'll do is let's go yarn code-gen generating the code and this confirms that
it still works but what you'll notice is that it won't actually do anything useful
you'll also notice that we've got some redundancies here so the handler portion is
defined twice and as always in programming, you don't want to define the same thing
multiple times so let's go ahead and cut this get rid of this and I'm going to call this definition
and then paste it down here I've got a tab in to keep the formatting and I'll just confirm if this
is singular or plural it's plural so let me put it s and there's also another level here called
mappings and this is where I provide the name let me go ampersand which is the key symbol that you
need to define the name of the definition and I'll call this global definition like so and
I'll tab that in saving this file and this means I can delete the handler up above
now what we do need to do is if I go back to the documentation you'll notice I need
to refer or point my data source mapping to the definition mapping so I basically just go asterisk
and then say global definition same thing for this one below global definition
save this and let me run yarn code-gen now you see I get an error and if I look at the the reason
it says unidentified alias global definition now this is because I'm actually using my definition
before I've defined it so let me go ahead and cut this and reorder this and place it up above
let's save this let me run yarn code gin again and you can see there's another error
where the end of stream or document separator is expected so the formatting is not quite right and
what you'll notice is that the name needs to be tabbed in so it needs to be in line
these ones should be okay but i can bring it back one tab just to keep it nice and consistent
now let's try running this again and success so what i've effectively done is gone ahead and
created my definition and then i've created my data source where i can customize it
and my mappings three points back to my global definition so i can now say let's start the polka
dot endpoint at a block height of a thousand and maybe kusama at fifteen thousand say
you'll also notice in the documentation that you can add a filter on the data source to
decide which data source to run on each network so for example this one here we use filter spec
name equals polka dot and same down below with kusama indeed we'll do this let's go spec name
kusama and the same for the one above and i'll just copy this in
like so so again just double checking yarn code gen no errors and then i can go yarn build as well
great there we go success now just a brief recap what we've done is we've expanded
our understanding of the manifest file by having different data sources having a global definition
of mapping so we can go ahead and change the end point as we saw in subquery project so when this
is overridden by say the kusama endpoint it will then come down here look at the kusama data source
read in the parameters from here and then refer back to the global definition that we
write only once as always thanks for watching catch you on the next video
you