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

Loading player…

Forking Mainnet

BaseMon, Oct 9, 2023, 12:00 AM

Forking in Hardhat: Run tests and transactions on a copy of the mainnet, allowing you to test real functions with real money before deploying. Gain confidence in your smart contracts, ensuring they work flawlessly with real-world scenarios. 🛠️💰 📚 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

one of the most powerful features with hard hat is the ability to Fork a main net and then run tests and transactions on a copy of it normally when you're running the hard hat Network locally it's just a simulated blockchain and it doesn't have the context or contracts or balances or addresses found on the main Network but if you're making a real application with real functions that move real money around you want to be able to test that it would be really really scary to just hope that it works right and that you can move a million dollars around without knowing for sure so to demonstrate this we've got a contract called balance reader that's pretty simple it's going to get the erc20 token balance of an address and a token in this case we're going to run a test that gets the usdc balance of the arbitrum one Gateway the first thing we need to do is take a look at our Imports unlike with remix we can't import directly from GitHub so instead we need to import the open Zeppelin contracts the same as you'd import any other package and we can do that with yarn add open Zeppelin slash contracts all right so let's see what happens if we run the test we can do npx hard hat test and since this is still in our repository with our lock contract but we don't want to run that we can pick individual tests to run by adding a path to the end of this so we'll say that slash test and then we'll run the balance reader and we've gotten an error the transaction is reverted because the function returned an unexpected amount of data this usually means that there's been an error of some sort and the reason why we're getting an error is because we're calling our get erc20 balance of function but the address and account for the account and the token address don't exist on the hard hat Network so what we need to do is we need to update our hard hat config and add this network we'll add it like any other network but what we're going to do is we're going to override hard hat which runs as the default network if you don't specify and we'll add a property for forking and then we need a URL and in this case we're going to use alchemies URL with our Alchemy key so I'll grab that from over here to save some typing and again note that I've set up a mainnet key in our DOT EnV file and then second we need to enable it by adding enabled as true so if we wanted to later stop using forking in our tests we can disable it easily just by making this false now we can run the tests again and we can see that the usdc balance of arbitrim Gateway is quite large so this is an introduction to the features available with forking but there are a number of other really powerful tools that you can use um among other things you could impersonate accounts so you could impersonate this address and use it to test transferring large amounts of usdc from one account to the other and making sure that if you're sending several million dollars worth of stuff that you're doing it correctly you can also more directly manipulate data on the blockchain for testing such as setting the balance or setting storage for your contract

Automatic transcript — names and jargon may be misspelled.