### Example Output for Starknet Devnet Version Check Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This snippet provides an example of the expected output when verifying the installation of Starknet Devnet. It shows the version number of the installed Devnet, confirming a successful setup. ```terminal starknet-devnet 0.4.1 ``` -------------------------------- ### Example Output for Scarb and Starknet Foundry Version Check Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This snippet provides an example of the expected output when verifying the installation of Scarb and Starknet Foundry. It shows the version numbers for Scarb, Cairo, Sierra, snforge, and sncast, indicating a successful setup. ```terminal scarb 2.11.4 (c0ef5ec6a 2025-04-09) cairo: 2.11.4 (https://crates.io/crates/cairo-lang-compiler/2.11.4) sierra: 1.7.0 snforge 0.42.0 sncast 0.42.0 ``` -------------------------------- ### Updating System and Installing Prerequisites on Ubuntu Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command updates the package lists for upgrades and new package installations, then installs essential tools like `curl`, `git`, and `build-essential` which are commonly required for compiling software from source and managing repositories on Ubuntu. ```terminal sudo apt update sudo apt install -y curl git build-essential ``` -------------------------------- ### Installing Scarb and Starknet Foundry on MacOS/Linux Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command uses the Starkup installer script to set up Scarb and Starknet Foundry on MacOS and Linux systems. It downloads and executes the installation script directly from the web. ```terminal curl --proto '=https' --tlsv1.2 -sSf https://sh.starkup.sh | sh ``` -------------------------------- ### Installing Latest Starknet Devnet with asdf Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command installs the latest stable version of Starknet Devnet using the 'asdf' version manager. It fetches and sets up the Devnet binaries, making them available for use. ```terminal asdf install starknet-devnet latest ``` -------------------------------- ### Installing Specific Scarb Version with asdf - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command uses `asdf`, a version manager, to install a specific version of Scarb. It's crucial for managing multiple Scarb versions and ensuring project compatibility when different compiler versions are required. ```terminal asdf install scarb ``` -------------------------------- ### Installing Homebrew via Script Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command executes the Homebrew installation script directly from GitHub, which downloads and sets up Homebrew, a package manager for macOS and Linux, in the user's system. It uses `curl` to fetch the script and `bash` to execute it. ```terminal /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` -------------------------------- ### Configuring Scarb to Allow Prebuilt Plugins - Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This `Scarb.toml` configuration snippet allows Scarb to use prebuilt plugins like `snforge_std`, potentially eliminating the need for a Rust installation for certain Starknet projects. It's relevant for Scarb versions 2.10+ and Starknet Foundry 0.37.0+. ```cairo [tool.scarb] allow-prebuilt-plugins = ["snforge_std"] ``` -------------------------------- ### Adding Starkli Environment Variables to Shell Profile - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command sources the Starkli environment file, making Starkli's binaries and configurations available in the current shell session. It should be added to the user's shell configuration file (`.zshrc` or `.bashrc`) for persistent setup. ```terminal . /Users//.starkli/env ``` -------------------------------- ### Initializing Project Environment with Yarn (Bash) Source: https://github.com/starknet-io/starknet-docs/blob/main/README.adoc This command navigates into the `starknet-docs` directory and then executes `yarn` to install project dependencies, preparing the local development environment for building the documentation. ```Bash cd starknet-docs && yarn ``` -------------------------------- ### Verifying Starknet Devnet Installation Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command checks if Starknet Devnet is correctly installed by querying its version. A successful output confirms that the local Starknet node is set up and ready for use. ```terminal starknet-devnet --version ``` -------------------------------- ### Installing Starknet Foundry with asdf Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc These commands add the Starknet Foundry plugin to `asdf`, install its latest version, and set it as the global default. This provides essential tools like `snforge` for testing and `sncast` for interacting with Starknet contracts. ```terminal asdf plugin add starknet-foundry asdf install starknet-foundry latest asdf set -u starknet-foundry latest ``` -------------------------------- ### Verifying asdf Installation Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command checks the installed version of `asdf`, confirming its successful installation and availability in the shell environment. This verification is necessary before proceeding to add and install plugins for specific tools. ```terminal asdf --version ``` -------------------------------- ### Verifying Homebrew Installation Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command checks the installed version of Homebrew, confirming that the installation was successful and that Homebrew is correctly configured and accessible in the current shell environment. ```terminal brew --version ``` -------------------------------- ### Verifying Starknet Tools Installation Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc These commands verify the successful installation of Scarb, Starknet Foundry (via `snforge` and `sncast`), and Starknet Devnet by checking their respective versions. This ensures all core Starknet development tools are correctly set up and accessible in the terminal. ```terminal scarb --version snforge --version && sncast --version starknet-devnet --version ``` -------------------------------- ### Checking Current Scarb Version - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command outputs the currently installed version of Scarb, the build toolchain for Cairo and Starknet projects. It's essential for troubleshooting compatibility issues with different compiler versions or project requirements. ```terminal scarb --version ``` -------------------------------- ### Installing Scarb with asdf Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc These commands add the Scarb plugin to `asdf`, then install the latest stable version of Scarb, and finally set it as the global default version. This enables the use of Scarb for compiling Cairo smart contracts. ```terminal asdf plugin add scarb asdf install scarb latest asdf set -u scarb latest ``` -------------------------------- ### Installing Starknet Devnet with asdf Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc These commands add the Starknet Devnet plugin to `asdf`, install its latest version, and set it as the global default. This enables the use of a local Starknet development network for testing and deploying contracts in an isolated environment. ```terminal asdf plugin add starknet-devnet asdf install starknet-devnet latest asdf set -u starknet-devnet latest ``` -------------------------------- ### Verifying asdf Version Manager Installation Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command checks if the 'asdf' version manager is installed on the system. 'asdf' is recommended for managing different versions of development tools like Starknet Devnet. ```terminal asdf --version ``` -------------------------------- ### Installing WSL and Ubuntu on Windows Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command installs Windows Subsystem for Linux (WSL) along with the default Ubuntu distribution on a Windows machine. It's the primary step for setting up a Linux environment on Windows to run Starknet tools. ```terminal wsl --install ``` -------------------------------- ### Serving Local Documentation with http-server (Bash) Source: https://github.com/starknet-io/starknet-docs/blob/main/README.adoc This command starts a local HTTP server using `npx http-server`, serving the generated documentation from the `public_html` directory. The `-c-1` flag disables caching, ensuring fresh content on every request. ```Bash npx http-server public_html -c-1 ``` -------------------------------- ### Installing asdf with Homebrew Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command uses Homebrew to install `asdf`, a version manager that allows managing multiple runtime versions of various programming languages and tools. This is crucial for easily switching between different versions of Scarb, Starknet Foundry, and Starknet Devnet. ```terminal brew install asdf ``` -------------------------------- ### Verifying Scarb and Starknet Foundry Installation Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc These commands check if Scarb and Starknet Foundry (including sncast) are correctly installed by querying their versions. A successful output confirms that the tools are accessible in the system's PATH. ```terminal scarb --version snforge --version && sncast --version ``` -------------------------------- ### Installing Dependencies on Ubuntu for Juno Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/juno.adoc This snippet installs required dependencies for Juno on Ubuntu using `apt-get`. It includes `libjemalloc-dev`, `libjemalloc2`, `pkg-config`, and `libbz2-dev`, followed by `make install-deps` for project-specific dependencies. ```terminal sudo apt-get install -y libjemalloc-dev libjemalloc2 pkg-config libbz2-dev make install-deps ``` -------------------------------- ### Setting Local Scarb Version with asdf - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command uses `asdf` to set a specific Scarb version as the local default for the current project directory. This ensures that `scarb` commands executed within that directory use the specified version, preventing conflicts with global installations. ```terminal asdf set scarb ``` -------------------------------- ### Displaying Starkli Declare Help and Compiler Versions - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command displays the help documentation for the `starkli declare` command, including a list of supported compiler versions that can be specified using the `--compiler-version` flag. It helps users identify compatible compiler versions for their contracts. ```terminal starkli declare --help ``` -------------------------------- ### Comprehensive Starknet Contract Function Example - Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/starknet-by-example/visibility-mutability.adoc This example demonstrates the implementation of external and internal functions in a Starknet Cairo contract. It showcases state-modifying (`set`) and view (`get`) external functions, an internal view function (`_read_value`), and a pure helper function (`increment`), illustrating how to define interfaces, implement them, and manage contract state access. ```Cairo // This trait defines the public interface of our contract // All functions declared here will be accessible externally #[starknet::interface] trait ContractInterface { fn set(ref self: TContractState, value: u32); fn get(self: @TContractState) -> u32; } #[starknet::contract] mod Contract { use starknet::storage::{StoragePointerReadAccess, StoragePointerWriteAccess}; use super::ContractInterface; #[storage] pub struct Storage { pub value: u32, } // External Functions Implementation // The `#[abi(embed_v0)]` attribute makes these functions callable from outside the contract // This is where we implement our public interface defined in ContractInterface #[abi(embed_v0)] pub impl ContractImpl of ContractInterface { // External function that can modify state // - Takes `ref self` to allow state modifications // - Calls internal `increment` function to demonstrate internal function usage fn set(ref self: ContractState, value: u32) { self.value.write(increment(value)); } // External view function (cannot modify state) // - Takes `@self` (snapshot) to prevent state modifications // - Demonstrates calling an internal function (_read_value) fn get(self: @ContractState) -> u32 { self._read_value() } } // Internal Functions Implementation // These functions can only be called from within the contract // The #[generate_trait] attribute creates a trait for these internal functions #[generate_trait] pub impl Internal of InternalTrait { // Internal view function // - Takes `@self` as it only needs to read state // - Can only be called by other functions within the contract fn _read_value(self: @ContractState) -> u32 { self.value.read() } } // Pure Internal Function // - Doesn't access contract state // - Defined directly in the contract body // - Considered good practice to keep pure functions outside impl blocks // It's also possible to use ContractState here, but it's not recommended // as it'll require to pass the state as a parameter pub fn increment(value: u32) -> u32 { value + 1 } } ``` -------------------------------- ### Creating Starkli Keystore from Private Key - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command generates an encrypted keystore file (`keystore.json`) from a provided private key, which is then used for signing transactions. It prompts the user to enter the private key and a password for encryption, enhancing security for account management. ```terminal starkli signer keystore from-key keystore.json ``` -------------------------------- ### Installing Dependencies on macOS for Juno Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/juno.adoc This snippet installs necessary dependencies for Juno on macOS using Homebrew. It includes `jemalloc` for memory allocation, `pkg-config` for managing compilation flags, and `make install-deps` for additional project-specific dependencies. ```terminal brew install jemalloc brew install pkg-config make install-deps ``` -------------------------------- ### Fetching Sepolia Account Details with Starkli - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command fetches the details of a predeployed Starknet smart wallet account on the Sepolia network and saves them to `account.json`. It requires the smart wallet's address and specifies the network, enabling local interaction with the deployed account. ```terminal starkli account fetch \ \ --output account.json \ --network=sepolia ``` -------------------------------- ### Example Cairo v2.0.0 Counter Contract Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/contract-abi.adoc This comprehensive Cairo v2.0.0 contract example demonstrates the structure of a Starknet contract, including interface definitions (`#[starknet::interface]`), storage (`#[storage]`), event definitions (`#[event]`), a constructor (`#[constructor]`), and an external implementation block (`#[external(v0)]`). It serves as a foundational example for understanding ABI generation in Cairo v2.0.0. ```Cairo #[starknet::interface] trait IOtherContract { fn decrease_allowed(self: @TContractState) -> bool; } #[starknet::interface] trait ICounterContract { fn increase_counter(ref self: TContractState, amount: u128); fn decrease_counter(ref self: TContractState, amount: u128); fn get_counter(self: @TContractState) -> u128; } #[starknet::contract] mod counter_contract { use starknet::ContractAddress; use super::{ IOtherContractDispatcher, IOtherContractDispatcherTrait, IOtherContractLibraryDispatcher }; #[storage] struct Storage { counter: u128, other_contract: IOtherContractDispatcher } #[event] #[derive(Drop, starknet::Event)] enum Event { CounterIncreased: CounterIncreased, CounterDecreased: CounterDecreased } #[derive(Drop, starknet::Event)] struct CounterIncreased { amount: u128 } #[derive(Drop, starknet::Event)] struct CounterDecreased { amount: u128 } #[constructor] fn constructor( ref self: ContractState, initial_counter: u128, other_contract_addr: ContractAddress ) { self.counter.write(initial_counter); self .other_contract .write(IOtherContractDispatcher { contract_address: other_contract_addr }); } #[external(v0)] impl CounterContract of super::ICounterContract { fn get_counter(self: @ContractState) -> u128 { self.counter.read() } fn increase_counter(ref self: ContractState, amount: u128) { let current = self.counter.read(); self.counter.write(current + amount); self.emit(CounterIncreased { amount }); } fn decrease_counter(ref self: ContractState, amount: u128) { let allowed = self.other_contract.read().decrease_allowed(); if allowed { let current = self.counter.read(); self.counter.write(current - amount); self.emit(CounterDecreased { amount }); } } } } ``` -------------------------------- ### Adding Starknet Devnet Plugin to asdf Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command adds the Starknet Devnet plugin to the 'asdf' version manager. This step is necessary to enable 'asdf' to manage and install different versions of Starknet Devnet. ```terminal asdf plugin add starknet-devnet ``` -------------------------------- ### Starting Juno Block Attestation Service - Bash Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/block-attestation.adoc This command initiates the Juno block attestation service using the compiled validator tool and the specified `config.json` file. The `--log-level debug` flag enables detailed logging, which is useful for monitoring and troubleshooting the service's operation. ```bash ./build/validator --config config.json --log-level debug ``` -------------------------------- ### Initializing Starknet Devnet Instance (Terminal) Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/devnet.adoc Initializes a local Starknet development network using Starknet Devnet. The `--seed=0` option ensures consistent addresses for predeployed accounts, which is useful for reproducible development and testing. This command starts the local blockchain environment. ```terminal starknet-devnet --seed=0 ``` -------------------------------- ### Verifying Initial Block Attestor Service Status - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/block-attestation.adoc This snippet displays the expected output when the block attestation service successfully starts. It provides crucial initial information such as the staker and operational addresses, current stake, epoch details (ID, start, length), and the attestation window, confirming the service's readiness. ```terminal Current attestation info staker_address=0x48f8ddc0bc864f33d4c47b79a1f0e1460e0777d0b0224d8c291f1039523306e operational_address=0x48f8ddc0bc864f33d4c47b79a1f0e1460e0777d0b0224d8c291f1039523306e stake=100000000000000000000 epoch_id=1201 epoch_start=712773 epoch_length=40 attestation_window=16 ``` -------------------------------- ### Manually Enabling WSL Features on Windows Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc These commands manually enable the necessary Windows features for WSL if the 'wsl --install' command fails. They activate the Windows Subsystem for Linux and Virtual Machine Platform, which are prerequisites for WSL. ```terminal dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart ``` -------------------------------- ### Example of Emitting an Event - Starknet Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc Illustrates how to use `emit_event_syscall` to emit an event with specific keys and data. The example shows creating `array!` literals for keys (strings 'key', 'deposit') and values (integers 1, 2, 3), then calling the syscall and unwrapping the result. ```Cairo let keys = array!['key', 'deposit']; let values = array![1, 2, 3]; emit_event_syscall(keys, values).unwrap_syscall(); ``` -------------------------------- ### Running the Juno Starknet Node Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/juno.adoc This command starts the Juno Starknet node, configuring it to expose HTTP and WebSocket RPC endpoints. It specifies the database path for the Sepolia snapshot and requires an L1 Ethereum WSS RPC endpoint (`WSS_HOST`) to sync with the Ethereum state, as Starknet is an L2 solution. ```terminal ./build/juno \ --http \ --http-host 0.0.0.0 \ --http-port 6060 \ --ws \ --ws-port 6061 \ --db-path snap/juno_sepolia_v0.12.5_484680 \ --eth-node WSS_HOST \ --network sepolia ``` -------------------------------- ### Adding secp256r1 Points in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This example demonstrates the initial steps for adding two points on the secp256r1 curve. It shows how to define coordinates and create a `Secp256r1Point` using `secp256r1_new_syscall`, preparing the inputs for the addition operation. Note: The example is incomplete as it only creates the first point. ```Cairo use starknet::secp256r1::Secp256r1Point; // Create two points to add (using generator point) let x = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296_u256; let y = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5_u256; // Create first point let point1 = secp256r1_new_syscall(x, y).unwrap_syscall().expect('Invalid point1'); ``` -------------------------------- ### Creating secp256r1 Point in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This example demonstrates how to create a new point on the secp256r1 curve using `secp256r1_new_syscall`. It initializes a point with known valid coordinates and shows how to handle the `Option` return type to check for point validity, including a commented example of retrieving coordinates. ```Cairo use starknet::secp256r1::Secp256r1Point; // Known valid point coordinates on secp256r1 curve (generator point) let x = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296_u256; let y = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5_u256; // Create a new point using the recommended function let point = secp256r1_new_syscall(x, y).unwrap_syscall(); match point { Option::Some(p) => { // Point is valid and can be used for further operations // For example, you could get its coordinates back: let (px, py) = secp256r1_get_xy_syscall(p).unwrap_syscall(); }, Option::None => { // Point was not on the curve } } ``` -------------------------------- ### Computing Keccak-256 Hash with Cairo Library Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This example demonstrates how to compute a Keccak-256 hash using the recommended `keccak_u256s_le_inputs` function from the `keccak.cairo` library. It shows how to prepare input data as an array of `u256` values and then pass it to the hashing function. ```Cairo use array::ArrayTrait; use keccak::keccak_u256s_le_inputs; // Create input data let mut data = ArrayTrait::new(); data.append(1_u256); data.append(2_u256); // Compute Keccak hash using the recommended function let hash = keccak_u256s_le_inputs(data.span()); ``` -------------------------------- ### Sourcing Zsh Configuration File - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command reloads the `.zshrc` configuration file, applying any changes made to the shell environment without needing to fully restart the terminal. It's used to activate newly added environment variables or aliases. ```terminal source ~/.zshrc ``` -------------------------------- ### Example Starknet Contract ABI (Cairo v1 JSON) Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/contract-abi.adoc This JSON snippet illustrates an example of a Starknet contract's Application Binary Interface (ABI) for Cairo v1. It outlines various functions like `constructor`, `get_name`, `get_symbol`, `get_decimals`, `get_total_supply`, `balance_of`, and `allowance`, detailing their inputs, outputs, and state mutability, typically for an ERC-20 like contract. ```json [ { "type": "function", "name": "constructor", "inputs": [ { "name": "name_", "type": "core::felt252" }, { "name": "symbol_", "type": "core::felt252" }, { "name": "decimals_", "type": "core::integer::u8" }, { "name": "initial_supply", "type": "core::integer::u256" }, { "name": "recipient", "type": "core::starknet::contract_address::ContractAddress" } ], "outputs": [], "state_mutability": "external" }, { "type": "function", "name": "get_name", "inputs": [], "outputs": [ { "type": "core::felt252" } ], "state_mutability": "view" }, { "type": "function", "name": "get_symbol", "inputs": [], "outputs": [ { "type": "core::felt252" } ], "state_mutability": "view" }, { "type": "function", "name": "get_decimals", "inputs": [], "outputs": [ { "type": "core::integer::u8" } ], "state_mutability": "view" }, { "type": "function", "name": "get_total_supply", "inputs": [], "outputs": [ { "type": "core::integer::u256" } ], "state_mutability": "view" }, { "type": "function", "name": "balance_of", "inputs": [ { "name": "account", "type": "core::starknet::contract_address::ContractAddress" } ], "outputs": [ { "type": "core::integer::u256" } ], "state_mutability": "view" }, { "type": "function", "name": "allowance", "inputs": [ { "name": "owner", "type": "core::starknet::contract_address::ContractAddress" }, { "name": "spender", "type": "core::starknet::contract_address::ContractAddress" } ], "outputs": [ { "type": "core::integer::u256" } ] } ] ``` -------------------------------- ### Sourcing Bash Configuration File - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command reloads the `.bashrc` configuration file, applying any changes made to the shell environment without needing to fully restart the terminal. It's used to activate newly added environment variables or aliases. ```terminal source ~/.bashrc ``` -------------------------------- ### Demonstrating Integer Types and Division in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/cairo-101/integers.adoc This Cairo example illustrates the declaration of unsigned 64-bit integers (`u64`) using both regular and suffix annotations. It demonstrates integer division, showing how results are truncated down to the nearest integer, with assertions verifying the expected outcomes. ```cairo fn main() { let a: u64 = 56; // Regular annotation let b = 32_u64; // Suffix annotation assert!(a / b == 1); assert!(b / a == 0); } ``` -------------------------------- ### Setting Global Starknet Devnet Version with asdf Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/environment-setup.adoc This command sets the globally default version for Starknet Devnet using 'asdf'. This ensures that the installed Devnet version is accessible from any terminal session without specifying the version explicitly. ```terminal asdf set -u starknet-devnet latest ``` -------------------------------- ### Testing a Starknet Component with a Mock Contract Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/starknet-by-example/components.adoc This Cairo test snippet outlines the setup for testing the `Switchable` component within the context of a `SwitchContract` mock. It imports necessary testing utilities from `snforge_std` and defines a `deploy` function. This function declares the `SwitchContract` to obtain its class, preparing it for deployment and subsequent testing of the embedded component's functionality. ```Cairo #[cfg(test)] mod test { use super::{ISwitchableDispatcher, ISwitchableDispatcherTrait}; use snforge_std::{ ContractClassTrait, DeclareResultTrait, declare, spy_events, EventSpyAssertionsTrait, }; use super::SwitchContract; // Used as a mock contract use super::switchable_component; fn deploy() -> ISwitchableDispatcher { let contract = declare("SwitchContract").unwrap().contract_class(); ``` -------------------------------- ### Detecting Current Shell (zsh/bash) - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/troubleshooting.adoc This command prints the name of the current shell being used, typically `zsh` or `bash`, which is useful for determining where to add environment variables. It helps identify the correct shell configuration file (e.g., `~/.zshrc` or `~/.bashrc`). ```terminal echo $SHELL ``` -------------------------------- ### Getting secp256r1 Point Coordinates in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This example demonstrates how to use `secp256r1_get_xy_syscall` to retrieve the x and y coordinates of a `Secp256r1Point`. It first creates a point and then asserts that the extracted coordinates match the original values used for point creation. ```Cairo use starknet::secp256r1::Secp256r1Point; // Create a point to extract coordinates from (using generator point) let x = 0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296_u256; let y = 0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5_u256; // Create the point let point = secp256r1_new_syscall(x, y).unwrap_syscall().expect('Invalid point'); // Get the coordinates let (point_x, point_y) = secp256r1_get_xy_syscall(point).unwrap_syscall(); // Verify the coordinates match the original values assert(point_x == x, 'x coordinate mismatch'); assert(point_y == y, 'y coordinate mismatch'); ``` -------------------------------- ### Implementing Basic Map Operations in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/starknet-by-example/mappings.adoc This snippet demonstrates how to declare and use a `Map` within a Cairo smart contract's storage. It defines an interface `IMapContract` with `set` and `get` functions, and implements them using `self.map.write` and `self.map.read` respectively. It requires importing `Map`, `StorageMapReadAccess`, and `StorageMapWriteAccess` traits. ```Cairo use starknet::ContractAddress; #[starknet::interface] trait IMapContract { fn set(ref self: TContractState, key: ContractAddress, value: felt252); fn get(self: @TContractState, key: ContractAddress) -> felt252; } #[starknet::contract] mod MapContract { use super::IMapContract; use starknet::ContractAddress; use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess}; #[storage] struct Storage { map: Map, } #[abi(embed_v0)] impl MapContractImpl of IMapContract { fn set(ref self: ContractState, key: ContractAddress, value: felt252) { self.map.write(key, value); } fn get(self: @ContractState, key: ContractAddress) -> felt252 { self.map.read(key) } } } ``` -------------------------------- ### Deploying a Contract using UDC in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/accounts/universal-deployer.adoc This Cairo example demonstrates how to interact with the Universal Deployer Contract (UDC) to deploy a new contract. It defines the UDC interface, sets the UDC address, and then calls the `deployContract` function with specific deployment parameters including the class hash, a salt, a unique flag, and empty calldata. The function returns the address of the newly deployed contract. ```Cairo #[starknet::interface] trait IUniversalDeployer { fn deployContract( ref self: TContractState, class_hash: ClassHash, salt: felt252, unique: bool, calldata: Span ) -> ContractAddress; } const UDC_ADDRESS: felt252 = 0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf; fn deploy() -> ContractAddress { let dispatcher = IUniversalDeployerDispatcher { contract_address: UDC_ADDRESS.try_into().unwrap() }; // deployment parameters let class_hash = class_hash_const<0x5c478ee27f2112411f86f207605b2e2c58cdb647bac0df27f660ef2252359c6>(); let salt = 1234567879; let unique = false; let mut calldata = array![]; // the UDC returns the deployed contract address dispatcher.deployContract(class_hash, salt, unique, calldata.span()) } ``` -------------------------------- ### Implementing Storage Packing for a Custom Struct in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/starknet-by-example/storage-packing.adoc This comprehensive example demonstrates how to apply the `StorePacking` trait to a custom `Time` struct containing `u8` values. It shows the `TimePackable` implementation for `felt252` conversion, detailing the bitwise logic for `pack` and `unpack`, and integrates this packed storage into a Starknet contract's `set` and `get` functions for efficient data handling. ```Cairo #[derive(Copy, Serde, Drop)] pub struct Time { pub hour: u8, pub minute: u8, } #[starknet::interface] pub trait ITime { fn set(ref self: TContractState, value: Time); fn get(self: @TContractState) -> Time; } #[starknet::contract] mod TimeContract { use starknet::storage::{StoragePointerWriteAccess, StoragePointerReadAccess}; use super::Time; use starknet::storage_access::StorePacking; #[storage] struct Storage { time: Time, } impl TimePackable of StorePacking { fn pack(value: Time) -> felt252 { let msb: felt252 = 256 * value.hour.into(); let lsb: felt252 = value.minute.into(); msb + lsb } fn unpack(value: felt252) -> Time { let value: u16 = value.try_into().unwrap(); let (q, r) = DivRem::div_rem(value, 256_u16.try_into().unwrap()); let hour: u8 = Into::::into(q).try_into().unwrap(); let minute: u8 = Into::::into(r).try_into().unwrap(); Time { hour, minute } } } #[abi(embed_v0)] impl TimeContract of super::ITime { fn set(ref self: ContractState, value: Time) { // This will call the pack method of the TimePackable trait // and store the resulting felt252 self.time.write(value); } fn get(self: @ContractState) -> Time { // This will read the felt252 value from storage // and return the result of the unpack method of the TimePackable trait return self.time.read(); } } } ``` -------------------------------- ### Example Starknet Transaction Receipt Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/network-architecture/transaction-life-cycle.adoc An example of a transaction receipt returned by the `starknet_getTransactionReceipt` method, illustrating the structure and typical fields such as `actual_fee`, `block_hash`, `events`, `execution_status`, and `messages_sent`. ```JSON { "jsonrpc": "2.0", "result": { "actual_fee": "0x221db5dbf6db", "block_hash": "0x301fc0d09c5810600af7bb9610be10596ad6f4e6d28a60d397dd148f0962a88", "block_number": 906096, "events": [ { "data": [ "0x181de8b0cd32999a5cc962c5f724bc0f6a322f02957b80e1d5fef49a87588b7", "0x0", "0x9184e72a000", "0x0" ], "from_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", "keys": [ "0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9" ] }, { "data": [ "0x764da020183e28a48ee38a9474f84e7e5ff13194", "0x9184e72a000", "0x0", "0x181de8b0cd32999a5cc962c5f724bc0f6a322f02957b80e1d5fef49a87588b7" ], "from_address": "0x73314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82", "keys": [ "0x194fc63c49b0f07c8e7a78476844837255213824bd6cb81e0ccfb949921aad1" ] }, { "data": [ "0x181de8b0cd32999a5cc962c5f724bc0f6a322f02957b80e1d5fef49a87588b7", "0x1176a1bd84444c89232ec27754698e5d2e7e1a7f1539f12027f28b23ec9f3d8", "0x221db5dbf6db", "0x0" ], "from_address": "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7", "keys": [ "0x99cd8bde557814842a3121e8ddfd433a539b8c9f14bf31ebf108d12e6196e9" ] } ], "execution_status": "SUCCEEDED", "finality_status": "ACCEPTED_ON_L2", "messages_sent": [ { "from_address": "0x73314940630fd6dcda0d772d4c972c4e0a9946bef9dabf4ef84eda8ef542b82", "payload": [ "0x0", "0x764da020183e28a48ee38a9474f84e7e5ff13194", "0x9184e72a000", "0x0" ], "to_address": "0xc3511006c04ef1d78af4c8e0e74ec18a6e64ff9e" } ], "transaction_hash": "0xdeadbeef", "type": "INVOKE" }, "id": 1 } ``` -------------------------------- ### Generating a New Starknet Project with Scarb Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/hellostarknet.adoc This command initializes a new Starknet project named `hello_starknet` using the Scarb build tool. It sets up the default project structure, including configuration files and a basic contract, and prompts the user to select a test runner, typically 'Starknet Foundry'. ```terminal scarb new hello_starknet ``` -------------------------------- ### Building Juno from Source Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/juno.adoc This snippet clones the Juno repository from GitHub, navigates into the directory, checks out a specific version (`v0.14.3`), and then compiles the Juno application using `make juno`. ```terminal git clone https://github.com/NethermindEth/juno.git cd ./juno git checkout v0.14.3 make juno ``` -------------------------------- ### Building Documentation Content with Antora (Bash) Source: https://github.com/starknet-io/starknet-docs/blob/main/README.adoc This command uses `npx` to execute the Antora build process, compiling the AsciiDoc content defined in `playbook.yml` into a static website, typically generated in the `public_html` directory. ```Bash npx antora playbook.yml ``` -------------------------------- ### Example of Reading Contract Storage in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This example demonstrates how to use `storage_read_syscall` to retrieve a value from contract storage. It first derives a `StorageAddress` from a `felt252` value using `storage_base_address_from_felt252` and then calls `storage_read_syscall` with domain `0` and the derived address. ```Cairo use starknet::storage_access::storage_base_address_from_felt252; ... let storage_address = storage_base_address_from_felt252(3534535754756246375475423547453) storage_read_syscall(0, storage_address).unwrap_syscall() ``` -------------------------------- ### Starknet Devnet Initialization Output (Terminal) Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/devnet.adoc Displays the expected output after successfully initializing Starknet Devnet. It includes details about predeployed `FeeToken` and `UDC` (Universal Deployer Contract) addresses and class hashes, along with the `Chain ID`, indicating the local network is ready for interaction. ```terminal Predeployed FeeToken ETH Address: 0x49D36570D4E46F48E99674BD3FCC84644DDD6B96F7C741B1562B82F9E004DC7 STRK Address: 0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d Class Hash: 0x046ded64ae2dead6448e247234bab192a9c483644395b66f2155f2614e5804b0 Predeployed UDC Address: 0x41A78E741E5AF2FEC34B695679BC6891742439F7AFB8484ECD7766661AD02BF Class Hash: 0x7B3E05F48F0C69E4A65CE5E076A66271A527AFF2C34CE1083EC6E1526997A69 Chain ID: SN_SEPOLIA (0x534e5f5345504f4c4941) ... ``` -------------------------------- ### Example: Extracting Block Number from Execution Info (Cairo) Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This Cairo example demonstrates how to extract the `block_number` from the `ExecutionInfo` struct obtained via the `get_execution_info` system call. It shows the necessary unboxing operations to access nested `block_info` and subsequently the `block_number` field. ```Cairo let execution_info = get_execution_info().unbox(); let block_info = execution_info.block_info.unbox(); let block number = block_info.block_number; ``` -------------------------------- ### Cloning Starknet Staking V2 Repository (Juno) - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/block-attestation.adoc This snippet provides the terminal commands to clone the official Starknet Staking V2 repository from GitHub and navigate into its directory. This is the foundational step for setting up the block attestation service for Juno validators. ```terminal git clone https://github.com/NethermindEth/starknet-staking-v2 cd starknet-staking-v2 ``` -------------------------------- ### Compiling Starknet Validator Tool (Juno) - Terminal Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/guides/pages/staking-on-starknet/block-attestation.adoc This command compiles the validator tool within the cloned `starknet-staking-v2` repository. Successful compilation is a prerequisite for configuring and running the Juno block attestation service. ```terminal make validator ``` -------------------------------- ### Response from Calling get_balance - Console Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/quick-start/pages/devnet.adoc This console output shows the successful response from calling the `get_balance` function, indicating an initial balance of 0x0 (zero) for the `HelloStarknet` contract. ```console command: call response: [0x0] ``` -------------------------------- ### Retrieving secp256k1 Point Coordinates Example in Cairo Source: https://github.com/starknet-io/starknet-docs/blob/main/components/Starknet/modules/architecture-and-concepts/pages/smart-contracts/system-calls-cairo1.adoc This example demonstrates how to retrieve the `x` and `y` coordinates of an existing `Secp256k1Point` using `secp256k1_get_xy_syscall`. It first creates a point using `secp256k1_new_syscall` with known `x` and `y` values, then calls `secp256k1_get_xy_syscall`, and finally asserts that the retrieved coordinates match the original input values. ```Cairo use starknet::secp256k1::Secp256k1Point; // Create a point to extract coordinates from let x = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798_u256; let y = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8_u256; let point = secp256k1_new_syscall(x, y).unwrap_syscall().expect('Invalid point'); // Get the coordinates let (point_x, point_y) = secp256k1_get_xy_syscall(point).unwrap_syscall(); // Verify the coordinates match the original values assert(point_x == x, 'x coordinate mismatch'); assert(point_y == y, 'y coordinate mismatch'); ```