Skip to content

Commit 149b179

Browse files
LandauRazamanusk
andauthored
"Declaring a smart contract" refactor (#1416)
* Update declare docs to use scarb new * various edits * minor fixes * minor fixes * minor fix * added pr1359 * fixed typos --------- Co-authored-by: amanusk <[email protected]>
1 parent 192a904 commit 149b179

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed
Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,162 @@
11
= Declaring a smart contract
22

3+
== Overview
4+
5+
Before a contract is deployed on Starknet it first needs to be _declared_. Declaration is the process of submitting a contract's code to Starknet and making it available for future deployments, analogous to registering its blueprint.
6+
7+
This page will guide you through the steps necessary to declare a smart contract on Starknet. For more details on declaration and its benefits, see xref:architecture-and-concepts:smart-contracts/contract-classes.adoc[Contract classes and instances].
38

49
== Prerequisites
510

6-
=== Ensure Starkli and Scarb are installed correctly
7-
Ensure that the below commands are working properly on your system.
11+
Ensure that the following commands are working properly on your system:
812

913
[source, bash]
1014
----
1115
starkli --version
1216
scarb --version
17+
snforge --version
18+
sncast --version
19+
asdf --version
1320
----
1421

1522
If either of the above commands fails, see xref:environment-setup.adoc[Setting up your environment].
1623

17-
== Introduction
18-
19-
Deploying a smart contract in Starknet requires two steps:
20-
21-
* Declaring the class of your contract, i.e. sending your contract's code to the network.
22-
* Deploying a contract, i.e. creating an instance of the code you previously declared.
23-
24-
[TIP]
25-
====
26-
If you require a smart contract for testing, you can use this sample contract, link:https://github.com/starknet-edu/starknetbook/blob/main/examples/vote-contracts/src/lib.cairo[`lib.cairo`], from the Starknet Book.
27-
====
28-
2924
== Compiling a smart contract
3025

31-
You can compile a smart contract using the Scarb compiler.
32-
33-
To compile a smart contract, create a directory containing a `Scarb.toml` file and a subdirectory named `src` containing your contract source code.
26+
Before a smart contract can be declared, it first needs to be compiled. To compile an existing smart contract project, simply navigate into the project's directory and run:
3427

35-
Add the following code to the `Scarb.toml` file:
36-
37-
[source,toml]
28+
[source,bash]
29+
----
30+
scarb build
3831
----
39-
[package]
40-
name = "contracts"
41-
version = "0.1.0"
4232

43-
[dependencies]
44-
starknet = ">=2.2.0"
33+
The compiled contract should be saved in the `target/dev/` directory.
4534

46-
[[target.starknet-contract]]
47-
sierra = true
48-
----
35+
If you require a new smart contract project, run either:
4936

50-
Navigate into the newly created directory:
5137
[source,bash]
5238
----
53-
cd <dir_name>
39+
scarb init
5440
----
5541

56-
Run the following command:
42+
in an empty folder or:
5743

5844
[source,bash]
5945
----
60-
scarb build
46+
scarb new <PROJECT_NAME>
6147
----
6248

63-
The compiled contract will be saved in the `target/dev/` directory.
49+
anywhere, and select the default Starknet Foundry as a test runner.
6450

65-
The contract is now compiled and ready to be deployed. Next you will need to declare an RPC provider within your contract.
66-
67-
== Setting an RPC provider
51+
[NOTE]
52+
====
53+
Building a Starknet Foundry project with Scarb requires https://www.rust-lang.org/[Rust] to be installed. You can verify that Rust is installed by running:
6854
69-
To interact with the Starknet network, you need to set an RPC endpoint within Starkli.
55+
rustc --version
7056
71-
The following are the RPC providers available for Starknet:
57+
or install Rust by following the instructions in the https://doc.rust-lang.org/beta/book/ch01-01-installation.html[Rust documentation].
7258
73-
[cols="1,2"]
74-
|===
75-
|Provider name |Description
59+
Moreover, the first time a project is built, some components of Scarb are compiled locally with the Rust toolchain. This process may take a few minutes, but will not happen in subsequent builds.
60+
====
7661

77-
|Infura or Alchemy
78-
|Use a provider like Infura or Alchemy.
62+
In any case, the `Scarb.toml` file in the project's directory should resemble the following (up to versions number):
7963

80-
|Custom configuration
81-
|Set up your own node and use the RPC provider of your node. More information on this can be found within the link:https://book.starknet.io/chapter_4/node.html[Starknet Book].
64+
[source,toml]
65+
----
66+
[package]
67+
name = <PROJECT_NAME>
68+
version = "0.1.0"
69+
edition = "2023_11"
8270
83-
|===
71+
[dependencies]
72+
starknet = "2.8.4"
8473
85-
For demonstration purposes, the Starknet Sequencer's Gateway is used in the below steps.
74+
[dev-dependencies]
75+
snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v0.32.0" }
76+
assert_macros = "2.8.4"
8677
87-
== Declaring a smart contract
78+
[[target.starknet-contract]]
79+
sierra = true
80+
----
8881

89-
A contract can be declared on Starknet using the following command:
82+
== Setting an RPC provider
9083

91-
[source,bash]
92-
----
93-
starkli declare target/dev/<NAME>.json --network=sepolia --compiler-version=2.1.0
94-
----
84+
In order to interact with Starknet, Starkli requires an RPC endpoint to be configured. For interactions with Starknet Sepolia and Starknet mainnet, Starkli supports default (and limited) RPC endpoints when using the `--network` flag. Configuring a custom RPC endpoint can be done by either using Starkli's `--rpc` flag or setting up Starkli's `STARKNET_RPC` environment variable (see more details in the https://book.starkli.rs/providers#using-an-rpc-url-directly[Starkli documentation]). To review all Starknet RPC providers, see xref:tools:api-services.adoc[API providers].
9585

96-
[NOTE]
97-
====
98-
The `--network` flag is used to specify the network you want to use, it could also be `mainnet` for example.
86+
For demonstration purposes, this tutorial uses Starkli's default Starknet Sepolia RPC endpoint by setting `--network=sepolia`.
9987

100-
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.
101-
====
10288

89+
== Declaring a smart contract
10390

104-
You can find the compiler version supported by Starkli by running:
91+
A contract can be declared on Starknet using Starkli by running following command:
10592

10693
[source,bash]
10794
----
108-
starkli declare --help
95+
starkli declare target/dev/<PROJECT_NAME>.sierra.json --network=sepolia
10996
----
11097

111-
In the `--compiler-version` flag you will see possible versions of the compiler:
98+
When using `starkli declare`, Starkli will do its best to identify the compiler version of the declared class. In case it fails, the `--compiler-version` flag can be used to specify the version of the compiler as follows:
11299

100+
. Find the compiler versions supported by Starkli by running:
101+
+
113102
[source,bash]
114103
----
115-
--compiler-version <COMPILER_VERSION>
116-
Statically-linked Sierra compiler version [possible values: 2.0.1, 2.1.0]
104+
starkli declare --help
117105
----
106+
+
107+
and looking for the possible values of the `--compiler-version` flag.
118108

119-
However, the Scarb compiler version may be `2.2.0`, you can find this out by running:
120-
109+
. Find the current Scarb version in use:
110+
+
121111
[source,bash]
122112
----
123113
scarb --version
124114
----
125115

126-
This is because Starkli and Scarb are not always in sync.
127-
128-
In this case you would need to use the compiler version that Starkli is using by installing a previous version of Scarb. See the https://github.com/software-mansion/scarb/releases[Scarb github repo] for more detail.
116+
. In case a different compiler version is required, switch to a different Scarb version using `asdf`:
129117

130-
You can do this by running the following command for installing Scarb version `0.6.1`:
118+
.. Install the desired Scarb version:
119+
+
120+
[source,bash]
121+
----
122+
asdf install scarb <VERSION>
123+
----
131124

125+
.. Select the desired Scarb version as the local version for the project:
126+
+
132127
[source,bash]
133128
----
134-
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh -s -- -v 0.6.1
129+
asdf local scarb <VERSION>
135130
----
136131

137-
If you were using a provider like Infura or Alchemy, the declaration command would look like this:
132+
[TIP]
133+
====
134+
The following is an example of declaring a contract with both a a custom RPC endpoint (provided by https://www.infura.io/[Infura]) and a specific compiler version:
138135
139136
[source,bash]
140137
----
141-
starkli declare target/dev/contracts_Ownable.sierra.json \
138+
starkli declare target/dev/<PROJECT_NAME>.sierra.json \
142139
--rpc=https://starknet-sepolia.infura.io/v3/<API_KEY> \
143-
--compiler-version=2.6.0
140+
--compiler-version=2.6.0 \
144141
----
142+
====
145143

146144
== Expected result
147145

148-
The result of the declaration command is a contract class hash:
146+
The output of a successful contract declaration using Starkli should resemble the following:
147+
149148
[source,bash]
150149
----
151150
Class hash declared: <CLASS_HASH>
152151
----
153152

154-
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 https://sepolia.starkscan.co/class/0x00e68b4b07aeecc72f768b1c086d9b0aadce131a40a1067ffb92d0b480cf325d[StarkScan] to see the contract class hash in the blockchain.
155-
156-
If the contract you are declaring has previously been declared by someone else, you will get an output like this:
153+
On the other hand, if the contract you are declaring has previously been declared, the output should resemble the following:
157154

158155
[source,bash]
159156
----
160157
Not declaring class as its already declared. Class hash: <CLASS_HASH>
161158
----
159+
160+
This is because declaration is a one-time process for each unique contract code, and a contract's class hash is its unique identifier (for more details, see xref:architecture-and-concepts:smart-contracts/class-hash.adoc[Class hash]).
161+
162+
In both cases, however, you should be able to see the declared contract on a block explorer like https://sepolia.starkscan.co/[StarkScan] or https://sepolia.voyager.online/[Voyager] by searching for its class hash.

0 commit comments

Comments
 (0)