20.2.2023

Starknet Devnet Forking

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!

Forking the Blockchain

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.

The setup

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:


>  starknet-devnet —version.

If not, you can download it by running:


> pip install starknet-devnet

Forking in Devnet

You can take a look in the official documentation about commands and details!

To fork a desired network state, start the devnet with:


> starknet-devnet --fork-network alpha-goerli

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!

Querying the forked chain

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:


> curl http://127.0.0.1:5050/account_balance?address=0xfe9a86ee02e03a8906003573aa5659adccb1f79eed4ce839bc6d27b53d6e98 -H "Content-Type:application/json"

The response should be something like:


{"amount":1981928855813672,"unit":"wei"}

And that’s it, congratulations! 🥳

Appendix: Use the forked devnet with StarkNet Hardhat plugin

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:


import "@shardlabs/starknet-hardhat-plugin";

module.exports = {

starknet: {

network: "integrated-devnet"

},

networks: {

integratedDevnet: {

url: "http://127.0.0.1:5050",

// optional devnet CLI arguments, read more here: https://shard-labs.github.io/starknet-devnet/docs/guide/run

args: ["--fork-network", "alpha-goerli"],

// stdout: "logs/stdout.log" <- dumps stdout to the file

stdout: "STDOUT", // <- logs stdout to the terminal

// stderr: "logs/stderr.log" <- dumps stderr to the file

stderr: "STDERR"  // <- logs stderr to the terminal

}

}

};

Where the most important part is the:


args: ["--fork-network", "alpha-goerli"],

Now, you can add a script in the /scripts folder - we can call it quick-script.ts:


import hardhat from "hardhat";

async function main() {

const contractFactory = await hardhat.starknet.getContractFactory("hello");

// get already deployed contract on the alpha-goerli testnet

const contract = contractFactory.getContractAt("0x672dadbdbd0da6f21af88dbf363a8865c063fbd95c39ae20820055c1429624d");

const { res: balance } = await contract.call("get_balance");

console.log("Balance: ", balance);

}

main()

.then(() => process.exit(0))

.catch((error) => {

console.error(error);

`process.exit(1);

});

Next, run the script like so:


>  npx hardhat run scripts/quick-script.ts

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! :)

Up next

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.*

Downlaod all images