第22篇 ERC20智能合约(0.6.0)

114 篇文章 712 订阅 ¥39.90 ¥99.00

本文给出 0.6.0 版本的标准ERC20智能合约(源码来自https://github.com/OpenZeppelin/openzeppelin-contracts),供大家参考。

1.标准ERC20合约源码:

该部分由4部分组成;

1.1 SafeMath.sol

主要处理算术运算中的溢出问题,达到更安全的计算目的;使用该库可以消除没有检测的计算类错误;因此建议始终使用它;

// SPDX-License-Identifier: MIT

pragma solidity ^0.6.0;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operati
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
好的,以下是一个简单的ERC20智能合约示例: ```solidity pragma solidity ^0.8.0; contract ERC20Token { string public name; string public symbol; uint256 public totalSupply; mapping(address => uint256) public balanceOf; mapping(address => mapping(address => uint256)) public allowance; constructor(string memory _name, string memory _symbol, uint256 _totalSupply) { name = _name; symbol = _symbol; totalSupply = _totalSupply; balanceOf[msg.sender] = _totalSupply; } function transfer(address _to, uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value, "Insufficient balance"); balanceOf[msg.sender] -= _value; balanceOf[_to] += _value; emit Transfer(msg.sender, _to, _value); return true; } function approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; emit Approval(msg.sender, _spender, _value); return true; } function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { require(balanceOf[_from] >= _value, "Insufficient balance"); require(allowance[_from][msg.sender] >= _value, "Insufficient allowance"); balanceOf[_from] -= _value; balanceOf[_to] += _value; allowance[_from][msg.sender] -= _value; emit Transfer(_from, _to, _value); return true; } event Transfer(address indexed _from, address indexed _to, uint256 _value); event Approval(address indexed _owner, address indexed _spender, uint256 _value); } ``` 该合约包括以下功能: - `name`:代币名称 - `symbol`:代币符号 - `totalSupply`:代币总量 - `balanceOf`:每个地址的代币余额 - `allowance`:允许其他地址花费的代币数量 该合约实现了ERC20标准中的以下函数: - `transfer`:从调用者的地址向另一个地址发送代币 - `approve`:允许另一个地址花费指定数量的代币 - `transferFrom`:从一个地址向另一个地址发送代币,前提是已经获得了允许 该合约还定义了两个事件: - `Transfer`:代币发送时触发 - `Approval`:允许花费代币时触发 要部署这个合约,你需要使用Solidity编译器将其编译为字节码,然后将字节码发送到以太坊网络上。你可以使用Remix或Truffle等工具来编译和部署智能合约

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wonderBlock

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值