3.Sway编程类型(Sway Program Types)
A Sway program itself has a type: it is either a contract, a predicate, a script, or a library. The first three of these things are all deployable to the blockchain. A library is simply a project designed for code reuse and is never directly deployed to the chain.
Sway 程序本身有一个类型:它可以是 contract、predicate、script 或 library。这些东西中的前三个都可以部署到区块链上。 library 只是一个为代码重用而设计的项目,永远不会直接部署到链上。
Every Sway file must begin with a declaration of what type of program it is. A project can have many libraries within it, but only one contract, script, or predicate. Scripts and predicates require main
functions to serve as entry points, while contracts instead publish an ABI. This chapter will go into detail about all of these various types of programs and what purposes they serve.
每个 Sway 文件 必须 从声明它是什么类型的程序开始。一个项目中可以有多个库,但只有一个合约、脚本或谓词。脚本和谓词需要 main
函数作为入口点,而合约则发布 ABI。本章将详细介绍所有这些不同类型的程序以及它们的用途。
Contracts are used primarily for protocols or systems that operate within a fixed set of rules. A good example would be a staking contract or a decentralized exchange.
合约主要用于在一组固定规则内运行的协议或系统。一个很好的例子是质押合约或去中心化交易所。
Scripts are used for complex on-chain interactions that won't persist. An example of this may be using a DEX and Lender to create a leveraged position (borrow, swap, re-collateralize, borrow) which is a complex transaction that would usually take multiple steps.
脚本用于不会持续存在的复杂链上交互。这方面的一个例子可能是使用 DEX 和贷款人来创建杠杆头寸(借入、交换、再抵押、借入),这是一个通常需要多个步骤的复杂交易。
Libraries are for code that is reusable and useful for handling common situations. A good example of this would be a library to handle fixed-point math or big number math.
库用于可复用且可用于处理常见情况的代码。一个很好的例子就是处理定点数学或大数数学的库。
Last updated