Declaring a smart contract

Last edit



Ensure Starkli and Scarb are installed correctly

Ensure that the below commands are working properly on your system.

starkli --version
scarb --version

If either of the above commands fails, see Setting up your environment.


Deploying a smart contract in Starknet requires two steps:

  • Declaring the class of your contract, i.e. sending your contract’s code to the network.

  • Deploying a contract, i.e. creating an instance of the code you previously declared.

If you require a smart contract for testing, you can use this sample contract, lib.cairo, from the Starknet Book.

Compiling a smart contract

You can compile a smart contract using the Scarb compiler.

To compile a smart contract, create a directory containing a Scarb.toml file and a subdirectory named src containing your contract source code.

Add the following code to the Scarb.toml file:

name = "contracts"
version = "0.1.0"

starknet = ">=2.2.0"

sierra = true

Navigate into the newly created directory:

cd <dir_name>

Run the following command:

scarb build

The compiled contract will be saved in the target/dev/ directory.

The contract is now compiled and ready to be deployed. Next you will need to declare an RPC provider within your contract.

Setting an RPC provider

To interact with the Starknet network, you need to set an RPC endpoint within Starkli.

The following are the RPC providers available for Starknet:

Provider name Description

Infura or Alchemy

Use a provider like Infura or Alchemy.

Custom configuration

Set up your own node and use the RPC provider of your node. More information on this can be found within the Starknet Book.

For demonstration purposes, the Starknet Sequencer’s Gateway is used in the below steps.

Declaring a smart contract

A contract can be declared on Starknet using the following command:

starkli declare target/dev/<NAME>.json --network=sepolia --compiler-version=2.1.0

The --network flag is used to specify the network you want to use, it could also be mainnet for example.

The --compiler-version flag is used to specify the version of the compiler you want to use. Starkli is currently running on version 2.6.x of the compiler.

You can find the compiler version supported by Starkli by running:

starkli declare --help

In the --compiler-version flag you will see possible versions of the compiler:

--compiler-version <COMPILER_VERSION>
          Statically-linked Sierra compiler version [possible values: 2.0.1, 2.1.0]

However, the Scarb compiler version may be 2.2.0, you can find this out by running:

scarb --version

This is because Starkli and Scarb are not always in sync.

In this case you would need to use the compiler version that Starkli is using by installing a previous version of Scarb. See the Scarb github repo for more detail.

You can do this by running the following command for installing Scarb version 0.6.1:

curl --proto '=https' --tlsv1.2 -sSf | sh -s -- -v 0.6.1

If you were using a provider like Infura or Alchemy, the declaration command would look like this:

starkli declare target/dev/contracts_Ownable.sierra.json \
    --rpc=<API_KEY> \

Expected result

The result of the declaration command is a contract class hash:

Class hash declared: <CLASS_HASH>

This hash is the identifier of the contract class in Starknet. You can think of it as the address of the contract class. You can use a block explorer like StarkScan to see the contract class hash in the blockchain.

If the contract you are declaring has previously been declared by someone else, you will get an output like this:

Not declaring class as its already declared. Class hash: <CLASS_HASH>