Setting Up the Deploy Script
Base·Mon, Oct 9, 2023, 12:00 AM
Set up your hardhat deploy script and integrate it with your test file, making contract deployment to different networks a breeze. Ensure consistency and efficiency in your smart contract development process, saving time and avoiding errors. 👍💼 📚 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
now that we've got hard hat deploy installed we need to do a little bit of setup the first thing we're going to do is we're going to check back in the docs and we're going to see here that they suggest adding these files to our typescript config to make sure that we can make use of typing and auto suggest in those files so we'll copy that go back to our editor and add it to our config file next we're going to set up a folder called deploy and in that folder we're going to create a file and name it zero zero one deploy lock and the reason for this is that the way that hard hat deploy works is it looks for the deploy folder and it runs any scripts that you have in that folder in alphabetical order since you may not necessarily want them to go in alphabetical order adding a number up front will let you control that order and therefore control what order your contracts are deployed next we can return to the docs and grab a little bit of this boilerplate here that will get us started with our deploy script so we'll paste that into our file but before we get started we're going to use one of the features and jump over to hard hat config and what we can do here is we can add a new item in our config a new property called named accounts and in that will create a deployer as a name and use account number zero similar to remix hard hat gives you a list of wallets with imaginary currency in them that you can use for various tasks and use in testing and in deployment and in this case we're assigning wallet number zero to the name deployer so we'll save that and jump back over to our deploy script the first thing we're going to do is we're going to import ethers from hard hat now you may have heard of ethers before it's a popular library that allows you to write JavaScript or typescript and interact with the blockchain in this case we're using the version that comes with hard hat because it has some additional functions that work with hard hats tool chain next we're going to add a constant to deploy and we grab that from hre the hard hat runtime environment Dot deployments then we'll import the deployer that named wallet that we just created so we'll add deployer and for this this is an asynchronous function so we need to await hre dot getnamed accounts and you'll need to use a weight with almost anything that's interacting with the blockchain because as I'm sure you've noticed it takes a fair amount of time for those transactions or calls to process next we'll set up some of our constants for our deployment first we'll create a value locked and for that we can use a utility that can parse ether for us so we don't have to worry about the decimals and make sure that we get it right because if we get it wrong we're going to use the wrong amount here so we'll say hre dot ethers Dot parseether and then that lets us put in 0.01 as a string and it'll convert it for us and make sure we get the decimals right we'll also add one for unlock time this would be how long we want to lock up The Ether in this contract and for now we'll just pick a round number and we'll say ten thousand next we need to use ethers to get the timestamp information from the blockchain so first we need to get the last block and we can do that with constant block number is again we await ethers dot provider dot get block number and with that block number we can get the last time stamp with con creating a constant called last block timestamp and for that we need to await the third stop provider and get the block for our block number and then we'll get the timestamp out of that as a number finally we want to actually deploy our contract again this will be asynchronous so we're going to await deploy of our lock function and the way that this works with hard hat deploy is pretty neat it can find any function or any contract that you have based off of the name of that contract that is in the contracts folder so in this case we have lock which note that it is not necessarily matching the file name right here but it's matching the name of the contract so if we also had a contract called unlock we would deploy that by putting unlock right here next we need to add some parameters for our deploy function the from field is going to be our deployer account we can also add arguments here for the Constructor function for our smart contract in this case we want to say our last block timestamp plus our unlock time because the contract calls for when it is allowed to release the funds and finally the value or how much of the main token for whichever Network you're deploying this on we're going to add and in this case we're going to use value locked and it's expected as a string so we will cast that to string and just fix a little typo right here that is args for arguments there can be more than one and it is named thusly and that's it our deploy function is ready to go
Automatic transcript — names and jargon may be misspelled.