Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
195 0 0
什么都不需要准备, E! J% G* U' g, a& i( W
*1., Z% L- p; q1 u8 _! M
启动网页的remix-ide即可. i8 Q: y- \# s. e7 G
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js! d0 U5 Z/ L0 b- ^# u& W/ @

, ^; U+ E6 z9 I0 g( |4 r4 Vpragma solidity ^0.5.10;
! e6 h# P+ U" ^  D6 o: \/**
/ ]5 \, Y+ k2 i/ S+ z * @title Token ERC20 implementation. N) s: ]2 w8 E
* @dev Simplified version - Contract allows only Transfer and Burn Tokens, M( c0 K. C- ^+ L  m
* @dev source: https://www.ethereum.org/token3 M! r, p# n' Z* [6 D
*/
% j8 z. ~( \) B6 R( }* Vcontract TokenERC20 {; ^; O) J1 Q' R$ l
    // Public variables of the token
8 e1 ~0 y/ u" |# g+ S    string public name;
' R/ f+ |( l, Y& d    string public symbol;+ {* r+ I0 N8 t0 b
    uint8 public decimals = 18;8 ]$ l7 Q' k; J2 n& k# P
    // 18 decimals is the strongly suggested default, avoid changing it
' V  f/ t5 e  C    uint256 public totalSupply;
' q# F+ M$ ?; G# e& s. l) t: E+ P2 z    // This creates an array with all balances
: D6 P% A+ H) B( V8 b    mapping (address => uint256) public balanceOf;
. U9 P3 w- E$ ?, v8 G2 F    // This generates a public event on the blockchain that will notify clients4 o! M& v$ {& s. i* ]5 Q( K/ V
    event Transfer(address indexed from, address indexed to, uint256 value);; d- s/ _1 T; r. c& N; g/ m
    // This notifies clients about the amount burnt% u1 N; `  [# j- F* b
    event Burn(address indexed from, uint256 value);
4 D+ {8 E* Y. T( }: n1 k    /**
7 y* h% C( V+ v! K; Z$ @     * Constructor function. N; M6 R( A' X2 c7 w
     *
3 m6 k: ~4 t9 E& i     * Initializes contract with initial supply tokens to the creator of the contract( V5 K/ n+ y4 T. ]" m
     */
5 o4 M6 a: O7 O( L5 M4 `  J    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)4 r5 }3 d( _7 j* n% ^
        public
9 [6 L; a) ^/ y! R% V    {$ {9 j; v- B7 \0 ^% T
        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
. n( l; ^3 X, }. A5 a        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens; o. p3 e6 z8 w+ d3 `& }7 y! \. ~
        name = tokenName;                                   // Set the name for display purposes
3 G# U2 e4 _0 G! C        symbol = tokenSymbol;                               // Set the symbol for display purposes
' R& V5 {+ Q1 ]) h& s8 s" i8 [. z/ a    }3 ?6 L0 z+ i, q) p
    /**
+ V/ J* p1 i8 b/ x) u7 C     * Internal transfer, only can be called by this contract) p1 a9 P& ?$ i& @) R& C  m
     */9 ^/ d8 s, A+ ?  {8 H- ]
    function _transfer(address _from, address _to, uint _value)2 F) `9 N0 D/ k6 o
        internal  o+ L1 i8 L, q+ z2 ]
    {( s9 p" I1 _* P
        // Prevent transfer to 0x0 address. Use burn() instead& z2 j* D/ Z, S. S! |9 `6 _
        require(_to != address(0x0));/ K) M5 h0 M5 V4 A
        // Check if the sender has enough
& a2 z+ u- g  m5 k9 R        require(balanceOf[_from] >= _value);
! A0 N) F. Q8 y% H' ?. k6 D        // Check for overflows
6 v/ [; u, u( B2 @8 F3 Y  N2 s        require(balanceOf[_to] + _value >= balanceOf[_to]);- H3 R+ Z; S% ?- N
        // Save this for an assertion in the future
! l4 H3 k- i' G0 q# _% m        uint previousBalances = balanceOf[_from] + balanceOf[_to];
/ k6 T0 W1 O; g. C2 l* O* A        // Subtract from the sender
7 _& U  q$ O+ u* y! g3 S( P: j        balanceOf[_from] -= _value;9 J% \4 h$ b9 g
        // Add the same to the recipient
9 |/ s: F4 d2 o5 U; _9 T6 O        balanceOf[_to] += _value;) F- U- g5 J- j# Z! ]
        emit Transfer(_from, _to, _value);& V4 y3 t; p$ d0 T4 J/ i- |
        // Asserts are used to use static analysis to find bugs in your code. They should never fail
0 S1 M- M5 b6 t1 V        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);" s6 j3 V7 L) ~  H' W
    }
- L# R+ {$ A$ w4 k    /**5 k% g- _* X9 l. w; M4 t# w
     * Transfer tokens
# [0 X1 B/ }5 K! l, ?     *5 [* c( k! {" k
     * Send `_value` tokens to `_to` from your account
3 h; j% w3 a6 e% Y( z* r2 X% l     *
* j4 w* ^- ]2 F8 ~- v     * @param _to The address of the recipient
- K8 S) Q- j) ]# k0 e- g# ^' s     * @param _value the amount to send
2 h! T9 S' X# l9 K; D9 g. \  v     */
  D  D$ }, k) z# i  y    function transfer(address _to, uint256 _value)
# M% f& D: I8 b: @1 G        public " ]$ q, j' \4 D
        returns (bool success)
" U& i0 x8 e! K  ?. Z- A) p    {$ v: h/ H5 z/ K' V& P' X4 j, v6 x, }
        _transfer(msg.sender, _to, _value);
1 N; z3 h$ A8 ]+ p7 {. n        return true;7 V, O* y: w9 B0 j, t( m, u
    }/ U" O* t& T" p. B% D0 m7 f
    /**" P* Q! n: k* w# r% L5 b" j
     * Destroy tokens
" R1 q2 P5 {# ~9 D1 ]     *# y# }7 g) ?9 S3 u
     * Remove `_value` tokens from the system irreversibly
! I- [9 M: `# d5 h     *8 r9 s/ m3 X* x) J7 ~# M/ c, F+ a! w
     * @param _value the amount of money to burn  Y4 V# A) y4 p+ m$ y+ B7 w
     */
: p3 ~; a8 c9 J, H4 J5 ]8 ~    function burn(uint256 _value)& Q! U& T6 }; `1 }
        public
+ {3 t; ^4 V2 q1 A- J0 k9 I        returns (bool success)
' ~. O6 X/ Y6 s  u: V    {  j; A9 C: R) l
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough4 V& {6 k; }0 ?# ^
        balanceOf[msg.sender] -= _value;            // Subtract from the sender
1 ~( L& S, w7 l        totalSupply -= _value;                      // Updates totalSupply
3 v8 Q$ e5 _) V9 t) M1 |        emit Burn(msg.sender, _value);) y9 _; Y) S3 y0 E- o
        return true;
6 q7 o9 `3 e0 c5 ]9 [7 F9 J5 V    }3 X! J  l* t, Q5 T* ?4 \
}' G( m: n* ]/ ~, ^9 U& {5 T
部署的话
* p6 `5 _! I" \4 L9 q/ w! ?+ Y2 b
$ _! v: p- ~: Z0 K, n三个参数,代币总金额,名字,代币符号(随你写了)
. v% W) h8 A" C0 U; U( O+ }0 ?' @  A0 F. p- z9 d
拷贝合约地址& d" w3 s% D1 s. I' p( J. q
+ O: u( M( p5 [8 w  t0 E; Z
添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1