Getting Started with StarkNet A Decentralized ZK-Rollup

Neha Kumari
4 min readApr 11, 2022

What is Starknet?

For Ethereum Scalability solution's most promising approaches are rollups Rollups are the way to “roll-up” a bunch of transactions into one single piece of data to save space and processing power on the blockchain. Right now, there are 2 main rollups: ZKsnarks and Optimistic rollups And one of the main projects implementing ZK Rollups are StarkNet.

StarkNet is a permissionless decentralized ZK-Rollup operating as an L2 network over Ethereum. This enables any dApp to achieve unlimited scale for its computation while leveraging Ethereum’s security. Developers can write Starknet contracts and deploy them on the network, and users can send transactions to these contracts (in a similar manner to how this is done on Ethereum).

The StarkNet node (called sequencer) is implemented in Python. The StarkNet transaction execution environment, called StarkNet OS (similar to the Ethereum Virtual Machine), is implemented in Cairo. This optimizes the proving performance of each transaction execution. A Solidity contract deployed on Ethereum connects the StarkNet network (L2) to Ethereum (L1).

How to write my first contract?

Cairo is the native programming language both for its infrastructure and for writing StarkNet contracts, teams are developing transpilers to Cairo from Solidity and other programming languages.

if you want to know more about Cairo check out the documentation

There are a few ways to write the first contract you can also do this via StarkNet CLI check out this guide if you want to know more but in this guide, we will be using nile for setting up a development environment for Cairo

Getting Started with nile :

Create a folder and cd into It

$ mkdir my-contract
$ cd my-contract

we will need to create a virtual environment for installing nile:

$ python3 -m venv env
$ source env/bin/activate

now you will notice a virtual environment has been created now we can install nile

$ pip install cairo-nile...
Successfully installed cairo-nile-0.5.2 click-8.1.2 importlib-metadata-4.11.3 python-dotenv-0.20.0 zipp-3.8.0

now we need to run this command :

$ nile init✅ Dependencies successfully installed
🗄 Creating project directory tree
⛵️ Nile project ready!

Now, type ls

$ ls
accounts.json contracts env Makefile node.json tests

Nile comes with a default smart contract which is inside the contracts folder named contract. Cairo

contract.cairo

Now let's compile this contract

$ nile compile
📁 Creating artifacts/abis to store compilation artifacts
🤖 Compiling all Cairo contracts in the contracts directory
🔨 Compiling contracts/contract.cairo
✅ Done

after compiling the contract there will be a new folder generated called artifacts which contains contract.json in which every information about the contract will be generated same as the solidity contract which will later help in the deployment.

It also comes with the test file for writing tests. running the test run the following command

$ pytest

Deployment

run the following command for running your local node if you are familiar with Ethereum layer1 development It is similar to running ganache

$  nile node* Listening on http://127.0.0.1:5000/ (Press CTRL+C to quit)# now lets deploy open an another terminal in same folder and paste $ source env/bin/activate
$ nile deploy contract — alias my_first_contract
🚀 Deploying contract
⏳ ️Deployment of contract successfully sent at 0x079832c77183196377d40d57f2ce0f38ccab806748c10789b064a00bd1540a8c
🧾 Transaction hash: 0x38251ef35394d0e19442a61c743db7a640a183a4fff80fe9516374fcab4e2aa
📦 Registering deployment as my_first_contract in 127.0.0.1.deployments.txt

note It will deploy by default to the local running node we can also deploy to mainnet or testnet.

let's deploy It to Goerli Testnet

nile deploy contract --alias my_first_contract--network alpha-goerli🚀 Deploying contract
⏳ ️Deployment of contract successfully sent at 0x02f15a5d7ac40f99c7fe440d8cb2dd1c7832b361fc255d7b754656b5d7e2af4d
🧾 Transaction hash: 0x1cd4801654c0f755546f87251d8804535a5d9715a6ecd1976e36d330a3695f7
📦 Registering deployment as my_first_contract in goerli.deployments.txt

Now let's check the transaction on voyager which is developed by Nethermind select the testnet and enter transaction hash

Using call and invoke, we can interact with the contract either on explorer or through the terminal

$ nile <command> <contract_identifier> <contract_method> [PARAM_1, PARAM2…]where command is call or invoke and contract identifier is the name of deployed address or alias

Now check the balance of the contract

$ nile call my_first_contract get_balance
0

This is how we can invoke the contract whenever done playing we can clean the workspace

$ nile clean🚮 Deleting artifacts directory
✨ Workspace clean, keep going!

Conclusion:

In this guide, we were able to understand about Zk-Rollup, Starknet, creating the first contract in Cairo, and deploying it locally and on a test net. For more check out StarkNet.

--

--