How to deploy Starknet contracts locally using Katana and Starkli

Okoli Evans
CoinsBench
Published in
5 min readOct 5, 2023

--

Starknet banner

The Starknet ecosystem has been very vibrant and continuously expanding thanks to their amazing developer community. Starknet is a layer2 scaling protocol built on Ethereum using Zero Knowledge technologies, and relies on the most scalable proof system: STARK.

Starknet contracts are built using a specialized, Rust-inspired, language called Cairo. The ecosystem boasts of a number of developer tools that make development experience simple and fast. Some of the most necessary tools include:

i. Scarb — The package manager for Cairo.

ii. Starkli — Starknet CLI used for declaring, deploying and interacting with deployed contracts, also used to get Starknet data such as block information, transaction details and more.

iii. Katana — Runs Starknet node for local development.

iv. Starknet Foundry — Foundry-inspired toolchain for developing, testing and deploying Starknet contracts.

Sample Starknet contract

We are going to deploy a simple storage contract that takes a value and saves to state, we will also have a function to read the saved value from storage:

#[starknet::contract]
mod Simple_storage {

#[storage]
struct Storage {
value: felt252,
}

fn set_value(ref self: ContractState, val: felt252) {
self.value.write(val);
}

fn get_value(self: @ContractState) -> felt252 {
self.value.read()
}
}

Deploying the contract

To deploy a Starknet contract we have to understand how accounts work on Starknet. Starknet accounts are by default smart contract accounts, that means one has to generate, fund and deploy the account first before they can perform transactions with such accounts.

Now this is where we understand fully why we need Katana. Instead of going through the long process of generating, funding and deploying an account, we can simply spring up prefunded and already deployed accounts using Katana. To install Katana, run these commands:

git clone https://github.com/dojoengine/dojo
cd dojo
cargo install --path ./bin/katana --locked --force

To start Katana:

katana

You’ll get a set of accounts like this:

PREFUNDED ACCOUNTS
==================
| Account address | 0x3ee9e18edc71a6df30ac3aca2e0b02a198fbce19b7480a63a0d71cbd76652e0
| Private key | 0x300001800000000300000180000000000030000000000003006001800006600
| Public key | 0x1b7b37a580d91bc3ad4f9933ed61f3a395e0e51c9dd5553323b8ca3942bb44e

| Account address | 0x33c627a3e5213790e246a917770ce23d7e562baa5b4d2917c23b1be6d91961c
| Private key | 0x333803103001800039980190300d206608b0070db0012135bd1fb5f6282170b
| Public key | 0x4486e2308ef3513531042acb8ead377b887af16bd4cdd8149812dfef1ba924d

ACCOUNTS SEED
=============
0
🚀 JSON-RPC server started: http://127.0.0.1:5050

Once we have katana up and running, the next thing is to set up our smart wallet using Starkli. To install Starkli run the following commands:

curl https://get.starkli.sh | sh
starkliup

You can confirm if the installation was successful using the command:

starkli --version

After installing Starkli, to create our smart wallet we need an encrypted keystore to hold our account credentials safely, the encrypted values in the keystore are what we need to create the signer.

To create the keystore, copy a private key from one of the accounts generated by katana, then run the command:

starkli signer keystore from-key account0_keystore.json

…this will demand you to paste a private key in the cli, the key you paste will not be visible so simply hit enter. You will be required to enter a password, again it will not be visible so just hit enter when you are done.

Sample output:

Created new encrypted keystore file: /home/evanseth/Cairo/YaslonV5/acct2_keystore.json
Public key: 0x04c0f884b8e5b4f00d97a3aad26b2e5de0c0c76a555060c837da2e287403c01d

Now we create the account descriptor. This file will hold your account details so they can be assessed by Starkli when needed. Use this command:

starkli account fetch <SMART WALLET ADDRESS> --rpc http://0.0.0.0:5050 --output account0_account.json

Now that we have fixed the signer and the account descriptor, next is to declare the contract. We need to declare the contract why?

We declare contracts to create a class hash. This class hash is unique for each contract and does not change provided the contract is not edited and recompiled, and this class hash is what will be deployed to the blockchain.

Starting Deployment Process

To begin deployment, we need to compile our contract to generate a contract_class.json file that we need.

To compile:

scarb build

If the compilation is successful, you should get a compiled file in the target/dev folder as “MYFOLDER_simple_storage.contract_class.json”.

Now that we have a compiled sierra file, next is to declare the contract:

starkli declare target/dev/MYFOLDER_Simple_storage.contract_class.json --rpc http://0.0.0.0:5050 --account account0_account.json --keystore account0_keystore.json

Sample output:

evanseth@evanseth-G3–3579:~/Cairo/YaslonV5$ starkli declare target/dev/YaslonV5_Simple_storage.contract_class.json - rpc http://0.0.0.0:5050 - account acct1.json - keystore acct1_keystore.json
Enter keystore password:
Sierra compiler version not specified. Attempting to automatically decide version to use…
Unknown network. Falling back to the default compiler version 2.1.0. Use the - compiler-version flag to choose a different version.
Declaring Cairo 1 class: 0x01036c88d718fd7ffd221b3834e9b3e1593d798a8a9fbb379370aa18853ec98f
Compiling Sierra class to CASM with compiler version 2.1.0…
CASM class hash: 0x0317d3ac2cf840e487b6d0014a75f0cf507dff0bc143c710388e323487089bfa
Contract declaration transaction: 0x050fdaeaf58a8fc1b34a2bb2f7739442a68b86798e5ea0be17d8839c07450925
Class hash declared:
0x01036c88d718fd7ffd221b3834e9b3e1593d798a8a9fbb379370aa18853ec98f

If you encounter `compiler version` error check if your compiler version is older than 2.1.0, if so you can either specify a compiler version to use in the command by adding the ` — compiler-version x.y.z` flag, or you can upgrade your starkli version to make sure you are using the latest version, use the command:

starkliup

To deploy the contract, we make use of the “Class hash declared” since what is deployed is the class hash generated from the contract:

starkli deploy <CLASS HASH DECLARED> --rpc http://0.0.0.0:5050 --account account0_account.json --keystore account0_keystore.json

Sample output:

evanseth@evanseth-G3–3579:~/Cairo/YaslonV5$ starkli deploy 0x01036c88d718fd7ffd221b3834e9b3e1593d798a8a9fbb379370aa18853ec98f - rpc http://0.0.0.0:5050 - account acct1.json - keystore acct1_keystore.json
Enter keystore password:
Deploying class 0x01036c88d718fd7ffd221b3834e9b3e1593d798a8a9fbb379370aa18853ec98f with salt 0x0045c8c2cf473332126356fae35310cead51a83f4c60db140341635cf069c4cd…
The contract will be deployed at address 0x00161b81bab755671699585aad359a0e51f2c72d3b7f7f7b94d8233099ed4ff2
Contract deployment transaction: 0x0460959b2b75a875cb45ae95c23060773c70c85dddd6a2ebb15ef6a83ca115e2
Contract deployed:
0x00161b81bab755671699585aad359a0e51f2c72d3b7f7f7b94d8233099ed4ff2

Viola!!! You have successfully deployed a Starknet contract locally using Starkli and Katana. The deployment address can be used to interact locally with the contract either by calling the contract (reading from the contract) or by invocation (writing to the contract).

P.S:

Depending on the time you are reading this, some commands written here in this article might not work as expected, or even work at all due to upgrades or changes that might happen. I will try to update the article though as new upgrades are released, but you can always get updated information from the official documentations… check references below.

Having said that, if you run into error using any commands in this article, please leave a comment with the description of the error, or get in touch with me here: https://twitter.com/OkoliEvans

References

Cairo book: https://book.cairo-lang.org/ch99-01-04-01-voting-contract.html

Starknet book: https://book.starknet.io/chapter_1/first_contract.html

Starknet: https://www.starknet.io/en

Starknet Foundry: https://foundry-rs.github.io/starknet-foundry/

--

--

Blockchain and Smart contract developer specializing in EVM & Starknet contracts. Proficient in Solidity, Rust and Cairo.