Scaffold ETH 2 - Extensions Development Guide
BuidlGuidl Talks · 12 Aug 2024 · 20:57
A hands-on walkthrough of the third-party extension system in Scaffold-ETH 2 and how to build your own extension. The presenter starts with a one-page extension kept in a GitHub repository and installs it with the create-eth command, then moves to a fuller workflow that adds a charts page, dependencies and CSS. Along the way the talk explains the folder structure, template files, the create extension command, when to choose an extension over forking, and how to test an extension locally with a symlinked development setup.
Topics
From BuidlGuidl Talks, hosted by BuidlGuidl.
Transcript
in this video we're going to take a look at scaffold eth 2 and its thirdparty extension system and how you can make your own extensions for scaffold e 2 if you go to the create eth repository it's going to have some documentation here about what extensions are and a quick overview on how you can develop your own extensions so what exactly is an extension well we can look at a example extension right here called EIP 712 and it's basically just a series of files we look into the structure right here you can see that it's under extensions packages nextjs and these files under nextjs folder are going to get merged into the base scaffold eth project when you run the MPX create eth
command to create an extension you should have some basic knowledge of how scaffold e 2 works if you don't and you're a beginner here's a link to a video about scaffold e 2 I suggest you watch that if you're more comfortable with scaffold 2 you can start making extensions really easily so let's quickly make make our own extension based on something like this make a directory and call it my first extension and then you go into the directory and you'll want to mimic this folder structure so extension packages and then n xjs so we'll open up that new folder and make folder called extensions and then within that called packages and then within that nextjs now this is going to be a really
simple extension it's just going to be a new page so with in the nextjs folder we'll make a app folder and then within that we'll call it first extension this is going to be the name of our new page and then we're going to have a page. TSX all right so we have the folder structure correct it's important to get all these folders in the right order so that when you you go to use the extension the scaffold e command line tool will know how to properly merge the files this extension is just going to be a very simple new page a new nextjs page so we're going to paste in the contents of the page and then that's basically all you need to make an extension so what you can do now is you have this folder structure you want to push this
folder structure and all its contents up into GitHub so I have it up here right now just make a repository you can call it whatever you like in this case it's called first extension and you can see the folder structure is reflected here and you can actually go ahead and use this extension so when using scaffold e there's two ways you can go you can get clone the repository or you can use the MPX create eth command this is what you're going to need to do if you want to use your extensions so we're going to do MPX create eth at latest a dash e and then you can put in your repository that you just made for the extension so mine was the first extension and when you hit enter you're going to have to install
the latest scaffold eth for project name you can name it whatever you want I'll just do my extension example and and I'm just going to pick hard hat so what it's going to do is it's going to install everything that comes with the base scaffold eth repository at the same time it's going to grab all the files that are in this repository for the extensions and it's going to merge them together that's why this folder structure is so important all right so now it's done installing we can go into the my extension example now we can run the standard scaffold Le commands so yarn chain we can run the yarn start is going to start up our scaffold e 2 all right so this is our installation of
scaffold e 2 with our extension and if you go to the new page called first extension you'll see that our pretty ugly page is there my first extension this is a test page for my first extension so that worked the extension has now been installed now part of your scaffold e 2 installation if we take a look at the folders going to packages nextjs app is where we made that folder and this is the page that we made this is the page that got merged into here from the extension system so essentially that's all you need to make an extension for scaffold e 2 but if we go back to our extension you can see we have some squiggly right here that's because it cannot find anything to do with the
react my our extension is literally just this page so there's no dependencies installed there's no prettier there's no es length it's kind of a fake environment and if it's a simple extension just like a onepage that may be fine but if you're doing a more complicated extension that has more moving Parts you want to follow a different workflow than just making a folder so let's take a look at a more complicated example and how we can do that so what we'll want to do is close out of everything here instead of just starting with the extension you want to clone down the create eth repository so this repo right here is what powers that CI command the MPX st e and we're going to clone it down with the get clone this repo is going to have a lot of helpful
scripts to help you create your extension we want to go into there and do a yarn install all right and if you come back to these docks right here you can follow this workflow we clone down the repo then we can do a yarn build p and da so that's what we're going to do now it's going to make some files into this disc folder and we can use those files so now what we're going to do is we're going to create what's called an instance of scaffold 2 right on our local machine and to create a new instance you can use the yarn CLI command this is basically going to be mimicking this Command right here the MPX create e but instead of you using MPX we're going to use the create eth repo that we just cloned down yarn CLI
and we're going to make a new folder so we're going to go up a directory and call it a charts extension okay so we're going to make an extension that adds some charts and you can pick whichever framework you want I'm just going to use hard hat it doesn't really matter so the extension we're going to make it's going to add some of these charts to our scaffold e 2 it's going to use the Shad CN charts right here and it's going to have to install some dependencies and some CSS and a new page to show off some example charts and all that is possible with the extension system once our create eth yarn CLI command is done it's done installing all that into a new folder so we'll go up that directory and it's going to make the folder called
charts extension right here and we can CD into that charts extension so what you get in this charts extension is basically the base installation of SCA these two there's nothing new no new extension and anything but everything will work so if you do the yarn chain it's going to work and then the yarn deploy finally the yarn start now this is going to bring up our front end on Local Host all right so what we have is our base installation of scaffold e 2 on our machine on Local Host and what we can do is now we can open this up in our code editor and we can start adding those new pages so for nextjs we're going to go into the app directory and
we're going to make a new page called charts and we're going to make a page. TSX and this is going to be where our new charting page is going to live and as you can see with this method since we're basically in a instance of SCA 2 we're not going to get those squigglies because react is installed all the dependencies are installed so it's like you're working on a normal application so unless you're very familiar with extensions and xjs pages and you know exactly how your extension is going to work this method is going to be a lot more user friendly because you can develop the pages and all the other files in a normal way so we have our charts page we're going to need to add some other dependencies to our repository you can just yarn add these
packages right here like normal now I'm basically following along the installation guide for Shad CN charts right here so we've installed the dependencies now we can make a folder for the chart components and then we need a CSS file right here I'm just going to copy and paste a lot of these files into our repo so in our components folder I'm going to make a new folder called UI and in that I'm going to make a file called chart TSX and I'm going to paste all this code into here now we're going to need a utils file so we going to make that right now I'm going to paste this stuff into here now we're going to to add some CSS for these charts and normally you want to edit this global. CSS but with extensions
there are files that you cannot edit perfectly fine to add new files those will get merged no problem if you want to edit existing files they have to be what's called a template file and I'll get into more of that later but Global CSS is not one of those template files so when you're making your extension you cannot edit this you'll need to make a new CSS file called chart. CSS and I'm going to paste some some of these CSS variables into here and one last file that is needed is this components. Json file and this is basically a Shad CN configuration file so now that we have everything else installed I'm going to paste in the rest of this charts page that we actually have example charts on here all right so now our charts page
will have better example charts going to have an area chart and a bar chart and if we go to our charts page you can see here's the bar chart right here and this is the area chart so everything is working inside of our local scaffold e 2 instance another thing I want to do is add a link to this page up in the header to do that I'm going to go into the header component right here and I'm going to just add a new link right here and it's going to be called charts going to link to the charts page and we'll have a new pie chart icon for it all right so now we have the new charts page in the header and it links to our new charts page with example charts it's important to note that this header component file it's going to be a
special file because instead of creating new files this header file is an existing file and it's actually one of those template files that I mentioned earlier we actually need to add this new menu item in a special way but first let's see how we can translate all these changes into our extension so once you have all of your changes in a good spot you have added all of your new packages your new pages your new utility files everything after you've committed all of your changes you're going to go back to the create E3 repo that you Clon down before you're going to run a special command called yarn create extension yarn create extension and you want to point to the instance that you created earlier so in our case you go up the directory and you go to the Chart
extension now what's going to happen is it's going to create an extension for you it's going to go into the charts extension folder and scan any new files that you've added and say and copy them over for you and it's going to give you warnings if you've edited a existing file such as header is going to say you need to manually add some changes right here and I'll do that in a little bit and it's going to skip some of the files that we don't really need and it's going to tell you that package.json needs to be manually created as well but for all the new files it's going to just copy them over into a folder for you it's going to copy all of those new files into a folder called external extensions so if we go into external extensions
you'll see that it made a new folder called charts extensions for us so if we go into there and we can see that it copied all of those new files into the proper directory so we don't have to worry about that complicated directory structure it copied over the new page the components slui page right here is going to copy over only the necessary new files that we've added in our other instance so the only things we need to do manually is create a package.json in the nextjs folder and we're going to paste in only the dependencies that we've installed because these are going to be merged into the regular dependencies that scaffold E2 already has and the last thing we need to add is that header file now we can't just add
any header file it needs to be a special file so under the components we're going to need to add a header. TSX rmjs and what this file is going to have it's going to have the icon that we added for a header and the menu object that we had for our header the header file is one of those template files let's take a look at some of the other template files now right now we're in the external extensions folder that create eth has created for us so let's go back into the main create eth folder all right if we go back into the create e directory here is the external extensions folder and this is our extension right now go up to the main
directory there's a templates folder right here under base you can find some of these template files so get ignore. template. MJS anything with a template. MJS is a template file so if we go into packages next component header. tx. template. MJS this is what the header file actually looks like if we open it up you can see that it is not a regular react file it is made of all these back ticks right here and all the contents of the file is put within the back text now what happens is when you use the create eth CLI it's going going to print all this stuff into a header. TSX file if we go up to the very top you can see there
is a menu icons import and the menu objects right here so these props are getting put into this file and these are the ones that we are editing in our header. TSX args arguments. MJS so these are being piped into that template file and that is how the template file becomes created and merg with our extension if you go go into the create eth repo you can do a quick search for anything that is template. MJS and you can see these are all the template files that you can edit using that special method so another example is this scaffold eth with providers so say you want to add a new provider you would
make a scaold e with providers. TSX arguments so. ar. MJS in your extension and then you can add new providers to the existing providers file and scaffold e to so any existing file that is not a template file such as footer. TSX you cannot really edit that using the extension system now you might be thinking that's cool but I want to edit the footer file the whole thing or I want to edit the entire header file why can't I do that with extensions well one of the great things about extensions is that since new files are able to be merged into these Bas files the scaffold e team can maintain and update these BAS files if we take a look at the scaffold
e 2 repo you can see that there's a team of great maintainers that keep it up to date with bug fixes and new features all the time so you actually get these fixes and new features for free when you use extensions so you don't have to worry about keeping these base files up to date all you need to do is worry about s your extension and existing files the rest of the files if there's like a bug and header or something like that then the maintainers will fix that bug all you need to do is worry about your little EXT exension if you really want to edit the entire header file or one of the nontemplate files that are existing then you're probably better off making a fork of scaffold e 2 rather than making an extension with extensions the team behind scaffold e 2 keeps those packages
and Bug fixes up to date for you if you would like to learn more about the templating system go into the create e repo under contributors and there is a doc under templating MD and it explains all the template files and the reason ing behind them and how the template file API works now coming back to our extension we have our header file created so our extension is complete we have all the files that got copied over for us we got the package.json file that we created with only the dependencies that we need and we have our header. TSX rmjs file which will be merged into the main header file where we use our extension the last thing to do is simply create a repo out of this folder so if
we go into the external extensions go into the charts extensions again right now this folder is under create eth but it is a get ignored folder so git is not going to pick this up if you go back into this folder you can do a git and knit and you can simply create a git repo out of this folder and push it up into GitHub so here's the GitHub Rebo I've made of that extension you can see all the files are here including the package.json and if you open the components here's that header file now this extension is ready to be used when you go and use the MPX gr eth at latest command it's important to note that the create extension command using yarn create extension this step is completely optional since extensions are just a
series of files and folders there's another workflow you can go through to test your extensions locally on your machine if we go back to the third party extensions doc under the create e repo there is a local testing and Publishing workflow we can try out this workflow by copying this Command right here so what we're going to do is use yarn C- e and we can use a local extension in our create e folder to create a new instance of scaffold e 2 so I'm going to come back into our create eth folder making sure that we have a external extensions folder you can simply just add all your extension files into this folder and you can run this command which is yarn C- e then your extension name in our case
it's going to be charge extension and the same thing we did before we're going to go up directory and we're going to create a new instance in a new folder I'm just going to call this one testing locally charts extension and I'm going to have this d-h Dev flag under here as well so if I hit enter it's going to ask me the same question of do you want hard hat or Foundry I'm just going to pick hard hat now with this workflow it's going to take a local extension that you have under the external extensions folder and it's going to create the instance of scaffold e 2 so it's going to make a new instance under testing locally this new folder once it's all done installing we can go into the testing locally folder and we can do the
yarn chain the yarn deploy and finally the yarn start to bring up the front end and if we go to locco host 3000 you can see we have a new instance of scaffold D2 using our local extension and our charts are all here now the cool thing about this workflow is that the files in our testing locally folder these are going to be Sim linked back to our external extensions folder so any changes that you make to the extension files in this folder they're going to automatically be reflected back into this external extensions folder so for example let's say I am locally testing my extension and I want to change something on this page so I want to change something on the charts page I would go into the testing locally page
so if I go into here under the charts page and I make an edit right here to The Heading so I can say my charts if I save save this remember we're under the testing locally charged extension files I save this it's going to update it here under our Local Host because that's the one we're running since we included that D- Dev flag back in our external extensions file if we go down to where that heading is you can see it automatically got changed so let us put this side by side if I add some say exclamation marks right here it's going to automatically update this file as well so this is is a quick way to update some existing extensions that you have locally and you can go back and forth
and once you've made these changes right here you can simply go back to that external extensions folder and get commit those extension changes back to your GitHub repo now this workflow is also optional so you can use either method however you want to make your extensions and with that our extension is done anytime you want to use SCA Le 2 and add some charts you can use this extension I would highly suggest you go into the create e repo right here and read all these docs under the contributor folder so the third party extensions doc templating doc and even the developer guide if you have questions or comments about the extension system please raise an issue or you can reach out to one of the team members behind the scaff 2 project extensions are a pretty new thing so if
there are bugs or anything that you wish to see change please reach out to one of the team members we're really excited to see the types of extensions that gets made by the community
Related talks
- Cómo desarrollar aplicaciones en Ethereum 🚀 | Demo en vivo con SE2, BuidlGuidl y SpeedRunEthereum
- Desarrolla tu Primera App Web3 Full Stack | Curso Cyfrin Updraft en Español
- Crea Tu Primera Cripto Moneda ERC20 Desde Cero | Cyfrin Updraft
- Ethereum Dev Onboarding Session
- Building on Ethereum in the AI Era
- Solidity 101 in Turkish: Updraft Study Session, Part 1
- Ethereum Foundation: Web2 dev to Web3 dev with 🏗scaffold-eth with Austin Griffith
- Solidity 101 in Hindi: Updraft Study Session, Part 1
- Solidity 101 in Arabic: Updraft Study Session, Part 1
