Did you know you can fork the StrakNet state to your local Devnet? Here - we’ll show you how.
Hello fren!👋
Did you know you can fork the StrakNet state to your local Devnet? Here - we’ll show you how.
Let’s dig in!
In general, forking allows you to copy the blockchain state, from a certain block to your local environment (in our case, StarkNet state to a local Devnet).
Why would you want to do this?
First reason would be so that you can work locally, test, and play without making any changes to the real blockchain.
Second reason - you can work locally with existing smart contracts, already deployed on the testnet / mainnet blockchain, without recreating them. For example, if you want to work with a particular dApp, you can find its smart contract(s) and fork the network for that block. This will allow you to test your project with that dApp without making real transactions.
First, of course, you need an installed and working devnet.
For devnet OS specific installation and instructions in general check here.
If you already have installed starknet-devnet before, make sure you have the latest version.
Check that with:
If not, you can download it by running:
You can take a look in the official documentation about commands and details!
To fork a desired network state, start the devnet with:
This will fork the alpha-goerli testnet on the last block (exact block number is shown at the bottom of the screenshot):
Of course, you can specify other networks, specify the block number etc. - check the docs!
Now, you can interact with the devnet the way you are used to, but we will give one small example - querying for an account balance for an address deployed on the alpha-goerli testnet (the one we forked)!
In another console window, enter this command, and provide some testnet address:
The response should be something like:
And that’s it, congratulations! 🥳
If you have already used the Hardhat plugin - read on! If not, check out our tutorial and get yourself familiar with it!
In order to use the forked devnet inside of your Hardhat project, you should modify your hardhat.config.ts file similar to:
Where the most important part is the:
Now, you can add a script in the /scripts folder - we can call it quick-script.ts:
Next, run the script like so:
For this particular contract, which was already deployed on the alpha-goerli testnet, you should get an output on the console like: Balance: 10n.
But, since we are now using the forked devnet, everything is happening locally, on your machine! :)
Many other tutorials coming our way - we will cover other use cases of the devnet, so make sure you follow our blog and Twitter.
Happy coding! 🚀🚀🚀
This article is also available on SpaceShard Medium.*