Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(declaration,deployment): Enhance contract declaration and deployment documentation #1359

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ A contract class does not necessarily require a deployed instance in Starknet.

A contract instance has a nonce, the value of which is the number of transactions originating from this address plus 1. For example, when you deploy an account with a `DEPLOY_ACCOUNT` transaction, the nonce of the account contract in the transaction is `0`. After the `DEPLOY_ACCOUNT` transaction, until the account contract sends its next transaction, the nonce is `1`.

== Declaration and deployment

Starknet separates the process of declaring a contract class from deploying contract instances. This separation offers several advantages:
[horizontal]
Gas optimization:: The contract code is stored on the network only once, reducing gas costs for subsequent deployments.
Reusability:: Multiple instances of the same contract can be deployed without repeatedly sending the full code.
Version control:: It allows for better management of contract versions across different deployments.

== Working with classes

[horizontal,labelwidth=20,role="stripes-odd"]
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@ 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.

image::declaration-process.png[]

[TIP]
====
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.
====

=== What is Declaration?

Declaration is the process of submitting your contract's code to the Starknet network, making it available for future deployments. It's a one-time process for each unique contract code. Think of declaration as registering a blueprint for your contract with the network.

Contract classes are analogous to classes in Object Oriented Programming. The class is defined once, and there can be many instances of it.

For more details on declaration and its benefits, see xref:contract-classes.adoc[Contract classes and instances].

== Compiling a smart contract

You can compile a smart contract using the Scarb compiler.
Expand Down Expand Up @@ -64,26 +74,6 @@ 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:

[cols="1,2"]
|===
|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 link:https://book.starknet.io/chapter_4/node.html[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:
Expand Down Expand Up @@ -159,3 +149,5 @@ If the contract you are declaring has previously been declared by someone else,
----
Not declaring class as its already declared. Class hash: <CLASS_HASH>
----

In this case, you can still use the block explorer to see the contract class hash in the blockchain.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Deploying a smart contract in Starknet requires two steps:

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

image::deployment-process.png[]

For more details on the declaration and deployment processes, see xref:contract-classes.adoc[Contract classes and instances].


== Deploying a smart contract
Deploying a smart contract involves instantiating it on Starknet. The deployment command requires the class hash of the smart contract and any arguments expected by the constructor.
Expand All @@ -45,6 +49,7 @@ starkli deploy \
--network=sepolia
----


== Expected result

After running the command and adding your password, you will see an output similar to this:
Expand All @@ -58,3 +63,39 @@ Contract deployed: 0x014825acb37c36563d3b96c450afe363d2fdfa3cfbd618b323f95b68b55
----

The smart contract has now been deployed to Starknet.

=== Verifying the Deployment

To verify your contract deployment:

* Using Starkli:
You can use the following command to retrieve the class hash of your deployed contract:

[source,bash]
----
starkli class-hash-at <CONTRACT_ADDRESS> --network=sepolia
----

* Using a Block Explorer:

You can use block explorers like https://voyager.online/ or https://starkscan.co/ to search for the contract address.

[IMPORTANT]
====
Remember to select the Sepolia testnet in these block explorers when searching for your deployed contract.
====

Your smart contract is now live on the Starknet network, ready to interact with users and other contracts.

=== Potential deployment errors

When using Starknet SDKs like starknet.js, starknet.py, or starknet-rs, you might encounter the following error during deployment:

[source,bash]
----
Requested ContractAddress(PatriciaKey(<CONTRACT_ADDRESS>)) is unavailable for deployment.
----

This error means that a contract is already deployed at the computed address. The address is a function of the class_hash, deployer_address (if specified), salt, and constructor_calldata_hash.

To resolve this issue, make sure to provide a different salt value. This ensures you're not attempting to override an already deployed contract.