Navigating Starknet: a guide to smart contracts
Starknet, a revolutionary Layer 2 solution for Ethereum, has been making waves in the blockchain community by offering scalability and cost-efficiency for decentralized applications (dApps). One of the most exciting aspects of Starknet is the ability to deploy and interact with smart contracts on its network. In this guide, we’ll walk you through the process of declaring, deploying, and interacting with smart contracts on Starknet.
Prerequisites. Before we dive into the world of Starknet smart contracts, let’s ensure you have the necessary prerequisites in place:
- Starkli and Scarb installation: Confirm that Starkli and Scarb are correctly installed on your system by running the following commands:
starkli --version
scarb --version
If any of these commands fail, don’t worry; you can find solutions in our guide on setting up your environment.
Declaring a smart contract. Deploying a smart contract on Starknet is a two-step process:
- Declaring the Class: This involves sending your contract’s code to the network.
- Deploying the Contract: This means creating an instance of the code you previously declared.
If you need an example smart contract to test with, you can use a pre-prepared example from the Starknet Book.
Compiling a smart contract
Before deploying a smart contract, you need to compile it using the Scarb compiler. Here are the steps:
- Create a directory that contains a
Scarb.toml
file and a subdirectory namedsrc
that contains your contract's source code. - Add the following code to the
Scarb.toml
file:
[package]
name = "contracts"
version = "0.1.0"
[dependencies]
starknet = ">=2.2.0"
[[target.starknet-contract]]
sierra = true
3. Navigate into the newly created directory:
cd <dir_name>
4. Compile your contract with the following command:
scarb build
The compiled contract will be saved in the target/dev/
directory. Your contract is now ready to be deployed.
Setting an RPC provider. To interact with the Starknet network, you need to set an RPC endpoint within Starkli. Several RPC providers are available for Starknet, including providers like Infura or Alchemy, as well as the option to set up your own node. For demonstration purposes, we’ll use the Starknet Sequencer’s Gateway in the following steps.
Declaring a smart contract
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
The --network
flag specifies the network you want to use, which can be, for example, mainnet
. The --compiler-version
flag specifies the version of the compiler you want to use. You can find the supported compiler versions 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 different, which can be checked with:
scarb --version
If Starkli and Scarb are not in sync, you may need to use the compiler version that Starkli is using by installing a previous version of Scarb. You can do this with the following command, which installs Scarb version 0.6.1:
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 \
--rpc=https://starknet-goerli.infura.io/v3/<API_KEY> \
--compiler-version=2.1.0
The result of the declaration command is a contract class hash, which serves as the identifier of the contract class in Starknet. It’s similar to an address and can be viewed using a block explorer like StarkScan.
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.
For example, if the constructor expects an address to assign as the owner:
starkli deploy \
<CLASS_HASH> \
<CONSTRUCTOR_INPUTS> \
--network=goerli-1
With the class hash and constructor inputs, the command might look like this:
starkli deploy \
0x00e68b4b07aeecc72f768b1c086d9b0aadce131a40a1067ffb92d0b480cf325d \
0x02cdAb749380950e7a7c0deFf5ea8eDD716fEb3a2952aDd4E5659655077B8510 \
--network=goerli-1
The expected result after running the command and providing your password is the contract’s deployment information, including the address where the contract is deployed.
Interacting with a smart contract
Now that your smart contract is deployed, you can interact with it using Starkli. There are two primary methods for interaction: call
for read-only functions and invoke
for writing functions that modify the state.
Calling a function. The call
command allows you to query a smart contract function without sending a transaction. For example, to retrieve the address of the current owner:
starkli call \
0x014825acb37c36563d3b96c450afe363d2fdfa3cfbd618b323f95b68b55ebf7e \
get_owner
--network=goerli-1
This will return the address of the owner.
Invoking a function. To modify the state of the smart contract, use the invoke
command, which submits a transaction to the network. For instance, to transfer ownership:
starkli invoke \
0x014825acb37c36563d3b96c450afe363d2fdfa3cfbd618b323f95b68b55ebf7e \
transfer_ownership \
0x011088d3cbe4289bc6750ee3a9cf35e52f4fa4e0ac9f42fb0b62e983139e135a \
--network=goerli-1
After the transaction is accepted on L2, you can confirm the state transition by calling the get_owner
function again.
Conclusion:
Starknet opens up exciting possibilities for decentralized applications, and understanding how to work with smart contracts on its network is a key step for developers and enthusiasts. With the ability to declare, deploy, and interact with smart contracts, you can harness the full potential of Starknet to build scalable and efficient dApps. Happy exploring!