New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Creating a Project

BaseMon, Oct 9, 2023, 12:00 AM

Set up a Solidity project using Visual Studio Code and Hardhat. Master contract compilation, testing, and integration with TypeScript, ensuring smooth development. Level up your smart contract skills and build reliable, bug-free projects with ease! 🛠️💻 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

as you can see here we're starting out with Visual Studio code and an empty folder opened up first thing we need to do is we need to make sure that we have an up-to-date version of node which we can check with node-v and we can see here that we have version 18.12.1 which is sufficient but these things do change pretty quickly so if you run into an error check out the docs to make sure that that hasn't progressed the next thing we're going to do is we're going to install hard hat itself with yarn add Dev hard hat there are also npm instructions on the docsite at hardhot.org so be sure to check that out if you prefer npm all right the install process is completed so the next thing we're going to do is we're going to run npx hard hat and this will open up a little app that will create our project for us we're going to pick typescript for some reasons we'll describe later so I'll go ahead and select that now this root is fine we do want to get ignore and we want all of these dependencies because we'll need them when we're writing tests later on so we'll let this run okay we can see that the project has finished building and we've got a list of files and folders in the Explorer let's take a look at those first starting at the top we have contracts and there is a demo contract called lock.soul in here we have our node modules folder scripts and this has a deployment script that we can use to deploy this contract to the blockchain test folder which contains some tests that look should look pretty familiar to you that is if you're a web developer the hard hat config file is where we can set up various things related to hard hat we're going to go ahead and change that to 8.

17 for now to stay consistent with the rest of these exercises we normally recommend that you stick to the most current version of solidity but that can introduce bugs that are not your fault and are difficult to figure out what's going on so it's really up to you particularly while you're learning the readme has a couple of useful commands and then we have some other various config files we can also run npx hard hat again within our project and this will give us a list of all of the commands and options that are available for now let's try compiling our project so we can go MPX hard hat compile because remember solidity is a compiled language that lets us write human readable code that gets turned into the byte code that is read by the blockchain looking up top we now have an artifacts folder and if we dig into that a little bit we can see the ABI for our smart contract returning to the contract itself we can see that there's a pretty straightforward smart contract that allows a user to lock some funds and make them unavailable until after the time limit has elapsed one thing I'll point out here that's nice is we do have the hard hat console logging Library added in so we can uncomment that uncomment this console log here uh and this log will show up in the console for us when we test I'll split the lock contract so that we can look at both the contract and the tests at the same time which we can see in lock.ts so again these should look pretty familiar to most developers we have uh hard hat chai matchers installed and it lets us use and write mocha and chai tests the way that we're accustomed to the great thing about using typescript is that hard hat now has something called type chain built in which makes intellisense work between our smart contracts and our tests so if I go here lock is the instance of the contract in this test and I can do lock.unlock time or lock dot withdraw and we can access our functions from the smart contract right here to run the tests we can use npx hard hat test uh if you haven't compiled recently this will recompile and then run your tests and we can see all of our unit tests are working as expected and while the tests were running we got those console.logs to work as well

Automatic transcript — names and jargon may be misspelled.