Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
105 0 0
什么都不需要准备
* \) M$ R- e: U8 C( m$ |*1.
8 }) Y" a/ D* f" C启动网页的remix-ide即可* |( y3 f1 g; [- K
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
6 D- H" a" x/ O9 j* |1 g: M: k
* i0 ?4 ~0 J7 V! d7 upragma solidity ^0.5.10;$ L* X9 I$ h/ ^$ j: q1 D' n
/**$ [, `4 ]8 E3 q9 F/ {  `% h
* @title Token ERC20 implementation
6 \; M/ D2 s, C * @dev Simplified version - Contract allows only Transfer and Burn Tokens* b5 s7 N3 ]1 t& ?( j- ^
* @dev source: https://www.ethereum.org/token
1 E. N: q# c0 h1 D */
- f& v5 C+ M! ^2 J  A$ kcontract TokenERC20 {4 T/ Y% @/ f$ \- F  b4 t
    // Public variables of the token; k5 z$ g& h+ B2 l/ I- A- t
    string public name;  t- q/ q( I3 f: n2 m
    string public symbol;
* ?' a$ X0 e0 Y% T, s    uint8 public decimals = 18;
8 O% Z9 T. g  ^: q* ?    // 18 decimals is the strongly suggested default, avoid changing it
4 \6 d- i- u0 ?- J! C) k    uint256 public totalSupply;& v3 S9 K+ [4 I
    // This creates an array with all balances
. z2 D$ P, k- x; [    mapping (address => uint256) public balanceOf;( m) m6 k7 Q( G9 D: q. N0 @
    // This generates a public event on the blockchain that will notify clients& y& `# F. P) i+ y3 E
    event Transfer(address indexed from, address indexed to, uint256 value);6 b4 V4 i2 L# N% @. }
    // This notifies clients about the amount burnt
4 \7 h* h* K  C    event Burn(address indexed from, uint256 value);0 B/ m8 R+ e0 s+ |
    /**, {0 L. x1 r$ a
     * Constructor function& A' s/ ?. X3 R
     *
% P. W0 E& E' l  E) G, P* i     * Initializes contract with initial supply tokens to the creator of the contract
& `$ M& o  t+ }* c6 p     */
9 Y8 s! R1 E! n, L+ ]5 n    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)/ f( ~. S+ i5 y9 f0 b  U, T
        public9 l' e+ B/ b/ x! \$ Y
    {2 T- j3 T6 p# B5 ]  @  C6 k/ e
        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
& R! O) d+ Q2 A& z) r; t        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens" \6 E) O& X$ a# f1 A
        name = tokenName;                                   // Set the name for display purposes$ h3 A9 |) o& `1 P
        symbol = tokenSymbol;                               // Set the symbol for display purposes
- t7 |) d; k2 [% p! l7 W    }
" j, H; q8 X' T0 l/ P2 a. v    /**
' n7 W# X# }+ b) \# w: n8 s     * Internal transfer, only can be called by this contract& l9 k% S6 H7 j: A  }. v
     */& ]( x  }9 L2 q+ ~  E
    function _transfer(address _from, address _to, uint _value)0 c8 l1 v7 Y) _1 w& F$ l+ k# [6 \8 w
        internal& N- l( y4 `" G- h
    {/ R& {2 W' V7 n$ }1 V2 _  @
        // Prevent transfer to 0x0 address. Use burn() instead4 r* f9 U4 Q- ]. h
        require(_to != address(0x0));1 f) {( K- k! c" x
        // Check if the sender has enough8 \$ I: @+ G+ b) t  L
        require(balanceOf[_from] >= _value);" X8 D2 ?  T/ I# t) ?/ H
        // Check for overflows$ F4 z* O# T# w6 Q, T4 n, J( ?  H
        require(balanceOf[_to] + _value >= balanceOf[_to]);
. p3 V0 e8 a. q4 w        // Save this for an assertion in the future6 b3 T$ A7 [1 R. X# Z
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
& P) y- L  q& O. H: D4 f        // Subtract from the sender
; t+ o  c+ e. w, ]0 _        balanceOf[_from] -= _value;, J$ o1 S; \) y6 j- t* D2 c
        // Add the same to the recipient% V4 H5 b" O: ?0 ~% A& O2 k
        balanceOf[_to] += _value;
2 G) ~- j8 j9 l2 W& r; x! k2 w        emit Transfer(_from, _to, _value);
# f8 e# O: I- a5 g1 w        // Asserts are used to use static analysis to find bugs in your code. They should never fail
5 l" m7 e* L, ^        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
! ]4 q2 I( l: D" `    }7 v. M) n7 d: V1 _. j; g
    /**) T% H) q; E' X! U8 k) V5 S1 c
     * Transfer tokens) K7 @, A- o) F3 V
     *2 G" z4 r/ r' e+ V! I& C$ W
     * Send `_value` tokens to `_to` from your account
9 y/ ?% j( N2 |) Y) t8 F2 |     *
( z# d( X% R/ @$ B8 Z     * @param _to The address of the recipient
: |% A- S9 _  z3 q; F0 M$ l% d     * @param _value the amount to send
; p: ]9 F/ L: q+ J     */
# [4 I7 i1 ^; o& ]8 x6 j% `* W* N    function transfer(address _to, uint256 _value)2 r' z, l* n/ X8 Z; Y
        public
( p2 x0 ~$ G7 q$ m        returns (bool success)
( e( p' X: w( G, `: O    {4 D/ Z9 b' g) o/ Z5 S
        _transfer(msg.sender, _to, _value);7 f5 |+ N: q& ^- d  e4 |% v
        return true;# ]8 L& }+ I- \9 {  C5 q2 O9 c
    }' \" v6 ^- Z0 j
    /**: R$ u+ n8 Z* g! }* W& G
     * Destroy tokens8 Z2 d$ N& y0 z7 e3 d' \' w
     *
6 U9 y* w; f- R" q     * Remove `_value` tokens from the system irreversibly4 [  D' p( h; g! Z- f
     *
2 {5 a9 L: I% a4 Q2 S' C% f6 \     * @param _value the amount of money to burn
( k, l5 i1 ]3 z0 p# e/ t     */. n) V- }$ \6 z4 C8 b
    function burn(uint256 _value); V/ S, S0 z) p: g. F  l
        public
+ C1 q9 s* I6 J  u        returns (bool success)
( l2 g) v) |4 k6 i5 o0 w    {
8 m8 F  M6 t; `3 J0 S# o4 F# q        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
* C7 g& X+ G1 d- l        balanceOf[msg.sender] -= _value;            // Subtract from the sender
0 v& _$ x5 ]7 ^+ e9 ^. B' s3 ^* I        totalSupply -= _value;                      // Updates totalSupply
) K: e% l! Y6 z$ C2 E        emit Burn(msg.sender, _value);" \% O& }6 f0 @
        return true;
. `: Q% y7 _' S9 j1 k3 F    }
6 p, j$ [8 p: \# N6 m% _9 i}
' I. [* _5 ?  e部署的话
+ |1 U* O- N+ U" E7 t+ p/ j8 Z( E  g4 Q
三个参数,代币总金额,名字,代币符号(随你写了)' O) r) e* r/ Z7 z3 J
, f* A7 ?& y* z/ _' _2 [$ Q
拷贝合约地址
2 ^7 B- ^* @: l, T
" @- K  |) [6 X$ }4 n8 ]& j% R  s: _, `添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1