Sway编程语言-更新中(The Sway Programming Language-Updatin
  • Sway编程语言(The Sway Programming Language)
  • 1. 导言(Introduction)
    • 1.1 安装(Installation)
    • 1.2 Sway快速入门 (Sway Quickstart)
    • 1.3 Fuel工具链 (The Fuel Toolchain)
    • 1.4 一个Forc项目 (A Forc Project)
    • 1.5 标准库 (Standard Library)
  • 2. 示例(Example)
    • 2.1计数器(Counter)
    • 2.2子货币(Subcurrency)
    • 2.3 FizzBuzz
    • 2.4 钱包智能合约(Wallet Smart Contract)
  • 3.Sway编程类型(Sway Program Types)
    • 3.1 合约(Contracts)
    • 3.2 库 (Libraries)
    • 3.3 脚本(Scripts)
    • 3.4 谓词 (Predicates)
  • 4. Sway语言基础 (Sway Language basics)
    • 4.1 变量 (Variables)
    • 4.2 内置类型(Built-in Types)
    • 4.3 常用库类型(Commonly Used Library Types)
    • 4.4 区块链类 (Blockchain Types)
    • 4.5 函数 (Functions)
    • 4.6 结构、元祖和穷举 (Structs, Tuples, and Enums)
    • 4.7 方法和关联函数 (Methods and Associated Functions)
    • 4.8 注释和日志 (Comments and Logging)
    • 4.9 控制流 (Control Flow)
  • 5. 用Sway部署区块链 (Blockchain Development with Sway)
    • 5.1 哈希和加密学 (Hashing and Cryptography)
    • 5.2 合约存储(Contract Storage)
    • 5.3 函数纯度 (Function Purity)
    • 5.4 标识符(Identifiers)
    • 5.5 原生资产(Native Assets)
    • 5.6 访问控制 (Access Control)
    • 5.7 调用合约(Calling Contracts)
  • 6. 高级概念 (Advanced Concepts)
    • 6.1 高级类型 (Advanced Types)
    • 6.2 通用类型 (Generic Types)
    • 6.3 特征 (Traits)
    • 6.4 集 (Assembly)
  • 7. 一般集聚 (Common Collections)
    • 7.1 堆上的向量(Vectors on the Heap)
    • 7.2 存储向量 (Storage Vectors)
    • 7.3 存储映射 (Storage Maps)
  • 8.测试(Testing)
    • 8.1 单元测试(Unit Testing)
    • 8.2 用Rust来测试 (Testing with Rust)
  • 9.应用前端开发 (Application Frontend Development)
    • 9.1 TypeScript SDK
  • 10.Sway应用(Sway Reference)
    • 10.1 编译器内部函数(Compiler Intrinsics)
    • 10.2 属性(Attributes)
    • 10.3 风格向导(Style Guide)
    • 10.4 已知各类问题(Known Issues and Workarounds)
    • 10.5 与Solidity的不同之处 (Differences From Solidity)
    • 10.6 与Rust的不同之处 (Differences From Rust)
    • 10.7 向Sway贡献 (Contributing To Sway)
  • 11. Forc引用 (Forc Reference)
    • 11.1清单参考 (Manifest Reference)
    • 11.2 工作区(Workspaces)
    • 11.3 依赖(Dependencies)
    • 11.4 命令(Commands)
      • 11.4.1 forc-addr2line
      • 11.4.2 forc-build
      • 11.4.3 forc-check
Powered by GitBook
On this page
  • Address类 (Address Type)
  • ContractId类 (ContractId Type)
  • Identity 类 (Identity Type)
  1. 4. Sway语言基础 (Sway Language basics)

4.4 区块链类 (Blockchain Types)

Previous4.3 常用库类型(Commonly Used Library Types)Next4.5 函数 (Functions)

Last updated 2 years ago

Sway is fundamentally a blockchain language, and it offers a selection of types tailored for the blockchain use case.

Sway从根本上说是一种区块链语言,它为区块链的使用情况提供了一系列的类。

These are provided via the standard library () which both add a degree of type-safety, as well as make the intention of the developer more clear.

这些类通过标准库()提供,既增加了一定程度的类的安全性,又使开发者的意图更加明确。

Address类 (Address Type)

The Address type is a type-safe wrapper around the primitive b256 type. Unlike the EVM, an address never refers to a deployed smart contract (see the ContractId type below). An Address can be either the hash of a public key (effectively an if you're coming from the EVM) or the hash of a . Addresses own UTXOs.

Address 类是对原始的 b256类的一个类安全封装。与EVM不同的是,地址永远不会指向已部署的智能合约(见下面的ContractId类型)。一个Address可以是一个公钥的哈希值(如果你是从EVM来的,那么实际上是一个)或者是一个的哈希值。地址拥有UTXO。

An Address is implemented as follows.

一个Address的实施如下:

pub struct Address {
    value: b256,
}

Casting between the b256 and Address types must be done explicitly:

b256 和Address类型之间的转换必须明确进行:

let my_number: b256 = 0x000000000000000000000000000000000000000000000000000000000000002A;
let my_address: Address = Address::from(my_number);
let forty_two: b256 = my_address.into();

ContractId类 (ContractId Type)

The ContractId type is a type-safe wrapper around the primitive b256 type. A contract's ID is a unique, deterministic identifier analogous to a contract's address in the EVM. Contracts cannot own UTXOs but can own assets.

ContractId 类是对原始的 b256类的一个类安全封装。一个合约的ID是一个唯一的、确定的标识符,类似于EVM中的合约地址。合约不能拥有UTXO,但可以拥有资产。

A ContractId is implemented as follows.

一个 ContractId 的实施如下:

pub struct ContractId {
    value: b256,
}

Casting between the b256 and ContractId types must be done explicitly:

b256 和 ContractId类型之间的转换必须明确进行:

let my_number: b256 = 0x000000000000000000000000000000000000000000000000000000000000002A;
let my_contract_id: ContractId = ContractId::from(my_number);
let forty_two: b256 = my_contract_id.into();

Identity 类 (Identity Type)

The Identity type is an enum that allows for the handling of both Address and ContractId types. This is useful in cases where either type is accepted, e.g. receiving funds from an identified sender, but not caring if the sender is an address or a contract.

Identity类是一个穷举,允许处理 Address 和 ContractId 类。这在任何一种类都被接受的情况下是有用的,例如,从一个确定的发件人接收资金,但不关心发件人是一个地址还是一个合同。

An Identity is implemented as follows.

一个 Identity 的实施如下:

pub enum Identity {
    Address: Address,
    ContractId: ContractId,
}

Casting to an Identity must be done explicitly:

铸造到一个 Identity必须明确进行:

        let raw_address: b256 = 0xddec0e7e6a9a4a4e3e57d08d080d71a299c628a46bc609aab4627695679421ca;
        let my_identity: Identity = Identity::Address(Address::from(raw_address));

A match statement can be used to return to an Address or ContractId as well as handle cases in which their execution differs.

match 语句可用于返回到Address 或 ContractId ,以及处理它们执行不同的情况。

        let my_contract_id: ContractId = match my_identity {
            Identity::ContractId(identity) => identity,
            _ => revert(0),
        };
        match my_identity {
            Identity::Address(address) => transfer_to_address(amount, token_id, address),
            Identity::ContractId(contract_id) => force_transfer_to_contract(amount, token_id, contract_id),
        };

A common use case for Identity is for access control. The use of Identity uniquely allows both ContractId and Address to have access control inclusively.

Identity的一个常见用例是用于访问控制。使用Identity可以唯一地允许ContractId和Address, 同时拥有访问控制。

        let sender = msg_sender().unwrap();
        require(sender == storage.owner.read(), MyError::UnauthorizedUser(sender));
lib-std
lib-std
externally owned account
predicate
外部拥有的账户
谓词