Declaring a smart contract
Ensure that the below commands are working properly on your system.
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,
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
name = "contracts"
version = "0.1.0"
starknet = ">=2.2.0"
sierra = true
Navigate into the newly created directory:
Run the following command:
The compiled contract will be saved in the
The contract is now compiled and ready to be deployed. Next you will need to declare an RPC provider within your contract.
To interact with the Starknet network, you need to set an RPC endpoint within Starkli.
The following are the RPC providers available for Starknet:
Infura or Alchemy
Use a provider like Infura or Alchemy.
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.
A contract can be declared on Starknet using the following command:
starkli declare target/dev/<NAME>.json --network=goerli-1 --compiler-version=2.1.0
You can find the compiler version supported by Starkli by running:
starkli declare --help
--compiler-version flag you will see possible versions of the compiler:
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:
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
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | 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 \
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>