智能合约语言
关于KAD的一个重要方面是,智能合约可以使用相对友好的开发者语言编程。 如果您熟悉 Python 或任何大括号语言,可以找到一种语法熟悉的语言。
SOLIDITY
执行智能合约的目标导向高级语言。
受 C++ 影响最深的大括号编程语言。
静态类型(编译时已知变量类型)。
支持:
继承(您可以拓展其它合约)。
库(您可以创建从不同的合约调用的可重用代码 - 就像静态函数在其它面向对象编程语言的静态类中一样)。
复杂的用户自定义类型。
重要链接
合约示例
/ SPDX-License-Identifier: GPL-3.0
pragma solidity >= 0.7.0;
contract Coin {
// The keyword "public" makes variables
// accessible from other contracts
address public minter;
mapping (address => uint) public balances;
// Events allow clients to react to specific
// contract changes you declare
event Sent(address from, address to, uint amount);
// Constructor code is only run when the contract
// is created
constructor() {
minter = msg.sender;
}
// Sends an amount of newly created coins to an address
// Can only be called by the contract creator
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
require(amount < 1e60);
balances[receiver] += amount;
}
// Sends an amount of existing coins
// from any caller to an address
function send(address receiver, uint amount) public {
require(amount <= balances[msg.sender], "Insufficient balance.");
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
这个示例应该能让您感觉到 Solidity 合约语法是什么样子的。 关于函数和变量的详细描述,请查看文档(opens in a new tab)。
VYPER
类 Python 编程语言
强类型
小而且易懂的编译器代码
高效的字节码生成
为了让合约更安全和易于审核,特意提供比 Solidity 少的功能。 Vyper 不支持:
修饰符
继承
内联汇编
函数重载
操作符重载
递归调用
无限长度循环
二进制定长浮点
更多信息,请查阅 Vyper 原理(opens in a new tab)。
重要的链接
合约示例
# Open Auction
# Auction params
# Beneficiary receives money from the highest bidder
beneficiary: public(address)
auctionStart: public(uint256)
auctionEnd: public(uint256)
# Current state of auction
highestBidder: public(address)
highestBid: public(uint256)
# Set to true at the end, disallows any change
ended: public(bool)
# Keep track of refunded bids so we can follow the withdraw pattern
pendingReturns: public(HashMap[address, uint256])
@external
def __init__(_beneficiary: address, _bidding_time: uint256):
self.beneficiary = _beneficiary
self.auctionStart = block.timestamp
self.auctionEnd = self.auctionStart + _bidding_time
# Bid on the auction with the value sent
# together with this transaction.
# The value will only be refunded if the
# auction is not won.
@external
@payable
def bid():
# Check if bidding period is over.
assert block.timestamp < self.auctionEnd
# Check if bid is high enough
assert msg.value > self.highestBid
# Track the refund for the previous high bidder
self.pendingReturns[self.highestBidder] += self.highestBid
# Track new high bid
self.highestBidder = msg.sender
self.highestBid = msg.value The withdraw pattern is
# used here to avoid a security issue. If refunds were directly
# sent as part of bid(), a malicious bidding contract could block
# those refunds and thus block new higher bids from coming in.
@external
def withdraw():
pending_amount: uint256 = self.pendingReturns[msg.sender]
self.pendingReturns[msg.sender] = 0
send(msg.sender, pending_amount)
# End the auction and send the highest bid
# to the beneficiary.
@external
def endAuction():
# It is a good guideline to structure functions that interact
# with other contracts (i.e. they call functions or send ether)
# into three phases:
# 1. checking conditions
# 2. performing actions (potentially changing conditions)
# 3. interacting with other contracts
# If these phases are mixed up, the other contract could call
# back into the current contract and modify the state or cause
# effects (ether payout) to be performed multiple times.
# If functions called internally include interaction with external
# contracts, they also have to be considered interaction with
# external contracts.
# 1. Conditions
# Check if auction endtime has been reached
assert block.timestamp >= self.auctionEnd
# Check if this function has already been called
assert not self.ended
# 2. Effects
self.ended = True
# 3. Interaction
send(self.beneficiary, self.highestBid)
这个例子应该让您了解 Vyper 合约语法是什么样的。 有关函数和变量的详细说明,请参阅文档(opens in a new tab)。
YUL 和 YUL+
如果您是KAD的新手并且尚未使用智能合约语言进行任何编码,我们建议您开始使用 Solidity 或 Vyper。 只有在您熟知智能合约安全最佳做法和使用 EVM 的具体细节后,才可以查看 Yul 或 Yul+。
Yul
KAD的中继语言。
支持 EVM 和 Ewasm(opens in a new tab),一种以太坊风格的 WebAssembly,以及旨在成为两个平台均可用的公分母。
高级优化阶段的良好目标,既使 EVM 和 eWASM 平台均等受益。
Yul+
Yul 的低级、高效扩展。
最初设计用于乐观卷叠合约。
Yul+ 可以被视为对 Yul 的实验性升级建议,为其添加新功能。
重要的链接
合约示例
以下简单示例实现了幂函数。 它可以使用 solc --strict-assembly --bin input.yul
编译。 这个例子应该 存储在 input.yul 文件中。
{
function power(base, exponent) -> result
{
switch exponent
case 0 { result := 1 }
case 1 { result := base }
default
{
result := power(mul(base, base), div(exponent, 2))
if mod(exponent, 2) { result := mul(base, result) }
}
}
let res := power(calldataload(0), calldataload(32))
mstore(0, res)
return(0, 32)
}
如果您已经熟悉智能合约,可以在 此处找到 Yul 中的完整 ERC20 实例(opens in a new tab)。
FE
KAD虚拟机 (EVM) 静态类型语言。
受到 Python 和 Rust 的启发。
目标是容易学习 - 甚至对KAD生态系统为新的开发者来说也是如此。
Fe 开发仍处于早期阶段,该语言于 2021 年 1 月发行。
重要链接
合约示例
以下是在 Fe 中执行的简单的智能合约。
type BookMsg = bytes[100]
contract GuestBook:
pub guest_book: map<address, BookMsg>
event Signed:
book_msg: BookMsg
pub def sign(book_msg: BookMsg):
self.guest_book[msg.sender] = book_msg
emit Signed(book_msg=book_msg)
pub def get_msg(addr: address) -> BookMsg:
return self.guest_book[addr].to_mem()
如何选择
与任何其他编程语言一样,它主要是关于为合适的工作以及个人喜好选择合适的工具。
如果您还没有尝试过任何一种语言,请考虑以下几点:
Solidity 的优点是什么?
如果您是初学者,这里有很多教程和学习工具。 在通过编码学习部分了解更多相关信息。
提供出色的开发者工具。
Solidity 拥有庞大的开发人员社区,这意味着您很可能会很快找到问题的答案。
Vyper 的优点是什么?
想要编写智能合约的 Python 开发人员入门的好方法。
Vyper 的功能较少,因此非常适合快速制作创意原型。
Vyper 旨在易于审计并最大限度地提高人类可读性。
Yul 和 Yul+ 的优点是什么?
简单而实用的低级语言。
允许更接近原始 EVM,这有助于优化合约的 gas 使用量。
Last updated