Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

区块链代币ERC-20源码,如此简单

朱丹铎
163 0 0
什么都不需要准备
! l' T$ {& x. c*1.
: J. L2 \" h- h9 P, j启动网页的remix-ide即可
! _/ m/ X! K: y, dhttps://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js' D9 P  ?: r  O1 M3 K/ O+ c9 a

: f3 h" S% o7 k' u0 r4 Q' bpragma solidity ^0.5.10;8 H/ W$ ?$ F- d6 M- r- ^* P
/**. e* R' O; M2 D3 K$ F3 ^9 N5 B0 v
* @title Token ERC20 implementation! Q9 i- c3 m, G* u2 G$ R  _
* @dev Simplified version - Contract allows only Transfer and Burn Tokens
* I/ D+ D& d0 e2 G- l+ t: ]. Y * @dev source: https://www.ethereum.org/token0 _: j3 m3 C8 X* y+ S+ N
*/
7 A/ u; K7 ~* R* {+ ]; L, z  fcontract TokenERC20 {9 J. g# r3 l& c0 [0 x5 |. g4 ]
    // Public variables of the token
. Q5 Z' @# V, u& a/ z+ T! W( e    string public name;
! r# p  S6 H  C/ ], @& ~    string public symbol;9 ]9 O8 T3 `0 F9 n3 p
    uint8 public decimals = 18;
8 y% s& C9 o$ V7 @4 D3 q2 y8 g: F& {    // 18 decimals is the strongly suggested default, avoid changing it
# S/ K+ w0 v/ @+ H    uint256 public totalSupply;# O' \, x2 o& a; f4 X
    // This creates an array with all balances( a! k/ g6 S& {- e1 p% b/ P7 ~) Y8 w
    mapping (address => uint256) public balanceOf;
9 X4 N) \- A$ {) Q9 a: d    // This generates a public event on the blockchain that will notify clients! h( _" P- b/ \! A! c
    event Transfer(address indexed from, address indexed to, uint256 value);5 u8 V9 i2 f; T/ N. d- c- D$ H
    // This notifies clients about the amount burnt% l0 n( i2 C' y$ ]5 [, a
    event Burn(address indexed from, uint256 value);
7 U% z8 j2 I) E0 i; Q9 X2 b    /**; m! g5 m( T8 k$ J
     * Constructor function: m0 g) X8 n) ^9 F
     *
2 i# l. c0 ~0 c+ Z% a     * Initializes contract with initial supply tokens to the creator of the contract
. N0 h! m3 B6 P2 _     */
% P" y2 G7 U7 q2 B( w! z! ?! m3 B    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)* C, ~- `/ A( I$ Y
        public
( D7 z/ a& e; A, l  i    {7 K  f3 j1 {8 U1 b8 S9 Y/ `
        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
. ^) w9 r* V' C: m: S9 i0 ^        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens) G1 i4 G* K6 v/ l; n6 `
        name = tokenName;                                   // Set the name for display purposes; f. b" N: s% b0 k( m, X
        symbol = tokenSymbol;                               // Set the symbol for display purposes
/ m, O! X# |5 F; ~7 s. b    }
2 I8 U' _3 }/ H    /**/ M6 _0 ^0 }+ M$ L/ k2 ]
     * Internal transfer, only can be called by this contract
: j  x7 _% D' S" @. W4 X     */
5 a6 o" `# V* m$ t  z    function _transfer(address _from, address _to, uint _value)
5 S. [0 P# ^5 B( d" v2 W( k7 \; o        internal2 Z$ R$ V) w  ?" ]( H
    {
3 x' o4 f2 x0 w6 c/ B% ]) E        // Prevent transfer to 0x0 address. Use burn() instead
# s. U+ c! _1 q, c/ i2 @        require(_to != address(0x0));" f  B& S2 q; D8 g. _
        // Check if the sender has enough9 z# [* U2 ]4 `" ~1 Q' v' p0 u8 g! g
        require(balanceOf[_from] >= _value);  l$ _6 M- }# o# o' q
        // Check for overflows
' d7 t. h( H* o        require(balanceOf[_to] + _value >= balanceOf[_to]);
+ A- {9 X7 w4 e8 x5 }9 F# L) z        // Save this for an assertion in the future
4 }3 C+ N! G  w& g/ l3 S        uint previousBalances = balanceOf[_from] + balanceOf[_to];9 v: o+ Z$ w+ h  W! N$ Z" f
        // Subtract from the sender3 U7 Y1 R  u' Z# q2 X
        balanceOf[_from] -= _value;
" l# P) i; p3 c  d5 {! g4 O0 ]        // Add the same to the recipient
# t( y# m: l0 w        balanceOf[_to] += _value;
  k2 ~5 x: \" v- ^        emit Transfer(_from, _to, _value);
( m& X0 P4 u3 L" ^; ~9 Z) x        // Asserts are used to use static analysis to find bugs in your code. They should never fail, N, w% }4 h; [# Z& z- Z0 `
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);3 s6 V7 d3 r3 Y. H
    }, O' q* P8 L% F$ x5 g" U
    /**' l4 {  C4 ]6 r+ N8 [
     * Transfer tokens
1 Q  H, u5 t: @2 F6 l6 a     *
" A& g2 K# V) M* [     * Send `_value` tokens to `_to` from your account! K$ Z; [, c5 Q
     *5 D9 K. D4 ?1 h' {" Q
     * @param _to The address of the recipient5 A! G; X; Z0 h: t& Y8 @) I# I" n
     * @param _value the amount to send
: i; _! t8 u& T" j     */
- t9 ~% O3 \, D/ o5 i    function transfer(address _to, uint256 _value)( e2 J, M4 r$ [
        public
# a6 j  A$ z1 G        returns (bool success) ' \/ U  \+ h- @" X+ `/ Y
    {0 M; a: c$ i( j9 V6 O. b6 M. J# M
        _transfer(msg.sender, _to, _value);3 t$ b& @0 g& F0 ]- U9 w' k3 t8 P; ^
        return true;: g1 \4 h2 x7 I3 e, D  r* t
    }/ S3 m, J9 k, x! ]3 R
    /**# C& o/ B* O5 g" E
     * Destroy tokens/ ~  v- Y. `# y2 {: r& o% N
     *
4 {, b, ]( s# X. o) A( H5 u     * Remove `_value` tokens from the system irreversibly0 H2 s1 _  _" }7 f& w
     *
& i/ }' z1 D9 F8 J     * @param _value the amount of money to burn
5 ]. K$ Z( v+ F) z5 r1 n% a3 J     */
) J6 A) l' T  s/ L- C3 Y3 X0 }. l  Z    function burn(uint256 _value)3 w( i. G7 G3 E1 |$ w
        public6 J2 y( U; ~9 ]6 G1 p( c9 H4 B
        returns (bool success)$ W; ^: ?  }1 ]9 e  K
    {9 o8 P; ]# T' k
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough- W9 j; F9 J, {' B
        balanceOf[msg.sender] -= _value;            // Subtract from the sender
/ S) @. J3 C+ j6 ]1 S, r( C  L        totalSupply -= _value;                      // Updates totalSupply
+ v/ `# N6 D% n: |2 E        emit Burn(msg.sender, _value);
) f$ X4 h  L9 F5 q1 l, ?5 E        return true;# M, ]+ H  w' s8 F
    }
) l5 M. i) y. D. Y7 s+ R$ K}
6 B" d8 s+ d3 f  T% C部署的话
% l: Q8 k& @  @6 A
4 m$ g! C: g% T8 E  R  M三个参数,代币总金额,名字,代币符号(随你写了)' q( Q/ i0 M+ H2 M4 f: [6 e2 _
8 t' X+ I( W. C; M; m$ `' g
拷贝合约地址
# r& C; _9 U! r
/ @4 O# V- a3 q' e) ^9 U添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1