title: 28. ERC20 tags: cairo starknet application wtfacademy ERC20 WTF Cairo极简教程: 28. ERC20 我最近在学 ,巩固一下细节,也写一个 ,供小白们使用。教程基于 版本。 推特:@0xAAScience|@WTFAcademy WTF Academy 社群:Discord|微信群|官网 wtf.academy 所有代码和教程开源在 github: github.com/WTFAcademy/WTF-Cairo 之前我们在接口那一讲提到了IERC20,并实现了一个完整的ERC20合约,这一讲,我们将介绍 代币标准,精简其中的内容,实现一个最简的ERC20合约,并发行自己的测试代币。
title: 28. ERC20 tags: - cairo - starknet - application - wtfacademy - ERC20
我最近在学cairo-lang,巩固一下细节,也写一个WTF Cairo极简教程,供小白们使用。教程基于cairo 2.2.0版本。
WTF Academy 社群:Discord|微信群|官网 wtf.academy
所有代码和教程开源在 github: github.com/WTFAcademy/WTF-Cairo
之前我们在接口那一讲提到了IERC20,并实现了一个完整的ERC20合约,这一讲,我们将介绍ERC20代币标准,精简其中的内容,实现一个最简的ERC20合约,并发行自己的测试代币。
ERC20是以太坊上的代币标准,来自2015年11月V神参与的EIP20。它实现了代币转账的基本逻辑:
ERC20有两个事件:Transfer事件和Approval事件,分别在转账和授权时被释放
#[event] #[derive(Drop, PartialEq, starknet::Event)] enum Event { Transfer: Transfer, Approval: Approval, } #[derive(Drop, PartialEq, starknet::Event)] struct Transfer { #[key] from: ContractAddress, #[key] to: ContractAddress, value: u256, } #[derive(Drop, PartialEq, starknet::Event)] struct Approval { #[key] owner: ContractAddress, #[key] spender: ContractAddress, value: u256, }
IERC20定义了6个函数,提供了转移代币的基本功能,并允许代币获得批准,以便其他链上第三方使用。
totalSupply()返回代币总供给
/** * @dev 返回代币总供给. */ fn totalSupply(self: @TContractState) -> u256;
balance_of()返回账户余额
/** * @dev 返回账户`account`所持有的代币数. */ fn balance_of(self: @TContractState, account: ContractAddress) -> u256;
allowance()返回授权额度
/** * @dev 返回`owner`账户授权给`spender`账户的额度,默认为0。 * * 当{approve} 或 {transferFrom} 被调用时,`allowance`会改变. */ fn allowance(self: @TContractState, owner: ContractAddress, spender: ContractAddress) -> u256;
transfer()转账
/** * @dev 转账 `amount` 单位代币,从调用者账户到另一账户 `to`. * * 如果成功,返回 `true`. * * 释放 {Transfer} 事件. */ fn transfer(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool;
approve()授权
/** * @dev 调用者账户给`spender`账户授权 `amount`数量代币。 * * 如果成功,返回 `true`. * * 释放 {Approval} 事件. */ fn approve(ref self: TContractState, spender: ContractAddress, amount: u256) -> bool;
transfer_from() 授权转账
/** * @dev 通过授权机制,从`from`账户向`to`账户转账`amount`数量代币。转账的部分会从调用者的`allowance`中扣除。 * * 如果成功,返回 `true`. * * 释放 {Transfer} 事件. */ fn transfer_from(ref self: TContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool;
现在我们写一个简化版的ERC20合约。
我们需要将上述函数放到接口中:
use starknet::ContractAddress; #[starknet::interface] trait IERC20<TContractState> { fn totalSupply(self: @TContractState) -> u256; fn balance_of(self: @TContractState, account: ContractAddress) -> u256; fn allowance(self: @TContractState, owner: ContractAddress, spender: ContractAddress) -> u256; fn transfer(ref self: TContractState, recipient: ContractAddress, amount: u256) -> bool; fn approve(ref self: TContractState, spender: ContractAddress, amount: u256) -> bool; fn transfer_from(ref self: TContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool; }
我们需要状态变量来记录账户余额,授权额度和代币信息。其中name,symbol,decimals对应代币的名称,代号和小数位数。total_supply对应代币的总量,而使用映射类型的balances和allowances则对应账户余额和授权额度。
#[storage] struct Storage { name: felt252, symbol: felt252, decimals: u8, total_supply: u256, balances: LegacyMap::<ContractAddress, u256>, allowances: LegacyMap::<(ContractAddress, ContractAddress), u256>, }
导入外部包
use starknet::get_caller_address; use starknet::ContractAddress;
构造函数:初始化代币名称、代号和小数位数。
totalSupply()返回代币总供给
fn totalSupply(self: @ContractState) -> u256 { self.total_supply.read() }
balance_of()返回账户余额
fn balance_of(self: @ContractState, account: ContractAddress) -> u256 { self.balances.read(account) }
allowance()返回授权额度
fn allowance( self: @ContractState, owner: ContractAddress, spender: ContractAddress ) -> u256 { self.allowances.read((owner, spender)) }
transfer()函数:实现IERC20中的transfer函数,代币转账逻辑。调用方扣除amount数量代币,接收方增加相应代币。土狗币会魔改这个函数,加入税收、分红、抽奖等逻辑。
fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) -> bool { let mut sender = get_caller_address(); self.balances.write(sender, self.balances.read(sender) - amount); self.balances.write(recipient, self.balances.read(recipient) + amount); self.emit(Transfer { from: sender, to: recipient, value: amount }); true }
approve()函数:实现IERC20中的approve函数,代币授权逻辑。被授权方spender可以支配授权方的amount数量的代币。
fn approve(ref self: ContractState, spender: ContractAddress, amount: u256) -> bool { let mut caller = get_caller_address(); self.allowances.write((caller,spender), amount); self.emit(Approval { owner: caller, spender: spender, value: amount}); true }
transferFrom()函数:实现IERC20中的transferFrom函数,授权转账逻辑。被授权方将授权方sender的amount数量的代币转账给接收方recipient。
fn transfer_from( ref self: ContractState, sender: ContractAddress, recipient: ContractAddress, amount: u256 ) -> bool { let mut caller = get_caller_address(); let mut allowance = self.allowances.read((sender, caller)); self.allowances.write((sender, caller), allowance - amount); self.balances.write(sender, self.balances.read(sender) - amount); self.balances.write(recipient, self.balances.read(recipient) + amount); self.emit(Transfer { from: sender, to: recipient, value: amount }); true }
mint()函数:铸造代币函数,不在IERC20标准中。这里为了教程方便,任何人可以铸造任意数量的代币,实际应用中会加权限管理,只有owner可以铸造代币:
fn mint(ref self:ContractState, amount: u256) { let mut caller = get_caller_address(); self.balances.write(caller, self.balances.read(caller) + amount); self.total_supply.write(self.total_supply.read() + amount); self.emit(Transfer{ from: Zeroable::zero(), to: caller, value: amount }); }
burn()函数:销毁代币函数,不在IERC20标准中。
fn burn(ref self:ContractState, amount: u256) { let mut caller = get_caller_address(); self.balances.write(caller, self.balances.read(caller) - amount); self.total_supply.write(self.total_supply.read() - amount); //self.emit(Transfer { from: caller, to: Zeroable::zero(), value: amount}); }
ERC20代币由于Remix上的限制,我们部署不使用接口和事件的ERC20合约(ERC20_NO_INTERFACE_EVENT.cairo)。现在,我们发行属于我们的第一个代币。
在Remix上编译并declareERC20合约,在构造函数的参数栏,name和symbol都设为WTF,然后点击Deploy键进行部署。

现在,我们创建好了WTF代币,接下来,我们要给自己铸造一些代币。点击Interact中Write,在mint那一栏输入要铸造的代币数量,需要注意的是,u256分为高地址和低地址,我们在高地址输入0,在低地址输入100,并点击Call按钮,这样我们就铸造好了100个WTF代币。

我们通过read中的balance_of()函数可以查看对应地址的代币余额,输入我们的账户,可以看到余额变为100,铸造成功。

在这一讲,我们学习了ERC20标准及其实现,并发行了我们的测试代币。