虽然智能合约(Smart Contract)这个词现在已经很流行了,但如果你问我到底什么是智能合约?我还真不能马上给出准确的答案,为此,我查看了 wikipedia 上关于 Smart Contract 的词条 (http://t.cn/RVQQ1Ja):

smart contract is a computer protocol intended to digitally facilitate, verify, or enforce the negotiation or performance of a contract. Smart contracts allow the performance of credible transactions without third parties. These transactions are trackable and irreversible.

Proponents of smart contracts claim that many kinds of contractual clauses may be made partially or fully self-executing, self-enforcing, or both. The aim of smart contracts is to provide security that is superior to traditional contract law and to reduce other transaction costs associated with contracting.

可以看到,所谓智能合约中的智能,其实和人工智能中的智能(Intelligence)还是有很大的区别,之所以称之为智能合约,也许是因为它比传统的合约要先进,主要体现在以下几个方面
  1. 你相信合约就行了,不需要第三方参与
  2. 它是可追踪的,不可逆的
  3. 它是可以自动执行的
不要小看这几点特性,智能合约的普及,可以让人们在交易过程中更为透明,你和一个陌生人交易,不会再是一次博弈的关系,而是多次博弈,因为所有的交易记录都是可追踪的,试想以下,如果你在和别人交易时,事先就知道今后还会和这个人交易,你会选择合作还是背叛?当然是合作。所以,相比于传统的合约,智能合约可以减少交易成本(Transaction cost (http://t.cn/RDjUw0D)),从而整体的提升社会效率。

什么是 EOS 智能合约

在 EOSIO 开发者文档 (http://t.cn/RDjUT09) 中,有这么一段描述
A real-world contract, simply stated, is an agreement governing outcomes for actions, given a set of inputs. A contract can range from formal legal contracts (e.g., a financial transaction) to something as simple as the “rules” of a game. Typical actions can be things such as fund transfers (in the case of a financial contract) or game moves (in the case of a game contract).

真实世界的合约,简单来说,是一组协议,给予其输入一定的行为,它会产生相应的结果。从正规的法律合同(如金融交易)到简单的游戏「规则」都属于合约的范畴。典型的行为可以是转账(金融合约)或玩家的移动(游戏合约)。

An EOSIO Smart Contract is software registered on the blockchain and executed on EOSIO nodes, that implements the semantics of a “contract” whose ledger of action requests are being stored on the blockchain.

EOSIO 智能合约软件注册在区块链上,并在 EOSIO 节点上执行,它实现了「合约」的语义,并将合约的请求行为存储在区块链上,即请求账本。

EOS 定义的智能合约包含两个要素:代表合约的程序和执行合约时的行为。同时,阅读上面这段文字时,你很可能会把它和比特币系统对应起来,其实比特币是智能合约的一个特例:它只定义了转账合约,即接收 UTXO(Unspent Transaction Output)、验证签名及创建新的 UTXO;而该合约也只有一个行为——转账(发送UTXO)。

和比特币把 UTXO 记录在区块链上一样,EOS 也把所有的行为数据记录在了区块链上,唯一不同的是,EOS 是一个智能合约平台,它不仅支持转账合约,现在你能看到的 CPU、Bandwidth 抵押、RAM 人机交易市场、投票等都在其范畴之内,可以看到智能合约平台是非常具有想象力的,它提供了多样性,即便如此,它仍然由最基本的因素组成:代表合约的程序和执行合约的行为。从这个角度来看,EOS 仍然只是比特币的扩展,所以在学习中,牢记这两个要素,有助于在学习的过程中不至于迷失。

如何开发 EOS 智能合约

开发 EOS 智能合约,你需要完成该合约对应的程序,并将其上传到 EOS 平台上,计算机语言方面,目前智能合约只能用 
C++
 开发,并将其编译成 
WebAssembly
 兼容的可执行程序,因为 
Go
 语言将支持 
WebAssembly
,所以未来你也可以使用 
Golang
  来写智能合约,除此之外,也有开发者正在将 EOS 接入 
JavaScript
 虚拟机和 
Python
 虚拟机,所以在不久的将来,开发智能合约将不再受 
C++
 这座大山的限制。
C++eosio.tokeneosio.token
createissuetransfer
同时,和应用程序提供的接口(interface)一样,你需要将所有的行为暴露出去,这样使用者才可以进行调用。EOSIO 为每个合约提供了统一的行为入口——
apply
 函数,你可以把它理解为 WebService 中的路由(routing),同时,为了方便开发,EOSIO 提供了一个宏,你只需要添加一行代码,就可以实现 
apply
 功能,如下:
EOSIO_ABI( eosio::token, (create)(issue)(transfer) )
以上代码将 
create
issue
 和 
transfer
 三个行为都暴露到了外部。也就是说,使用者要执行该合约,首先进入的是 
apply
 函数,然后根据具体的行为,再进入到不同的 
create
issue
 或 
transfer
 中运行。
eosio.tokenapply
boost::multi_indexBoostC++multi_index
multi_indexmulti_indexmulti_index
apply
contracts/eosio.token

总结一下,这篇文章主要讲述了什么是智能合约,EOS 智能合约具体又指什么,以及写一个 EOS 智能合约的关键,它们是:

multi_index