Home

Awesome

Starknet's Counter Workshop

In this workshop, you will learn how to create a simple Starknet smart contract, implement public functions, and events, access external contracts, and use OpenZeppelin's Ownable contract.

After completing each step, run the associated script to verify it has been implemented correctly.

Use the Cairo book and the Starknet docs as a reference.

Setup

Clone this repository and choose whether you prefer using Docker to manage global dependencies or not in the following steps:

Option 1: Without Docker

  1. Install asdf (instructions)
  2. Install Scarb 2.8.0 via asdf (instructions)
  3. Install Starknet Foundry 0.27.0 via asdf (instructions)
  4. Install the Cairo 1.0 extension for VSCode (marketplace)

Option 2: With Docker

  1. Make sure Docker is installed and running
  2. Install the Dev Containers extension for VSCode (marketplace)
  3. Launch an instance of VSCode inside of the container by going to View -> Command Palette -> Dev Containers: Rebuild and Reopen in Container

Note: All the commands shown from this point on will assume that you are using the integrated terminal of a VSCode instance running inside the container. If you want to run the tests on a different terminal you'll need to use the command docker compose run test.

Step 1

Switch to the step1 branch to enable the verification tests:

git checkout -b step1 origin/step1

Goal

Initialize the project structure within the cloned repository by using the Scarb package manager and enable compilation of Starknet Contracts.

Requirements

Note: Using any other name will disrupt upcoming steps.

Verification

When completed, build your project by running the following command:

scarb build

Hints

Step 2

Switch to the step2 branch to enable the verification tests:

git checkout -b step2 origin/step2

Goal

Add snforge as a dependency within your Scarb.toml file to allow execution of tests with Starknet Foundry.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 3

Switch to the step3 branch to enable the verification tests:

git checkout -b step3 origin/step3

Goal

Implement the constructor function to initialize an input number and store a variable named counter within the contract.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 4

Switch to the step4 branch to enable the verification tests:

git checkout -b step4 origin/step4

Goal

Implement an interface for the contract which contains the get_counter() function. This function should return the value of the stored counter variable within the contract.

Requirements

Note: Any other given name to the contract's interface would break the test, be sure to have to correct name!

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 5

Switch to the step5 branch to enable the verification tests:

git checkout -b step5 origin/step5

Goal

Within the same interface created in the previous step, implement a function called increase_counter() that can increment the current value of the counter by 1 each time it is invoked.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 6

Switch to the step6 branch to enable the verification tests:

git checkout -b step6 origin/step6

Goal

Implement an event named CounterIncreased that emits the current value of the counter variable, every time the value is increased.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints


Note: CHECKPOINT Reached ⛳️! Switch to the step15-js branch to get a deployment script based on starknet.js.

git checkout -b step15-js origin/step15-js

Step 7

Switch to the step7 branch to enable the verification tests:

git checkout -b step7 origin/step7

Goal

In this step, we will introduce an external smart contract that acts as a kill switch for a specific function. Your task is to add the external KillSwitch contract as a dependency within your project.

Note: The KillSwitch contract can be found here.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 8

Switch to the step8 branch to enable the verification tests:

git checkout -b step8 origin/step8

Goal

Initialize the KillSwitch contract by storing the contract's address given as an input variable in the constructor function.

Requirements

  1. Store a variable named kill_switch as type ContractAddress.
  2. Update the constructor function to initialize the kill_switch variable.

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 9

Switch to the step9 branch to enable the verification tests:

git checkout -b step9 origin/step9

Goal

Implement the KillSwitch mechanism in the increase_counter() by calling the is_active() function from the KillSwitch contract.

Requirements

Note: Analyze the KillSwitch code to understand the interface and the contract structure from here.

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Note: If you want to deploy the Counter contract, you can use the following deployed KillSwitch contract address.

Sepolia

Contract Address: 0x05f7151ea24624e12dde7e1307f9048073196644aa54d74a9c579a257214b542

Step 10

Switch to the step10 branch to enable the verification tests:

git checkout -b step10 origin/step10

Goal

Protect the increase_counter() function by reverting the transaction if KillSwitch mechanism is enabled.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 11

Switch to the step11 branch to enable the verification tests:

git checkout -b step11 origin/step11

Goal

Add the external OpenZeppelin contracts as a dependency within your project.

Note: The OpenZeppelin contracts can be found here.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

$ scarb test

Hints

Step 12

Switch to the step12 branch to enable the verification tests:

git checkout -b step12 origin/step12

Goal

Initialize the Ownable component from the OpenZeppelin contracts.

Before working on this step, make sure to read Chapter 16.2: Composability and Components and see how Components work.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 13

Switch to the step13 branch to enable the verification tests:

git checkout -b step13 origin/step13

Goal

Modify the constructor function to call the initializer() function within the Ownable component to initialize the owner.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hint

Step 14

Switch to the step14 branch to enable the verification tests :

git checkout -b step14 origin/step14

Goal

Protect the increase_counter() function so that only the owner of the contract can call this.

Requirements

Verification

When completed, execute the test suite to verify you've met all the requirements for this section.

scarb test

Hints

Step 15

Switch to the step15-js branch to get a deployment script based on starknet.js.

git checkout -b step15-js origin/step15-js

Goal

To deploy your account contract to Starknet's testnet using the deploy.ts script found in the scripts folder.

Dependencies

Run the command below from the project's root folder to install the deployment script dependencies.

npm install

Deployer Wallet

Create a wallet that the script can use to pay for the declaration of your account contract.

Steps

  1. Create a wallet on Starknet testnet using the Argent X or Braavos browser extension.
  2. Fund the wallet by using the Faucet or the Bridge.
  3. Create a file in the project's root folder called .env
  4. Export the private key of the funded wallet and paste it into the .env file using the key DEPLOYER_PRIVATE_KEY.
DEPLOYER_PRIVATE_KEY=<YOUR_FUNDED_TESTNET_WALLET_PRIVATE_KEY>
  1. Export the public key of the funded wallet and paste it into the .env file using the key DEPLOYER_ADDRESS
DEPLOYER_ADDRESS=<YOUR_FUNDED_TESTNET_WALLET_PUBLIC_ADDRESS>

RPC Endpoint

To successfully deploy the contract with the script on the Starknet Testnet, you will need to provide an RPC URL. For our workshop, we will use Blast's Public RPC Endpoint.

Add the following line in your .env file:

RPC_ENDPOINT=https://starknet-sepolia.public.blastapi.io/

Refer to Blast to learn more about their Starknet RPC Endpoints.

Run the Script

Run the script that will declare and deploy your smart contract on the Starknet Testnet and ensure that you adjust the constructor inputs in your deploy.ts file appropriately.

Note: If you are deploying the smart contract from the CHECKPOINT, in the deploy.ts file, you will only need the initial_counter in the constructor variable. Ensure that you remove or comment out the kill_switch_address and initial_onwer variables.

Additionally, ensure that the variable names in the deploy.ts constructor are the same as in the counter.cairo constructor function.

const constructor = myCallData.compile("constructor", {
  initial_counter: 100,
  address: "0x05f7151ea24624e12dde7e1307f9048073196644aa54d74a9c579a257214b542",
  initial_owner: process.env.DEPLOYER_ADDRESS,
});

Steps

  1. From the project's root folder run npm run deploy
  2. Follow the instructions from the terminal

If the script finishes successfully your smart contract is ready to be used on Starknet testnet. Congratulations!