setup-cairo-contracts

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cairo Setup

Cairo 项目搭建

Project Scaffolding

项目框架搭建

Install toolchain and create a project:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.starkup.sh | sh
scarb new my_project --test-runner=starknet-foundry
This scaffolds a complete Starknet project with
snforge
testing preconfigured.
安装工具链并创建项目:
bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.starkup.sh | sh
scarb new my_project --test-runner=starknet-foundry
此命令会搭建一个预配置了
snforge
测试环境的完整Starknet项目。

OpenZeppelin Dependencies

OpenZeppelin 依赖配置

Look up the current version from https://docs.openzeppelin.com/contracts-cairo before adding. Add to
Scarb.toml
:
Full library (umbrella package):
toml
[dependencies]
openzeppelin = "<VERSION>"
Individual packages (faster builds — only compiles what you use):
toml
[dependencies]
openzeppelin_token = "<VERSION>"
openzeppelin_access = "<VERSION>"
Available individual packages:
openzeppelin_access
,
openzeppelin_account
,
openzeppelin_finance
,
openzeppelin_governance
,
openzeppelin_interfaces
,
openzeppelin_introspection
,
openzeppelin_merkle_tree
,
openzeppelin_presets
,
openzeppelin_security
,
openzeppelin_token
,
openzeppelin_upgrades
,
openzeppelin_utils
.
openzeppelin_interfaces
and
openzeppelin_utils
are versioned independently. Check the docs for their specific versions. All other packages share the same version.
完整库(整合式包):
toml
[dependencies]
openzeppelin = "<VERSION>"
独立包(构建速度更快——仅编译你用到的部分):
toml
[dependencies]
openzeppelin_token = "<VERSION>"
openzeppelin_access = "<VERSION>"
可用的独立包包括:
openzeppelin_access
openzeppelin_account
openzeppelin_finance
openzeppelin_governance
openzeppelin_interfaces
openzeppelin_introspection
openzeppelin_merkle_tree
openzeppelin_presets
openzeppelin_security
openzeppelin_token
openzeppelin_upgrades
openzeppelin_utils
openzeppelin_interfaces
openzeppelin_utils
的版本独立于其他包。请查看文档获取它们的具体版本。其余所有包共享同一版本。

Import Conventions

导入规范

The import path depends on which dependency is declared:
  • Umbrella package (
    openzeppelin = "..."
    ): use
    openzeppelin::
    as the root
  • Individual packages (
    openzeppelin_token = "..."
    ): use the package name as the root
cairo
// Individual packages
use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
use openzeppelin_access::ownable::OwnableComponent;
use openzeppelin_upgrades::UpgradeableComponent;

// Umbrella package equivalents
use openzeppelin::token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;
Components are integrated via the
component!
macro, embedded impls, and substorage:
cairo
component!(path: ERC20Component, storage: erc20, event: ERC20Event);

#[abi(embed_v0)]
impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>;
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;
导入路径取决于你声明的依赖类型:
  • 整合式包
    openzeppelin = "..."
    ):使用
    openzeppelin::
    作为根路径
  • 独立包
    openzeppelin_token = "..."
    ):使用包名作为根路径
cairo
// 独立包导入方式
use openzeppelin_token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
use openzeppelin_access::ownable::OwnableComponent;
use openzeppelin_upgrades::UpgradeableComponent;

// 整合式包等效导入方式
use openzeppelin::token::erc20::{ERC20Component, ERC20HooksEmptyImpl};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;
组件通过
component!
宏、嵌入式实现和子存储进行集成:
cairo
component!(path: ERC20Component, storage: erc20, event: ERC20Event);

#[abi(embed_v0)]
impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>;
impl ERC20InternalImpl = ERC20Component::InternalImpl<ContractState>;