Video summary

Re{solve} Hackathon Workshop: Starknet Smart Contracts

Main summary

Key takeaways

Technology

Summary of "Re{solve} Hackathon Workshop: Starknet Smart Contracts"

This workshop provides a practical introduction to building and testing smart contracts on Starknet using the Cairo programming language and the Starknet Foundry toolset. It is part of a hackathon series organized by the Starknet Foundation.

Key Technological Concepts and Tools Covered

  • Cairo Language for Starknet
    • Cairo is the programming language used to write smart contracts on Starknet.
    • The workshop builds on a previous session covering Cairo basics, now focusing on smart contract development.
  • Starknet Development Environment Setup
    • Use of Starkup: an open-source project that installs all necessary dependencies for Starknet development with a single command.
    • Installation includes:
      • scar (Starknet package manager, similar to Rust’s Cargo)
      • smforge (Starknet Foundry testing suite CLI)
      • sncast (CLI for interacting with Starknet)
      • asdf (version manager to handle project-specific binary versions via a tool-versions file)
  • Project Initialization and Configuration
    • Creating a new smart contract project with smforge new.
    • Defining dependencies in scar.toml, including the Starknet dependency to enable smart contract macros and forge for testing.
    • Use of IDE extensions for Cairo syntax highlighting and TOML support.
  • Smart Contract Structure in Cairo
    • A contract is a module annotated with the #[contract] attribute.
    • Storage is defined as a struct annotated with #[storage].
    • A constructor function initializes storage variables and is annotated with #[constructor].
    • Traits define the public interface of the contract, annotated with #[contract_interface].
    • The trait is implemented for the contract with an implementation block annotated with #[impl_contract] (versioned as v0 for future-proofing).
    • Reading and writing to storage uses traits like StorageRead and StorageWrite.
  • Example: Counter Smart Contract
    • Storage holds a single u32 counter variable.
    • Constructor initializes the counter with a passed initial value.
    • Interface exposes a get_counter() method to read the counter.
    • Implementation reads from storage and returns the counter value.
  • Testing with Starknet Foundry (smforge)
    • Tests are defined as functions annotated with #[test].
    • Common test setup involves declaring the contract and deploying it to a local devnet.
    • Deployment requires serialized constructor arguments passed as an array of felts.
    • Tests verify constructor initialization, increment and decrement functions, and error handling.
  • Increment and Decrement Functions
    • increase_counter increments the stored counter by 1.
    • decrease_counter decrements the counter but includes:
      • A check to prevent underflow (counter cannot go below zero).
      • Ownership restriction: only the contract owner can call decrease_counter.
  • Ownership and Access Control
    • Owner address stored in contract storage.
    • Ownership is checked by comparing the caller address (retrieved via get_caller_address()) with the stored owner.
    • If the caller is not the owner, the contract reverts with an error.
    • Testing ownership uses Foundry’s cheat codes to mock the caller address, allowing tests to simulate calls from owner and non-owner accounts.
  • Error Handling and Assertions
    • Use of assert statements to enforce conditions and revert transactions with error messages.
    • Tests include scenarios that expect panics (reverts) with specific error messages to verify correct error handling.
  • Best Practices and Advanced Notes
    • Emphasis on modular design: separating storage, interface, implementation.
    • Use of traits and generics for flexibility and compile-time safety.
    • Encouragement to reuse audited components (e.g., ownership modules from OpenZeppelin’s Cairo smart contracts) rather than implementing access control manually.
    • Reference to OpenZeppelin components for secure, tested smart contract features.

Summary of Workshop Flow

  • Installation and setup of Starknet development environment using Starkup and asdf.
  • Creating a new Cairo smart contract project with smforge.
  • Writing a simple counter contract with storage, constructor, and getter method.
  • Compiling the contract to verify correctness.
  • Writing and running unit tests for contract initialization and reading storage.
  • Adding increment and decrement functions with proper storage mutation.
  • Implementing ownership logic and access control on decrement function.
  • Using Foundry cheat codes to mock caller addresses in tests to validate access control.

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video