20.1.2023

Getting started with Starknet Hardhat Plugin (Part 2: “Connect to devnet”)

Second part of a series of tutorials on how to use the StarkNet Hardhat Plugin for Cairo smart contract development.

Hello again frens! 👋

In our previous tutorial (tutorial Part 1) you may have noticed that doing this on the testnet can be slow 😞

But don't worry - in this tutorial Part 2, we will show you how to do the same operations, but on a local instance of the StarkNet network, called the devnet!

If you already have installed starknet-devnet before, make sure you have the latest version with. Check that with:


>  starknet-devnet —version.

If not, you can download it by running:


> pip install starknet-devnet


For devnet device specific and installation in general check here

Start Devnet

In this tutorial we are going to deploy our contract on Devnet, so to make that possible, open a new terminal, and then run


> starknet-devnet --seed 0

--seed 0 ensures the creation of the same pre-deployed accounts each time.

From the output should see that we have a bunch of pre-deployed accounts with funds in them:

Account #0

Address: 0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a

Public key: 0x7e52885445756b313ea16849145363ccb73fb4ab0440dbac333cf9d13de82b9

Private key: 0xe3e70682c2094cac629f6fbed82c07cd

Account #1

Address: 0x69b49c2cc8b16e80e86bfc5b0614a59aa8c9b601569c7b80dde04d3f3151b79

Public key: 0x175666e92f540a19eb24fa299ce04c23f3b75cb2d2332e3ff2021bf6d615fa5

Private key: 0xf728b4fa42485e3a0a5d2f346baa9455

Etc …

We’ll use one of those accounts to deploy and interact with our contracts.

Configure the Plugin

First and foremost let’s configure our hardhat.config file in our project, to be able to connect to the local devnet.
The config file should now look like this.


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


module.exports = {
   starknet: {
       dockerizedVersion: "0.10.3",
       network: "devnet"
   },
   networks: {
       devnet: {
           url: "http://127.0.0.1:5050"
       }
   }
};

Commands

In the console, write this command:


> npx hardhat starknet-compile contracts/hello.cairo

From the tutorial Part 1, you can use the already compiled hello.cairo contract! 

Note: there is a convenient difference in Part 2 with this step, compering to Part 1 tutorial. Because Devnet gives us 10 already pre-deployed accounts, we don’t need to go through all the same steps! We can skip the declaration and deployments of accounts!

We’ll just use Account #0 from the devnet output to deploy our contract and interact with it! Let’s add a new file to our project and name it quick-script.ts.  We can then copy the following code to it:


import hardhat, { starknet } from "hardhat";
 
async function main() {
   const account = await starknet.OpenZeppelinAccount.getAccountFromAddress(
       "0x7e00d496e324876bbc8531f2d9a82bf154d1a04a50218ee74cdd372f75a551a", // OZ ACCOUNT ADDRESS
       "0xe3e70682c2094cac629f6fbed82c07cd" // OZ PRIVATE KEY
   );

const contractFactory = await hardhat.starknet.getContractFactory("hello");
   await account.declare(contractFactory);
   const contract = await account.deploy(contractFactory);
 
   console.log("Deployed to:", contract.address);
   const { res: balanceBefore } = await contract.call("get_balance");
   console.log("Balance before invoke: ", balanceBefore);
 
   await account.invoke(contract, "increase_balance", { amount: 30 });
   const { res: balanceAfter } = await contract.call("get_balance");
   console.log("Balance after invoke:", balanceAfter);
}
 
main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error);
        process.exit(1);
    });

Finally, we run the script with:


>  npx hardhat run scripts/quick-script.ts 

…which will declare, deploy and make some calls to the contract.

Hooray! You did it! 

You can see that the contract was deployed to the local network Devnet and we made some other transactions as well, getting the balance and increasing balance!

And that’s it! Congratulations again! 😃

More information

Also be sure to check out the hardhat-example repository for more examples on features and usage!

Up Next

There will be more tutorials, so make sure you follow our blog and Twitter

Happy coding!🚀🚀🚀

‍‍*This article is also available on SpaceShard Medium.

Downlaod all images