Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
115 0 0
什么都不需要准备
$ z1 c4 S) i6 q+ P  c4 l; L*1.
. k7 a0 I9 h5 |2 U  J* r8 ^) ?启动网页的remix-ide即可* n% \) e& {' @# m1 n& d' P
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js2 S- B9 B% ]& q3 x1 ^# @% o

; ]: {: a& m/ X: _( `pragma solidity ^0.5.10;3 X) Q$ s0 u% s
/**
1 ?" U" d6 c8 l$ L  v* o * @title Token ERC20 implementation
  B, s0 ?+ _! ]) ] * @dev Simplified version - Contract allows only Transfer and Burn Tokens% J) V+ H# U# S. ]
* @dev source: https://www.ethereum.org/token2 l' k$ v% h7 N
*/( H$ n$ T- f  J/ d2 T" M1 m7 R
contract TokenERC20 {6 H: o4 q- u1 Y( x5 z
    // Public variables of the token
  u" i0 @+ F' y* c' p    string public name;
2 O6 j- o; n6 i    string public symbol;
9 |8 C. m3 E  @; }5 P( b    uint8 public decimals = 18;. `8 i- U, @( G4 z' S
    // 18 decimals is the strongly suggested default, avoid changing it. t( w; J2 W% X1 O
    uint256 public totalSupply;
! |5 P4 f( J  M, v# m$ Z' v    // This creates an array with all balances$ J. R! r! P+ ]+ k
    mapping (address => uint256) public balanceOf;
7 u% t" y1 t  A" i    // This generates a public event on the blockchain that will notify clients
. h8 i/ T% s- z    event Transfer(address indexed from, address indexed to, uint256 value);
2 Q6 j( H0 J8 x, J/ F* P0 d    // This notifies clients about the amount burnt
) F2 w9 B( o4 |. V; Z  C    event Burn(address indexed from, uint256 value);$ V* z, t" J6 c
    /**5 H# P% i' ^- G
     * Constructor function# C! N8 {; P$ y  G& L
     *
8 |* s. C8 x5 i9 L2 i( G0 E  W     * Initializes contract with initial supply tokens to the creator of the contract! W, _% p9 w- B2 }0 w( _! O/ t
     */
: D6 Z; c- C: T! B6 Y) C    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)7 x2 ^4 K0 m. f; e0 c2 D
        public
9 |( O; r. @8 W, G( {1 v! e    {
- x- O# `4 \: }5 ]! h7 `' ]        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount% I7 [; {& V) n+ b1 [6 K
        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
9 F2 ]5 k% M1 d& ?4 }7 G; P3 ]        name = tokenName;                                   // Set the name for display purposes$ ^' N: k. O  a4 x5 T
        symbol = tokenSymbol;                               // Set the symbol for display purposes9 ]# X7 T( v. Q' C4 W/ B
    }( }& Z/ [/ ~8 p) ]- i9 L& z4 _
    /**& w: H6 B! c  ^( s
     * Internal transfer, only can be called by this contract1 T' |& g/ L' o; N% G9 E9 S
     */
1 J1 I7 y( y$ S, B* }    function _transfer(address _from, address _to, uint _value)2 s1 _6 o/ m% ~" C- d
        internal
3 f1 L9 K; X3 X  k    {
* o( u; r) ?3 j' X6 I        // Prevent transfer to 0x0 address. Use burn() instead7 j2 d, ]1 m& j3 a* |
        require(_to != address(0x0));
7 v0 c& d$ ?8 D' [- P9 m  D        // Check if the sender has enough. _6 N) O( i6 O. k9 P
        require(balanceOf[_from] >= _value);
1 K; l* e/ b) a1 }7 I5 |4 m        // Check for overflows
$ ^6 y/ v2 T! f        require(balanceOf[_to] + _value >= balanceOf[_to]);6 L. C- o' P% Q* u6 W( V: h
        // Save this for an assertion in the future( ^1 N4 N; ~& c7 ^) G" }
        uint previousBalances = balanceOf[_from] + balanceOf[_to];  g. F% J- W; w9 T# H
        // Subtract from the sender( {: B( f9 E6 T7 v4 f" p
        balanceOf[_from] -= _value;$ {6 g: s4 H9 O8 ]
        // Add the same to the recipient
* @) }. @# v1 N+ `' k7 u        balanceOf[_to] += _value;
. W; d3 [' ^' p  [' O        emit Transfer(_from, _to, _value);
& R0 u* x7 o) _/ q7 L        // Asserts are used to use static analysis to find bugs in your code. They should never fail4 B4 A0 O* v3 w* _+ G0 i% X7 L
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
0 H' |6 \. c/ w  E, V    }$ o9 G* ^$ |1 A
    /**9 b3 \, R7 a. _& e
     * Transfer tokens
- Z9 t8 ]" A( e* m: k  w! O     *! ?( k  K$ O$ k
     * Send `_value` tokens to `_to` from your account1 b$ g$ p4 q* T7 d
     *+ u6 ~$ |" W6 X  C6 W
     * @param _to The address of the recipient
: a7 j, U2 C# h$ m7 W3 a5 ~     * @param _value the amount to send, Y7 S6 t7 a7 ^/ n! h3 @: i
     */0 ^, H2 W2 H0 U2 D1 V: o* K
    function transfer(address _to, uint256 _value)% W/ L1 T2 S' |$ u  Y/ Z7 H! G: {
        public ! R% ^4 P  F- s7 Z" n
        returns (bool success)
/ Y% G- g' }$ ~5 ]    {
4 [- k' U; v; c, h        _transfer(msg.sender, _to, _value);
* X+ o  t( y7 i1 O        return true;3 R! |& t7 n! f+ L/ O! J
    }4 _$ s* d: L* J- T/ l
    /**
, g" X6 ]- r% |, t: u& K     * Destroy tokens; V6 K" B% q( J
     *0 `# \, ?3 m% s6 c. {/ p3 T
     * Remove `_value` tokens from the system irreversibly
. b( E1 l* `3 `8 ^9 L, [- _" j9 h     *
- c; c9 h( @5 G+ \4 l6 M     * @param _value the amount of money to burn
) M; s# j, _  z- ?! i1 c     */* U9 o3 T- e5 q
    function burn(uint256 _value)
2 z7 z, ?( A. _4 ~        public
7 v7 V. F0 K0 i1 w# u" L# C        returns (bool success)* P# p( R, J) E5 A; ~2 m9 D
    {
% w$ `6 ?) g. G2 h        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
* {! N0 E, S9 r6 r2 s# z        balanceOf[msg.sender] -= _value;            // Subtract from the sender
1 G. T; I. V% t2 [3 D( K        totalSupply -= _value;                      // Updates totalSupply
; \9 S, M8 w$ ]4 j; L( Z        emit Burn(msg.sender, _value);
+ W- @; Z: d: }, y) b5 M$ h        return true;, c8 u2 s  i1 o7 x( S8 J5 D+ z! l
    }- b+ D5 s( L& s. Z. j
}) n$ r( f! {' e. b" X1 r
部署的话4 Q0 u, _  L* z# @( W7 T

& Z1 C: ~3 U+ j. o& v. v三个参数,代币总金额,名字,代币符号(随你写了)
; h, n& t+ w! p2 H+ o( \* j
! h/ G7 ^/ c# D4 C& H拷贝合约地址
9 v2 U4 ?) a2 d0 t5 w0 b9 K* g6 z7 c0 O/ R
添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1