温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

solidity智能合约[55]-token

发布时间:2020-07-02 11:51:29 阅读:510 作者:jonson_jackson 栏目:开发技术
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

数字货币

货币是用来交换、偿还债务的媒介。古代货币为金、银、贝壳等实物。现代中央银行发行的纸币等。相对于古代的一般等价物而言
现在的货币本质上是由政府信用的背书。其本身并没有价值。 同理、比特币与以太币本身并没有价值,但是依托于区块链网络的特性,使得其拥有货币的完美属性。包括:
·便携
·耐用
·可分割
·可识别
·可替代
·稀缺且难以仿冒

数字货币引入

如下是一段简单的代币代码。balanceOf映射表存储金额。构造函数,定义了初始化发行与管理者。 transfer函数定义了转账操作。完成了货币存储与转移的功能。 本质上,此货币就是存储在balanceOf映射表中的数字。数字本身并没有价值,只有当你认为他有价值的时候,它才会有价值。所以,数字货币的背后,常常是对应与一定的商业价值。例如公司的股权等。

1234567891011121314151617
pragma solidity ^0.4.23;contract tokenDemo{    mapping(address=>uint) public balanceOf;    address owner;    constructor(uint initSupply) public {        balanceOf[msg.sender] = initSupply;        owner = msg.sender;    }    function transfer(address _to,uint _value) public {        balanceOf[owner] -= _value;         balanceOf[_to] += _value;    }}

一般代币合约的缺陷

如上面写好的简单代币demo,有三种缺陷。
1、没有权限的控制,任何人都可以调用transfer进行转账
2、没有防止溢出***
3、功能有限
4、没有统一的规范。

想象一下,如果每一个人或企业都写一个属于自己的代币合约。每一个合约都有自己的查询资金的函数名。都有自己的转账的函数名。那么每一次当我们需要用到其他人的代币,都需要查询,此代币合约中,每一个函数的功能。大大降低了效率。
这就为我们引出了ERC20代币。

ERC20协议

ERC是以太坊征求意见( Ethereum Request for Comment-20)的缩写. 20代表它的序号。其规范了代币合约,也就意味着,一旦合约满足了ERC20代币的规范,那么其必然有规范的函数标准。如下,就是ERC20代币协议规定的不同的函数及其功能。ERC20协议只是定义了函数的定义与功能,需要代币设计者自定义的实现函数功能。

1234567891011121314151617
interface ERC20Interface {     //总发行数量    function totalSupply()  external returns (uint);    //查询数量    function balanceOf(address tokenOwner)  external returns (uint balance);    //查询授权数量    function allowance(address tokenOwner, address spender)  external returns (uint remaining);     //转账    function transfer(address to, uint tokens) external  returns (bool success);    //授权    function approve(address spender, uint tokens) external returns (bool success);    //授权转账    function transferFrom(address from, address to, uint tokens) external returns (bool success);    event Transfer(address indexed from, address indexed to, uint tokens);    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);}

ERC20代币实现

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
pragma solidity ^0.4.18;// ----------------------------------------------------------------------------// '' token contract//// 部署地址 :// 标志      : LOVE// 名字    :  LOVE TOKEN// 总供应量 100000000000000000000000000// 精度    : 18// ---------------------------------------------------------------------------// ----------------------------------------------------------------------------// SafeMath安全库// ----------------------------------------------------------------------------contract SafeMath {    function safeAdd(uint a, uint b) public pure returns (uint c) {        c = a + b;        require(c >= a);    }    function safeSub(uint a, uint b) public pure returns (uint c) {        require(b <= a);        c = a - b;    }    function safeMul(uint a, uint b) public pure returns (uint c) {        c = a * b;        require(a == 0 || c / a == b);    }    function safeDiv(uint a, uint b) public pure returns (uint c) {        require(b > 0);        c = a / b;    }}// ----------------------------------------------------------------------------// ERC20 代币标准// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md// ----------------------------------------------------------------------------contract ERC20Interface {    //总发行数量    function totalSupply() public constant returns (uint);    //查询数量    function balanceOf(address tokenOwner) public constant returns (uint balance);    //查询授权数量    function allowance(address tokenOwner, address spender) public constant returns (uint remaining);     //转账    function transfer(address to, uint tokens) public returns (bool success);    //授权    function approve(address spender, uint tokens) public returns (bool success);    //授权转账    function transferFrom(address from, address to, uint tokens) public returns (bool success);    event Transfer(address indexed from, address indexed to, uint tokens);    event Approval(address indexed tokenOwner, address indexed spender, uint tokens);}// ----------------------------------------------------------------------------// 所有者合约// ----------------------------------------------------------------------------contract Owned {    address public owner;    address public newOwner;    event OwnershipTransferred(address indexed _from, address indexed _to);    function Owned() public {        owner = msg.sender;    }    modifier onlyOwner {        require(msg.sender == owner);        _;    }    function transferOwnership(address _newOwner) public onlyOwner {        newOwner = _newOwner;    }    function acceptOwnership() public {        require(msg.sender == newOwner);        OwnershipTransferred(owner, newOwner);        owner = newOwner;        newOwner = address(0);    }}// ----------------------------------------------------------------------------// ERC20代币,增加标志、名字、精度// 代币转移// ----------------------------------------------------------------------------contract LOVEToken is ERC20Interface, Owned, SafeMath {    string public symbol;    string public  name;    uint8 public decimals;    uint public _totalSupply;    mapping(address => uint) balances;    mapping(address => mapping(address => uint)) allowed;    // ------------------------------------------------------------------------    // 构造函数    // ------------------------------------------------------------------------    function LOVEToken() public {        symbol = "LOVER";        name = "LOVER Token";        decimals = 18;        _totalSupply = 100000000000000000000000000;        balances[0x6AFe57C1F589C4744ab9FF4ac8899080695a6f5e] = _totalSupply;        Transfer(address(0), 0x6AFe57C1F589C4744ab9FF4ac8899080695a6f5e, _totalSupply);    }    // ------------------------------------------------------------------------    // 总供应量    // ------------------------------------------------------------------------    function totalSupply() public constant returns (uint) {        return _totalSupply  - balances[address(0)];    }    // ------------------------------------------------------------------------    // 得到资金的数量    // ------------------------------------------------------------------------    function balanceOf(address tokenOwner) public constant returns (uint balance) {        return balances[tokenOwner];    }    // ------------------------------------------------------------------------    // 转账从代币拥有者的账户到其他账户    // - 所有者的账户必须有充足的资金去转账    // - 0值的转账也是被允许的    // ------------------------------------------------------------------------    function transfer(address to, uint tokens) public returns (bool success) {        balances[msg.sender] = safeSub(balances[msg.sender], tokens);        balances[to] = safeAdd(balances[to], tokens);        Transfer(msg.sender, to, tokens);        return true;    }    // ------------------------------------------------------------------------    // 授权    // ------------------------------------------------------------------------    function approve(address spender, uint tokens) public returns (bool success) {        allowed[msg.sender][spender] = tokens;        Approval(msg.sender, spender, tokens);        return true;    }    // ------------------------------------------------------------------------    // 和approve连接在一起    //    // The calling account must already have sufficient tokens approve(...)-d    // for spending from the from account and    // - From account must have sufficient balance to transfer    // - Spender must have sufficient allowance to transfer    // - 0 value transfers are allowed    // ------------------------------------------------------------------------    function transferFrom(address from, address to, uint tokens) public returns (bool success) {        balances[from] = safeSub(balances[from], tokens);        allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);        balances[to] = safeAdd(balances[to], tokens);        Transfer(from, to, tokens);        return true;    }    // ------------------------------------------------------------------------    // 返回授权数量    // ------------------------------------------------------------------------    function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {        return allowed[tokenOwner][spender];    }    // ------------------------------------------------------------------------    // 合约不接受以太币    // ------------------------------------------------------------------------    function () public payable {        revert();    }       // ------------------------------------------------------------------------      // Owner can transfer out any accidentally sent ERC20 tokens      //所有者能够转移任何ERC20代币的接口     // ------------------------------------------------------------------------    function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {        return ERC20Interface(tokenAddress).transfer(owner, tokens);    }}
  • 本文链接: https://dreamerjonson.com/2018/11/26/solidity-55-token/

  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处!

solidity智能合约[55]-token

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×