Deploy and verify a smart contract on StarkNet

DavideDavide
4 min read

In this article, we'll see how you can deploy a smart contract using the ProtoStar framework and how to verify it.

On StarkNet, a smart contract can be verified programmatically directly from the terminal using StarkScan; the StarkNet contract verifier.

Deploy a smart contract with Protostar

Protostar is a framework you can use to write, test, and deploy a smart contract.

To try verifying a contract using StarkScan, let's first deploy a contract using Protostar. So, initialize a new Protostar project to get a sample contract.

protostar init

Follow the instructions on the terminal, which will create a project folder with the sample project in it.

Inside the src directory, you will find a smart contract written in Cairo as an example, and we can deploy this one and verify it.

The contract is named main.cairo.

Cairo smart contract example

This smart contract example has a constructor function, a function to add a number to the current saved balance, and a function to display the current saved number.

%lang starknet
from starkware.cairo.common.math import assert_nn
from starkware.cairo.common.cairo_builtins import HashBuiltin

@storage_var
func balance() -> (res: felt) {
}

@external
func increase_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
    amount: felt
) {
    with_attr error_message("Amount must be positive. Got: {amount}.") {
        assert_nn(amount);
    }

    let (res) = balance.read();
    balance.write(res + amount);
    return ();
}

@view
func get_balance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (res: felt) {
    let (res) = balance.read();
    return (res,);
}

@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
    balance.write(0);
    return ();
}

Deploy on StarkNet testnet with Protostar

Once you have a contract in the src folder, we can compile the smart contract and deploy it.

Note that in this case, we are using the protostar deploy command and the alpha-goerli testnet. Both of these will probably be removed in future releases.

Let's first compile the smart contract.

protostar build

You should recevie a similar response.

11:17:58 [INFO] Built the project successfully                                  
11:17:58 [INFO] Execution time: 2.04 s

You can find the compiled files inside a build folder now appearing in the main project's directory.

Deploy the contract on the StarkNet testnet

Now that we compiled the smart contract, we can use the protostar deploy command to deploy it on the testnet.

protostar deploy ./build/main.json --network alpha-goerli

We use the --network flag to specify the network.

At this stage of development, you will get a few warnings because, in the future, these things will change.

The result will be like this.

11:18:45 [WARNING] `protostar deploy` will be removed in the future release                                                                                                
https://docs.starknet.io/docs/Blocks/transactions/#deploy-transaction
11:18:45 [WARNING] alpha-goerli is a legacy network name parameter and it won't be supported in future versions
11:18:45 [WARNING] alpha-goerli is a legacy network name parameter and it won't be supported in future versions
11:18:46 [INFO] Deploy transaction was sent.

Contract address: 0x0484f03a325c463917f807bfe275ef09e7d65f5fce8e9ea54dc9f379e50f317b
Transaction hash: 0x01bacfb4188da7ba7a445df6dd4b5a70f2fc61d18c3945529fc4021fa00b5140

https://goerli.voyager.online/contract/0x0484f03a325c463917f807bfe275ef09e7d65f5fce8e9ea54dc9f379e50f317b
11:18:46 [INFO] Execution time: 2.58 s

Now we can see the tx hash, the new contract address, and a link to Voyager, the StarkNet explorer.

Congratulations! You just deployed a smart contract written in Cairo on the StarkNet testnet!

Verify a smart contract on StarkNet

Now that we deployed a smart contract, we can verify it. The cool thing is that everything is done programmatically from the terminal, using StarkScan.

Since we used Protostar to deploy the contract, we first have to run this command.

protostar install

The result will be.

12:30:15 [INFO] Executing install                                                                                     
12:30:15 [INFO] Installed successfully
12:30:15 [INFO] Execution time: 1.96 s

Now we can run the verify command; if StarkScan is not yet installed in your system, this command will prompt the installation.

Run.

npx starkscan

The result will be.

Need to install the following packages:
  starkscan@0.0.3
Ok to proceed? (y) y

๐Ÿ‘‹ Hello, Starknet explorer. Welcome to the Starkscan Contract Verifier โœจ

โ€ผ๏ธ  BEFORE YOU START:
๐Ÿ Python users, please activate your virtual environment.
๐ŸŒŸ Protostar users, please run protostar install.

? Main file to be verified โ˜‘๏ธ  src/main.cairo

โœ” ๐ŸŒŠ Nile environment found!
โ„น Searching in Nile cairo path /home/soos/Documents/Coding/python/starknet_cairo/lib/python3.9/site-packages,contracts

โœ” ๐Ÿ“„ ./src/main.cairo file found!

โœ” All files found

? Please enter the deployed Contract Address or Class Hash:  0x0484f03a325c463917f807bfe275ef09e7d65f5fce8e9ea54dc9f379e50f317b

โœ” Found contract address on Testnet, which implements class hash 0x07100822455c20f8ae5ff6745ebd91726a60b691964b191f862760e74f11c7a4

? Select networks to verify Testnet

? Compiler version: 0.10.0

? Is this an Account contract? No

? Contract Name: main

โœ” main verified on testnet: https://testnet.starkscan.co/class/0x07100822455c20f8ae5ff6745ebd91726a60b691964b191f862760e74f11c7a4

โœจ All done! Thanks for using the Starkscan Contract Verifier.

You just need to answer a few questions, like the contract's name, compiler version, and if this is a wallet contract or not.

Congrats on getting to the end of this! Learning something new can be difficult, especially something new like StarKnet, but you deployed and verified a smart contract today!

0
Subscribe to my newsletter

Read articles from Davide directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Davide
Davide

Hey there! I'm Davide, a passionate developer advocate at Chainstack, the leading suite of blockchain infrastructure services. I'm dedicated to empowering current and aspiring web3 developers by sharing valuable content and resources. Within my articles, you'll discover a treasure trove of straightforward projects designed to bolster your understanding of fundamental Python, Solidity, JavaScript, and web3 skills. Whether you're a seasoned developer or just starting out, these projects offer an ideal learning path for honing your abilities in the exciting world of blockchain development. Feel free to explore my articles, and let's embark on this remarkable journey of mastering web3 together!