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
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,39 @@ 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.

We can draw a parallel with C++ programming:

* Writing the contract code is similar to defining a class in C++.
* Declaring the contract is analogous to compiling that class into object code.
* Deploying an instance of the contract is like creating an object of that class.

Another parallel with Java:

* Writing the contract is like writing a Java class.
* Declaring is similar to compiling the Java class into bytecode.
* Deploying is like instantiating an object of that class in the JVM.

Just as you can create multiple objects from a single class in C++, you can deploy multiple instances of a declared contract in Starknet.

=== Why Separate Declaration and Deployment?

Separating declaration from deployment offers several advantages:

* 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.

== Compiling a smart contract

You can compile a smart contract using the Scarb compiler.
Expand Down Expand Up @@ -153,9 +181,20 @@ 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 https://testnet.starkscan.co/class/0x00e68b4b07aeecc72f768b1c086d9b0aadce131a40a1067ffb92d0b480cf325d[StarkScan] to see the contract class hash in the blockchain.

Using Starkli: You can use the following command to retrieve the contract class by its hash:

[source,bash]
----
starkli class-by-hash <CLASS_HASH> --network=sepolia
----

If the contract is successfully declared, this command will return the contract class details.

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

[source,bash]
----
Not declaring class as its already declared. Class hash: <CLASS_HASH>
----

In this case, you can still use the methods above to verify the existing declaration.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ 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[]

Deployment is the process of creating a live, functional instance of your declared smart contract on the Starknet network. It's like bringing a blueprint to life by constructing an actual building.

Let's use a factory production analogy:

* Declaration is like creating a product blueprint and registering it with the factory.
* Deployment is like using that blueprint to manufacture an actual product on the assembly line.
* Each deployed contract is a unique product instance, even though they're all based on the same blueprint.


== 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 +55,8 @@ starkli deploy \
--network=sepolia
----

Think of <CLASS_HASH> as the product code in our factory analogy, and <CONSTRUCTOR_INPUTS> as the specific settings for this product instance.

== Expected result

After running the command and adding your password, you will see an output similar to this:
Expand All @@ -58,3 +70,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.