timezone |
---|
Asia/Shanghai |
- Former banker and securities industry professional, engaged in macro and microeconomic analysis, stock, futures, forex, cryptocurrency trading, and quantitative research.
- yes
Historical Management of Societal Roles: Traditionally, societal roles such as currency, property rights, and social status have been governed by centralized entities through protocols and registries, with their value dependent on widespread trust in their integrity.
Challenges of Centralization: Centralized management often faces issues like corruption, conflicts of interest, and exclusion (Eli Ben-Sasson, Bareli, Brandt, Volokh, 2023).
Bitcoin's Integrity Web: Bitcoin introduced a novel approach called an "integrity web," characterized by:
Public Protocol: It is governed by an openly described protocol.
>>Inclusive Peer-to-Peer Network: It operates over a broad and decentralized network.
>>Fair Value Distribution: It distributes value fairly to maintain societal consensus on its integrity.
Ethereum's Expansion: Ethereum built upon Bitcoin's concept by extending the integrity web to include any function definable by computer programming, beyond just monetary functions.
Scalability vs. Decentralization: Both Bitcoin and Ethereum struggle with balancing scalability and decentralization. They often prioritize inclusivity, allowing verification of the system's integrity by those with limited resources, which can affect their ability to meet global demand.
Blockchain Definition In the rapidly evolving technological landscape, defining "Blockchain" is complex, but Eli Ben-Sasson (2023) highlights three key properties:
- Public Protocol: Blockchain operates on an openly accessible protocol, fostering transparency and trust, which are vital for broader adoption.
- Open P2P Network: Rather than being centralized, blockchain functions over a peer-to-peer (P2P) network, enhancing resilience and reducing the risk of censorship or failure.
- Value Distribution: Blockchain incentivizes its participants by distributing value in a wide-ranging, equitable manner. This motivates operators to maintain the system’s integrity while promoting consensus.
While these properties encapsulate the essence of Blockchain, the definition may evolve as the technology matures.
Starknet Definition Starknet is a Layer-2 solution for Ethereum, designed to enhance transaction speed, reduce costs, and ensure security using zk-STARKs technology. It balances scalability and consensus through a framework of mathematical proofs, allowing anyone to validate its integrity. Starknet enables powerful operators to boost network capacity while ensuring universal accessibility to verification tools.
Starknet’s Mission Starknet aims to empower individuals to freely implement any social functions they desire, rooted in decentralization and inclusivity.
Starknet’s Values
- Lasting Broadness: Starknet avoids power centralization, ensuring broad participation in its governance and operations. Its protocol remains open, and inclusivity is central to its structure.
- Neutrality: Starknet remains neutral, agnostic to the societal functions it supports, and resistant to censorship.
- Individual Empowerment: Education and autonomy of users are prioritized, fostering informed participation in the ecosystem.
Key Features of Starknet
- Low Costs: Starknet transactions are cheaper than those on Ethereum, and upcoming updates like Volition and EIP 4844 will further reduce costs.
- Developer-Friendly: It allows easy dApp development through its native language, Cairo.
- Speed and Efficiency: Future releases aim to further improve transaction speed and reduce costs.
- CVM (Cairo Virtual Machine): Starknet operates its own VM (Cairo VM), which enables innovative decentralized applications beyond the Ethereum Virtual Machine (EVM).
Key innovations include Account Abstraction, Volition for regulating data availability, and Paymaster, which allows flexible transaction fee payments, including gasless transactions.
Cairo: The Language of Starknet Cairo, inspired by Rust, is the programming language for creating scalable and secure STARK-based smart contracts. Developers can start learning Cairo through resources like the Cairo Book and Starklings.
Starknet Governance Starknet’s governance is overseen by the Starknet Foundation, focusing on community-driven development and decision-making. Governance involves community voting on protocol upgrades, starting with testing on the Goerli Testnet and then moving to Mainnet after approval.
SNIP: Starknet Improvement Proposals SNIPs (Starknet Improvement Proposals) are formal blueprints for enhancing the Starknet protocol. They outline technical specifications and rationale for proposed changes. SNIPs provide transparency and are vital for technical discussions and decision-making within the Starknet community.
Starknet is a scalable layer 2 solution built on Ethereum, designed to improve transaction throughput while maintaining Ethereum's security. The guide you provided outlines how to deploy and interact with a Starknet smart contract using the Cairo programming language, which specializes in creating validity proofs.
-
Setting Up the Development Environment:
Developers are advised to use the Remix IDE with the Starknet plugin enabled. After installation, they need to select the appropriate Cairo version (v2.5.4 in this example) under the settings tab. -
Modifying the Sample Project:
The tutorial begins with a preconfigured sample project. Developers need to check and update the Cairo version in theScarb.toml
file to ensure it matches the version being used (v2.5.4). -
Cleaning and Renaming the Project:
Rename the root directory to "ownable" and adjust theScarb.toml
file's[package]
section to reflect this change. Certain files (balance.cairo
andforty_two.cairo
) must be deleted, and thelib.cairo
file should be cleared to prepare for new content.
These steps form the groundwork for deploying an example contract (Ownable
) on Starknet. For full details on the process, visit the guide directly.
-
Cairo Programming Language: It is essential to have a basic understanding of Cairo. Reading chapters 1-6 of the Cairo Book (covering topics such as Getting Started, Enums, and Pattern Matching) is advised, followed by the Starknet Smart Contracts chapter. This will help developers understand the examples in this chapter.
-
Starknet Development Tools:
- Starknet supports multiple programming languages for development, such as JavaScript, Rust, and Python.
- Front-end developers can use Starknet.js with React, while Rust and Python are ideal for back-end development.
- The Starknet SDK is available for various languages to facilitate dApp creation.
-
Tools and Frameworks:
- Scarb: A package manager for compiling smart contracts.
- Starkli: A CLI tool for interacting with the Starknet network.
- Starknet Foundry: A framework for testing smart contracts.
- Katana: Creates a local test node for development and testing.
-
SDKs and Front-end Development:
- Starknet.js: Used for building front-end applications with React.
- Starknet_py and Starknet_rs: SDKs for interfacing with Starknet using Python and Rust.
-
Testing:
- Starknet-Foundry and Devnet provide tools for testing smart contracts effectively in a local environment.
This chapter focuses on setting up the essential tools for Starknet development, including Starkli, Scarb, and Katana.
- Starkli: A command-line tool for interacting with the Starknet network.
- Install via:
curl https://get.starkli.sh | sh
starkliup
- Verify installation with:
starkli --version
- Scarb: Cairo's package manager, similar to Rust’s Cargo, used to compile Cairo code to Sierra (an intermediate language between Cairo and CASM).
- Requirements: Git must be installed and added to the system's PATH.
- Recommended installation method: Use asdf to manage Scarb versions.
asdf plugin add scarb
asdf install scarb 2.5.4
asdf global scarb 2.5.4
- Alternatively, install Scarb via the official script:
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh
- Katana: A local Starknet node for development.
- Install using the dojoup installer:
curl -L https://install.dojoengine.org | bash
dojoup
- Verify installation with:
katana --version
This chapter provides a guide for compiling, deploying, and interacting with a Starknet smart contract using tools like Starkli, Scarb, and Katana.
-
Version Check: Ensure the following commands return specified versions:
scarb --version starkli --version katana --version
Required versions:
- Katana: 0.6.0-alpha.7
- Starkli: 0.2.8 (f59724e)
-
Project Initialization:
- Create a new Scarb project with
scarb new my_contract
. - Update the
Scarb.toml
file to include Starknet dependencies.
- Create a new Scarb project with
-
Environment Variables:
- Set up a
.env
file in thesrc/
directory with:export STARKNET_ACCOUNT=katana-0 export STARKNET_RPC=http://0.0.0.0:5050
- Set up a
-
Create a Smart Contract:
- Edit
src/lib.cairo
to include a basic contract template. - Compile the contract using
scarb build
.
- Edit
-
Declare the Contract:
- Ensure environmental variables are loaded with
source .env
. - Start Katana in a separate terminal using
katana
. - Declare the contract with:
starkli declare target/dev/my_contract_hello.contract_class.json
- Successful declaration returns a unique contract class hash.
- Ensure environmental variables are loaded with
- Deploy the Contract:
- Use the command:
starkli deploy <CLASS_HASH> <CONSTRUCTOR_INPUTS>
- Convert strings to felt252 format with
starkli to-cairo-string
.
- Use the command:
-
Read Data:
- Use
starkli call <CONTRACT_ADDRESS> get_name
to fetch data without modifying state. - Decode the result with
starkli parse-cairo-string
.
- Use
-
Modify State:
- Use
starkli invoke <CONTRACT_ADDRESS> set_name <felt252>
to change the contract state. - Convert new names to felt252 format before invoking.
- Use
This chapter outlines the complete workflow for developing and managing Starknet smart contracts, focusing on ensuring compatibility between tools and providing practical commands for contract management.
Scarb is the package manager for Cairo and Starknet projects, handling dependencies, compiling projects, and streamlining the development process, similar to Rust's Cargo. Here's an overview of its usage:
-
Initialization and Workflow:
- Set up a project with
scarb new <project_name>
, which generates aScarb.toml
file and asrc/lib.cairo
file. - Add dependencies directly in the
Scarb.toml
file or via thescarb add
command. - Compile projects into Sierra code using
scarb build
, producing an intermediate output for deployment to Starknet.
- Set up a project with
-
Project Structure:
- A typical Cairo package includes a
Scarb.toml
file and asrc
directory containing.cairo
files (e.g.,lib.cairo
). - External libraries can be added via the
[dependencies]
section ofScarb.toml
, specifying either registry or Git sources.
- A typical Cairo package includes a
-
Components:
- Starting with version 2.3.0, Scarb supports Components—modular add-ons that encapsulate reusable logic, storage, and events for smart contracts, allowing for easy extension of contract functionality.
-
Example:
- A simple Cairo contract can be modularized using components, such as separating ownership logic into an
ownable_component
module and integrating it with the main contract.
- A simple Cairo contract can be modularized using components, such as separating ownership logic into an
-
Commands Cheat Sheet:
scarb new <project_name>
: Initialize a new project.scarb build
: Compile the project into Sierra code.scarb add <dependency> --git <repo>
: Add a Git-hosted dependency.scarb clean
: Remove build artifacts.
-
Version Changes:
- Version 2.3.0 introduced JSON outputs for Sierra and CASM code and support for components.
This guide helps set up essential Starknet development tools.
-
Essential Tools:
- Starkli: A command-line interface (CLI) for interacting with Starknet.
- Scarb: Cairo’s package manager for compiling code to Sierra, the intermediary language for Starknet.
- Katana: A Starknet node designed for local development.
-
Starkli Installation:
- Install via command:
curl https://get.starkli.sh | sh
- Verify installation with
starkli --version
. Repeat the steps to upgrade.
- Install via command:
-
Scarb Package Manager Installation:
- Requirements: Ensure a Git executable is available in your PATH.
- Install via asdf for version management:
asdf plugin add scarb asdf install scarb 2.5.4 asdf global scarb 2.5.4
- Alternatively, install directly:
curl --proto '=https' --tlsv1.2 -sSf https://docs.swmansion.com/scarb/install.sh | sh
- Verify installation with
scarb --version
.
-
Katana Node Installation:
- Install using the Dojo engine installer:
curl -L https://install.dojoengine.org | bash
- Verify installation with
katana --version
.
- Install using the Dojo engine installer:
Scarb is Cairo’s package manager designed for both Cairo and Starknet projects. It simplifies development by handling dependencies, compiling code, and integrating with tools like Foundry.
- Initialize a project with
scarb new
, generating aScarb.toml
file and a basic project structure. - Add Code to the
src
directory. - Add Dependencies via
scarb add
. - Compile the project using
scarb build
, converting it into Sierra code, the intermediate layer between Cairo and CASM.
- Projects follow a modular structure where each package has a
Scarb.toml
manifest file and asrc/lib.cairo
file. - Packages can include external libraries and modules, which are declared in the
Scarb.toml
file.
Example project structure:
my_package/
├── src/
│ ├── lib.cairo
├── Scarb.toml
- Build projects using
scarb build
, which generates the compiled Sierra code. - Add dependencies by editing
Scarb.toml
or usingscarb add
. Dependencies can be hosted on GitHub or other sources and pinned to specific commits for consistency.
Components in Scarb allow for reusable modular add-ons that extend contracts without rewriting logic. For instance, ownership functionality can be separated into a component and plugged into various contracts.
A typical Starknet contract can use components like ownable_component
for ownership management, ensuring reusable and cleaner contract design.
scarb new <project_name>
: Initialize a new project.scarb build
: Compile Cairo code into Sierra.scarb add <dependency>
: Add a dependency from a Git repository.scarb clean
: Remove build artifacts.
Scarb is a powerful tool in the Cairo ecosystem, helping developers efficiently manage packages, dependencies, and code. With more experience, developers can fully leverage its capabilities to enhance their Starknet projects. Components, in particular, make it easier to build modular, reusable contract logic.
Katana: A Local Node for Starknet Development
Katana is a tool developed by the Dojo team to aid in local development for Starknet. It allows developers to run a local Starknet environment for testing and interacting with smart contracts without needing to rely on public testnets. This local node is highly beneficial for fast, iterative contract development and debugging.
- Local Development & Testing: Katana provides a local node, making it easier to test contracts, similar to the starknet-devnet (a public testnet maintained by Shard Labs).
- Contract Interaction: It enables developers to deploy and interact with contracts in a local setting. Examples of contract interaction using Katana can be found in resources like The Cairo Book.
- RPC Integration: Katana supports Remote Procedure Calls (RPC), allowing communication between nodes and making it possible to interact with your local node via tools like Infura or Alchemy.
-
Installation: Katana can be installed using the
dojoup
installer with the following commands:curl -L https://install.dojoengine.org | bash dojoup
After installation, verify it using
katana --version
. -
Starting a Local Node: Launch a local Starknet node with specific parameters such as the number of accounts and seed:
katana --accounts 3 --seed 0
This creates prefunded accounts for testing, with output displaying their addresses, private and public keys, and the URL for the JSON-RPC server (e.g.,
http://0.0.0.0:5050
). -
Persistence: The local node does not store data permanently, meaning all information is erased after the node stops. To stop the node, simply use
Ctrl+C
.
-
环境准备:确保安装
scarb
和starkli
,并成功运行相关命令。 -
智能钱包设置:创建包含私钥的 Signer 和包含钱包地址的 Account Descriptor,部署到 Sepolia Testnet。
-
密钥管理:使用 Starkli 生成和安全存储 keystore 文件。
-
RPC 提供者选择:推荐使用 Infura、Alchemy 或免费 RPC 服务。
-
账户描述符创建:通过 Starkli 的 fetch 命令生成 Account Descriptor 文件,包含智能钱包的独特信息。
-
合约声明与部署:先声明合约,再部署实例,确保使用正确的 class hash 和构造参数。
-
与合约交互:使用 call 命令读取合约状态,使用 invoke 命令修改状态,处理潜在错误。
-
验证与确认:通过区块浏览器检查合约部署状态和交互结果。
-
Starkli 简介:
- Starkli 是一个命令行工具,旨在简化与 Starknet 的交互,基于 starknet-rs。
-
基本设置:
- 确保完成基本安装后,可以通过命令
starkli --version
验证安装。
- 确保完成基本安装后,可以通过命令
-
连接 Starknet:
- Starkli 使用 JSON-RPC 提供者进行连接,选项包括:
- Infura 或 Alchemy 等服务。
- 临时本地节点(如 katana)用于开发和测试。
- 自建节点。
- Starkli 使用 JSON-RPC 提供者进行连接,选项包括:
-
使用 Katana 交互:
- 启动 Katana 并获取链 ID 和最新区块号的命令示例。
- 通过命令
starkli declare
声明合约并查看状态变化。
-
合约部署:
- Starkli 支持参数解析,简化合约部署过程,通过特定格式传递构造函数输入。
-
与 Testnet 交互:
- 使用第三方 JSON-RPC API 与 Testnet 交互,示例命令可获取最新区块号。
-
环境变量设置:
- 设置环境变量以便进行 ETH 转账等操作,并通过
starkli invoke
命令执行。
- 设置环境变量以便进行 ETH 转账等操作,并通过
-
脚本创建:
- 提到将在下一小节中创建一个 Bash 脚本,以进一步与 Starknet 交互。
这段内容详细说明了如何创建和运行自定义 Bash 脚本与 Starknet 进行交互,具体分为两个示例:Katana Local Node 和 Goerli Testnet。
- 目的:连接到本地 StarkNet 开发网络,获取当前链 ID、最新区块号和指定账户的余额。
- 步骤:
-
确保 Katana 在终端 1 中运行:
katana
-
在终端 2 中创建一个脚本文件:
touch script_devnet
-
使用文本编辑器编辑该文件,插入以下脚本:
#!/bin/bash chain=$(starkli chain-id --rpc http://0.0.0.0:5050) echo "Connected to the Starknet local devnet with chain id: $chain" block=$(starkli block-number --rpc http://0.0.0.0:5050) echo "The latest block number on Katana is: $block" account1="0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973" balance=$(starkli balance $account1 --rpc http://0.0.0.0:5050) echo "The balance of account $account1 is: $balance ETH"
-
执行脚本:
bash script_devnet
-
输出内容将显示连接的链 ID、最新区块号以及指定账户的余额。
-
- 目的:连接到 Goerli 测试网络,读取最新区块号并检索特定交易哈希的交易收据。
- 步骤:
-
创建一个脚本文件:
touch script_testnet
-
编辑文件并粘贴以下脚本:
echo "Input your testnet API URL: " read url chain=$(starkli chain-id --rpc $url) echo "Connected to the Starknet testnet with chain id: $chain" block=$(starkli block-number --rpc $url) echo "The latest block number on Goerli is: $block" echo "Input your transaction hash: " read hash receipt=$(starkli receipt $hash --rpc $url) echo "The receipt of transaction $hash is: $receipt"
-
执行脚本:
bash script_testnet
-
输入测试网 API URL 和交易哈希(示例哈希:
0x2dd73eb1802aef84e8d73334ce0e5856b18df6626fe1a67bb247fcaaccaac8c
)。
-
这些示例展示了如何通过自定义 Bash 脚本实现与 Starknet 的交互,帮助用户获取链信息、区块数据和账户余额等,增强了对区块链网络的操作能力。
Starknet Devnet (Rust-based) is a local development environment designed for testing and development on the Starknet platform. It is similar to the Python-based version but implemented in Rust for performance.
- Docker: Starknet Devnet can be installed using Docker (follow provided instructions).
- Manual Installation:
- Prerequisites: Install Rust (follow Rust installation guide).
- Procedure: Clone the repository using the command
git clone https://github.com/0xSpaceShard/starknet-devnet-rs.git
, and then runcargo run
to start.
Once installed, running the command cargo run
starts the Devnet, predeploying several contracts and accounts (with addresses, private and public keys, and seed values provided). A sample output will include details such as:
- Fee Token Contract
- Universal Deployer Contract
- Predeployed Accounts
- Seed-based Account Replication: The
--seed
flag enables reproducing the same account sequence across different sessions.- Example:
cargo run -- --seed 912753742
- Example:
- Dumping and Loading Data:
- To save your progress, use the
--dump-on exit --dump-path
flag. To load saved data, specify the dump path and seed during launch. - Example for dumping:
cargo run -- --dump-on exit --dump-path ./dumps/contract_1
- Example for loading:
cargo run -- --dump-path ./dumps/contract_1 --seed 912753742
- To save your progress, use the
--accounts
: Specify number of predeployed accounts.--account-class
: Set the class used by predeployed accounts (e.g.,cairo0
orcairo1
).--initial-balance
: Set initial account balance.--port
and--host
: Define where the server listens for connections.
Dumping and loading data across different versions may not be compatible, so caution is advised when upgrading Devnet versions.
Tokens can be minted by making a POST request:
curl -d '{"amount":8646000000000, "address":"0x6e...eadf"}' -H "Content-Type: application/json" -X POST http://localhost:5050/mint
This Rust version of Starknet Devnet offers a similar feature set to the Python-based version but provides performance optimizations through Rust.
Designing Freedom by Stafford Beer is a collection of lectures from 1973, where Beer explores cybernetics, management, and organizational theory. He introduces the concept of "freedom" in the context of self-organizing systems, arguing that true freedom is found within systems that provide enough structure to guide decision-making while allowing flexibility for adaptation and innovation.
Key themes include:
- Systems Thinking: Beer emphasizes the need for understanding organizations as dynamic, interconnected systems. He advocates for using cybernetic principles to design more responsive and adaptive systems.
- Homeostasis in Organizations: Like biological systems, organizations must maintain balance and stability while responding to environmental changes. Beer refers to this as homeostasis, emphasizing the importance of continuous feedback loops.
- Viable System Model (VSM): One of Beer’s most influential contributions, VSM is a framework for designing systems that can survive in complex and unpredictable environments. It is built on the idea that for any system to be viable, it must balance autonomy with central control.
- Freedom and Control: Beer challenges the traditional dichotomy between freedom and control, proposing that freedom is maximized not by removing constraints but by designing systems that allow flexibility within appropriate constraints.
The Deployment Script Example for Starknet smart contracts guides users through setting up a testing and deployment environment, using a Bash script to automate tasks such as initializing accounts, testing contracts, building and deploying, and performing multicalls. Below is a summary of the steps:
- Compatible with scarb 2.4.3, cairo 2.4.3, sierra 1.4.0, snforge 0.14.0, and sncast 0.14.0.
- Requires installation of jq for handling JSON.
- Ensure you complete the Starknet Devnet setup beforehand.
-
Create Script File:
- Create a
script.sh
in the project’s root directory. - Make the script executable using:
chmod +x script.sh
.
- Create a
-
Insert Script:
The script includes functions and steps to automate the testing and deployment process. Important elements are:- Error Management: Stops on the first error with
set -e
. - Environment Variables: Use these for security (e.g.,
ACCOUNT1_ADDRESS
,ACCOUNT1_PRIVATE_KEY
). - Account Setup: Defines accounts for smart contracts via JSON and manages them through
sncast
. - Contract Testing: Runs tests using
snforge
, proceeding only if tests pass. - Contract Deployment: Uses
scarb
to build andsncast
to declare and deploy contracts, handling different Devnet versions (Rust or Python). - Multicalls: Creates a
multicall.toml
to execute multiple contract function invocations. - State Queries: Checks the contract's state (e.g., balances) after deployment.
- Error Management: Stops on the first error with
-
Optional Adjustments:
Modify the Bash path if necessary by usingwhich bash
.
To run the script, provide private key environment variables before execution:
ACCOUNT1_PRIVATE_KEY="your_key" ACCOUNT2_PRIVATE_KEY="your_key" ./script.sh
- The script uses
set -e
to enhance reliability by stopping on command failure. - Secure private keys and sensitive information. Consider moving hardcoded values (e.g., account addresses) to a configuration file.
Starknet.js is a JavaScript/TypeScript library used to connect decentralized applications (DApps) to Starknet, an Ethereum layer 2 solution. Its architecture is modeled after ethers.js, making it easier for developers familiar with Ethereum to work with Starknet.
-
Installation:
- Install via npm:
- Stable release:
npm install starknet
- Latest features:
npm install starknet@next
- Stable release:
- Install via npm:
-
Core Concepts:
- Account: Represents the end-user and their wallet. Unlike Ethereum's Externally Owned Accounts (EOAs), Starknet accounts are contracts.
- Provider: Used to interact with the Starknet network, providing a "read" connection. Services like Infura or Alchemy can be used to set up an RPC provider.
- Contracts: Interact with deployed smart contracts using the contract address and ABI. Contracts require a signer to modify blockchain states.
-
Handling Units:
- Special utility functions are available to handle data types such as Cairo's Uint256 structs.
-
Deployment: Starknet.js supports deploying smart contracts. The deployment process involves setting up a Node.js environment, preparing the necessary scripts, and configuring TypeScript. It also walks through creating key pairs, compiling contract code, and deploying contracts on Starknet.
-
Additional Tools:
- Automated contract deployment, interaction with account contracts, and ETH transfers using pre-defined scripts.
-
Setup and Global Variables:
- Defines essential variables like contract names, profile names, addresses, and keys. It uses environment variables for sensitive data such as private keys, which is a good security practice, although additional precautions like using
.env
files would further improve safety.
- Defines essential variables like contract names, profile names, addresses, and keys. It uses environment variables for sensitive data such as private keys, which is a good security practice, although additional precautions like using
-
Cleaning Previous Environment:
- Deletes old account files to ensure a fresh environment for new accounts.
-
Running Contract Tests:
- Runs tests using
snforge
and checks for failures. If tests fail, the script halts and prompts the user to fix the issues before continuing.
- Runs tests using
-
Creating Accounts:
- Iterates through JSON-defined account data to add or create new accounts using
sncast
. It checks for existing accounts to avoid duplication.
- Iterates through JSON-defined account data to add or create new accounts using
-
Building, Declaring, and Deploying Contracts:
- Builds the smart contract with
scarb
and declares it on the Starknet network. Depending on whether the class hash already exists, it either reuses the existing hash or declares a new one. - Deploys the contract and stores the deployed contract address.
- Builds the smart contract with
-
Executing Multicalls:
- Sets up and executes multicalls using a TOML configuration file, which runs multiple contract functions in a single batch, reducing network overhead.
-
Querying Contract State:
- After deployment and multicall execution, the script checks the contract’s state (e.g., the balance) to verify the correctness of the contract.
-
Cleanup:
- Cleans up temporary files like the multicall TOML file once execution is complete.
-
The script is run with environment variables for the private keys:
ACCOUNT1_PRIVATE_KEY="0x259f4329e6f4590b" ACCOUNT2_PRIVATE_KEY="0xb4862b21fb97d" ./script.sh
- Environment Variables: Using environment variables is safer than hardcoding private keys, but they could still be exposed to other processes on the machine. For more security, you could use tools like
dotenv
or secret management systems (e.g., AWS Secrets Manager) to manage sensitive data. - set -e: Ensures that the script exits on the first error, which helps prevent the execution of subsequent steps in case of failures, enhancing reliability.
- Configuration Files: Instead of hardcoding values such as account addresses or contract names in the script, you could move them to a configuration file, making the script easier to maintain and update.
This script offers a solid foundation for deploying smart contracts on Starknet in a controlled, automated fashion. It can be customized and extended to suit more complex scenarios or different environments.
Overview: Starknet.js is a JavaScript/TypeScript library that facilitates the connection of web applications and decentralized apps (D-Apps) to Starknet, similar in structure to ethers.js.
Installation:
- To install the library:
- For the latest release:
npm install starknet
- For the latest features:
npm install starknet@next
- For the latest release:
Key Concepts:
-
Account: Represents the end user and requires user interaction for wallet connection (e.g., ArgentX, Braavos). Starknet accounts are contracts, differing from Ethereum's Externally Owned Accounts.
async function connectWallet() { const starknet = await connect(); const nonce = await starknet.account.getNonce(); const message = await starknet.account.signMessage(...); }
-
Provider: Enables interaction with the Starknet network as a read-only connection. It can be a default provider or through services like Infura.
export const provider = new Provider({ sequencer: { network: "goerli-alpha" }, }); const block = await provider.getBlock("latest");
-
Contracts: To interact with deployed contracts, you'll need their address and ABI. A contract can be instantiated using either a provider (for read-only access) or a signer (for state-changing transactions).
const contract = new Contract(abi_erc20, contractAddress, starknet.account); const balance = await contract.balanceOf(starknet.account.address);
Unit Handling: Proper handling of unit conversions, such as converting Cairo structs like Uint256, is crucial for accurate data management.
Deployment of Smart Contracts: Starknet.js allows for automated smart contract deployments. Key steps include:
- Initializing the project environment and installing dependencies.
- Setting up TypeScript and environment configurations.
- Writing deployment scripts including account contract declaration, deployment, and interaction functions.
This guide provides a step-by-step process for integrating a simple counter smart contract with a frontend application. Readers will learn to:
- Connect the Frontend to a Smart Contract: Establish a link to interact with the smart contract.
- Initiate Transactions: Execute functions to increment or decrement the counter.
- Read and Display Data: Retrieve and show the current counter value on the frontend.
For a more in-depth exploration, viewers are encouraged to attend the Basecamp frontend session, which includes theoretical and practical components.
- React.js: Framework for building the frontend.
- @argent/get-starknet: Library for interacting with Starknet wallets.
- Starknet.js: JavaScript library for Starknet interactions.
- Clone the project repository:
git clone https://github.com/Darlington02/basecamp-frontend-boilerplate
- Navigate to the directory and install dependencies:
cd basecamp-frontend-boilerplate npm install
- Launch the project:
yarn start
- connectWallet: Connects to the blockchain via wallet providers (e.g., ArgentX).
- disconnectWallet: Ends the connection to the wallet.
- increaseCounter: Triggers the increment of the counter.
- decreaseCounter: Triggers the decrement of the counter.
- getCounter: Retrieves the current counter value.
- connectWallet: Asynchronously connects to the blockchain and updates the component state with connection details.
- disconnectWallet: Asynchronously disconnects and resets the component's state.
The useEffect
hook is employed to automatically connect to Starknet when the component mounts.
- ABI (Application Binary Interface): Defines the functions and variables of the smart contract.
- Signer: Authorizes transactions and bears execution fees.
- Provider: Facilitates communication with the blockchain and data fetching.
- increaseCounter: Interacts with the smart contract to increment the counter.
- decreaseCounter: Interacts with the smart contract to decrement the counter.
- getCounter: Fetches the current count using a provider.
-
目标:构建基于 Starknet-react 和 StarknetKit 的 web3 应用,使用 Cairo 编写的 ERC-20 智能合约。
-
环境设置:使用特定版本的工具和库,包括 Scarb、Cairo、Starknet-react 等。
-
合约开发:编写和部署 ERC-20 合约,使用 Openzeppelin 库,定义代币名称和符号。
-
前端构建:使用 NextJS、Starknet-react 和 StarknetKit,配置合约连接。
-
钱包连接:支持 Argent 和 Braavos 钱包,允许用户连接和断开。
-
功能实现:读取代币余额和转账功能,用户可以发送和接收代币。
-
结果:在 Sepolia 测试网成功完成代币的部署和交互。