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

Loading player…

Testing with Typescript and Typechain

BaseMon, Oct 9, 2023, 12:00 AM

Add new functionalities to your smart contract like a pro! In this video, we'll explore how to implement a deposit function with timestamp validation and ownership checks. Unlock the potential of your smart contracts and write robust tests using Chai matchers and ethers.js. Watch now to ensure your contract's reliability and security! 💼🔒 📚 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

so we're looking at our lock contract and let's say we wanted to add a little bit of functionality to it maybe we'll treat it like a piggy bank where you can only withdraw after a certain date but you can add more money to it whenever you want so let's add a function called deposit and this won't need any arguments because we'll make it public and use the payable keyword since our contract design has the contract itself hold the balance that's in deposit we do want to make it so that the user can't accidentally deposit funds after the withdrawal date so we'll add a require and say that block Dot timestamp must be less than or equal to our unlock time or you get a message you can't deposit after unlock and then maybe we don't want our users to accidentally deposit funds into the wrong piggy bank so we'll require that the message dot sender is the owner and we'll give the message you aren't finally we can see that an emit event is emitted for withdrawal so let's add one for deposit we'll say event deposit and that's going to have a uint for the amount and a uint for when the same as withdrawal finally we need to go back down to our new function and we can emit that event with emit deposit message dot value and block Dot timestamp so we've written a new function we should write tests for it as well so we'll go over to our lock.ts test file and after being able to receive and store the funds to lock we can add in a new test that should allow additional deposits and we'll want to call are our new function but you'll notice if you try to write lock dot deposit our new function is missing it's not present in the intellisense which is unfortunate and the reason for that is we haven't recompiled the contract so it's related to the compiled version and the compilation process that type chain does its magic so let's go back down to our console and run npx hard hat compile and we can see that we can now say lock Dot deposit finally one thing we want to take a look at while we're here is up at the top of our test file where it creates the fixture that does the setup for every single test and essentially recreates the contract and redeploys it on the blockchain so that it's fresh and unmanipulated and you can get predictable test results although bear in mind that that could also lead to unpredictable test results if you're expecting things to still be there each test um and if we look here we can see that we have an owner and another account um which are using a library called ethers in a function called get signers and so these are essentially wallets or accounts that are uh used in testing and we can use them to do things like make sure that the other account is not able to withdraw the funds that belong to the owner

Automatic transcript — names and jargon may be misspelled.