How to deploy Starknet contracts to testnet or mainnet

Okoli Evans
5 min readNov 8, 2023
starknet banner

Deploying and interacting with smart contracts on Starknet can be a little tricky, especially for newbies that are not used to the ecosystem yet; this article is a guide for such developers or users. If you need a local environment to deploy and play around with your contracts, you can find details on how to deploy locally using Katana here.

To get the full details on how to deploy contracts to Starknet mainnet or testnet, I will be walking us through the process by deploying this sample contract below.

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, value: felt252) {
self.value.write(value);
}

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

Deploying the contract

To deploy a Starknet contract we have to first understand how accounts work on Starknet. Starknet accounts are by default smart contract accounts, that means one has to create, fund and deploy the account first before they can perform transactions with such accounts; we need a wallet to achieve this. The two major wallets that are used on Starknet are Argent X and Braavos.

Setting up an account:

  1. Install Braavos or Argent X wallet extensions.
  2. Create a new account.
  3. Fund your account with ETH. Click on `Deposit` on Braavos or `Add funds` on Argent X, then select `Starknet token faucet` to get testnet tokens. You can also use the Starknet Goerli faucet, here. If you want to work on mainnet you can use the StarkGate bridge to send Ether from L1. You can find the complete list of bridges on Starknet here.
  4. For Braavos, on the account page, deploy your account by clicking on `set up your account on-chain`. For Argent X, make an outgoing transaction from the account, this transaction will automatically deploy your account on-chain. A simple way to achieve this is to send a few tokens to account2 in your wallet.

Now that we have successfully created and deployed our accounts, we can now proceed with the next steps.

Installing Starkli

Starkli is a 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.

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. We need a private key for this…

For Braavos users:

i. Click on account.

ii. On the account banner, click on the settings icon and click on ‘show private key’.

iii. Unlock and copy private key.

For Argent X users:

i. Click on the settings icon on top right.

ii. Click on the account banner, then click on `export private key`.

iii. Unlock and copy private key.

To create the keystore, after copying the private key from your wallet account, 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 will hold your account details so they can be assessed by Starkli when needed. You need an rpc for this so as to specify the network from which the account will be fetched.

One popular rpc endpoint used by many is Infura. Simply create an account on https://infura.io, go to your dashboard and click on `my first key`. On the list scroll down to ‘Starknet’, open the dropdown and select the network you want to deploy on- testnet or mainnet.

Now use this command to fetch and set your account:

starkli account fetch <YOUR WALLET ADDRESS> --rpc https://starknet-goerli.infura.io/v3/infura-api-key --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 our contract_class, next is to declare the contract:

starkli declare target/dev/MYFOLDER_simple_storage.contract_class.json --rpc https://starknet-goerli.infura.io/v3/infura-api-key --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 https://starknet-goerli.infura.io/v3/infura-api-key --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 Starknet 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 https://starknet-goerli.infura.io/v3/infura-api-key --account account0_account.json --keystore account0_keystore.json

Sample output:

evanseth@evanseth-G3–3579:~/Cairo/YaslonV5$ starkli deploy 0x01036c88d718fd7ffd221b3834e9b3e1593d798a8a9fbb379370aa18853ec98f - rpc https://starknet-goerli.infura.io/v3/infura-api-key - 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

Yeahhhh!!! You have successfully deployed a contract on Starknet testnet (or mainnet).

The deployment address can be used to inspect the contract, get the contract’s information or interact with the contract on any Starknet explorer like https://starkscan.co/ , https://goerli.voyager.online/ or https://starkcompass.com/

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 to the tools. I will try to update the article though as new upgrades are released.

However, if you run into error using any commands in the 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/ch02-05-testnet-deployment.html

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

--

--

Okoli Evans

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