Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
121 0 0
什么都不需要准备  [7 k: n% f& R  F0 b
*1.
1 t' }! a7 W; U/ S3 k* E' a启动网页的remix-ide即可% [5 I+ K% A. g7 b0 e
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js2 N, h' _- [5 [

6 q- s1 ?% d2 Jpragma solidity ^0.5.10;
/ Z' U+ X4 m( T5 L5 J/**
7 M2 {: a: T, L  G1 y! z7 ?. O * @title Token ERC20 implementation4 j& D+ ~4 F0 g# {  n
* @dev Simplified version - Contract allows only Transfer and Burn Tokens
: h' G0 k1 L/ B& Q1 X) o- ]7 E * @dev source: https://www.ethereum.org/token; y/ q; |6 z  v7 m; f9 Y
*/
* p7 `8 H" h; s+ ~6 z5 w2 b( B2 s' Dcontract TokenERC20 {" `0 U7 [/ Z, z4 h$ A$ a' k
    // Public variables of the token# y8 ]6 p7 f# V( F# Z! ?
    string public name;
$ ^7 l& e  ^7 n    string public symbol;: N/ j; ^2 I# J1 r4 e
    uint8 public decimals = 18;3 P7 e7 J. q& n2 H3 d
    // 18 decimals is the strongly suggested default, avoid changing it
' @# Z1 a- w* E    uint256 public totalSupply;
; {7 D+ o% r$ o; y0 w    // This creates an array with all balances
  M$ g8 z9 `& i2 ]: |/ t    mapping (address => uint256) public balanceOf;
+ @$ h9 |. v$ t' ^. T    // This generates a public event on the blockchain that will notify clients' h) B1 y4 Y! Z/ S' ?
    event Transfer(address indexed from, address indexed to, uint256 value);
) a3 P/ B4 ~1 z8 A' d" v    // This notifies clients about the amount burnt" s1 c9 V, C. c% |
    event Burn(address indexed from, uint256 value);# s( E+ h4 W1 t; N
    /**
9 I: B8 B! y- c9 z2 k- v: ~6 N     * Constructor function7 R7 I; o1 z+ [- B; `5 N$ N! t& K$ ?
     *
" Z: ]4 o3 ^, W9 O  S8 f     * Initializes contract with initial supply tokens to the creator of the contract+ ?. x- }0 C0 B& L4 S, D7 a9 I- a
     */# L+ M$ _2 K; i
    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
% J: r) n; p4 N/ @6 w& x) ^+ q        public
3 n2 W3 d9 J- K7 b! Z5 a    {, K) ~' e7 u1 s4 H% Q
        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount. l( @0 O; I7 y
        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
: U& t4 o5 O6 O* }5 n$ @0 [        name = tokenName;                                   // Set the name for display purposes
0 p1 s) J7 j! @# X1 e        symbol = tokenSymbol;                               // Set the symbol for display purposes4 o8 m$ C1 y" |/ w/ z+ s7 D" d
    }
! n* |5 y% M, U0 H7 w. e    /**
) \! v' V+ t. e( `* }: F3 N; @. i     * Internal transfer, only can be called by this contract
( ~8 C0 k! ?4 W0 V1 i" H. t     */, s. P1 h0 j$ S: E
    function _transfer(address _from, address _to, uint _value)% h+ {9 G- ?; b1 s" R, ]
        internal
" b& f& M% y! U' Q2 L" v5 c    {5 t5 x. z8 ]/ ]4 A  N* F# Z  s
        // Prevent transfer to 0x0 address. Use burn() instead/ j* H( n  N1 W4 p# c
        require(_to != address(0x0));1 @  q* v' u' N$ J* c5 F
        // Check if the sender has enough
3 S  x5 b) i; {; _        require(balanceOf[_from] >= _value);
6 ^8 O! d# m9 f        // Check for overflows) W& q. o8 D$ }& i9 b2 q; r
        require(balanceOf[_to] + _value >= balanceOf[_to]);
9 y4 k1 n0 K% h( @        // Save this for an assertion in the future
7 K- i, f) W( [' ~        uint previousBalances = balanceOf[_from] + balanceOf[_to];. |# [. ^& W( I1 h" Z% v7 y
        // Subtract from the sender* v7 z+ z6 _. `: M( V6 ^
        balanceOf[_from] -= _value;! M9 v' v( }) J  ~- c0 L" m8 p
        // Add the same to the recipient$ b  r% Q/ z# |6 m! V
        balanceOf[_to] += _value;
6 m/ J/ [4 T4 h6 ~0 Q        emit Transfer(_from, _to, _value);, |7 V" f6 ?: x
        // Asserts are used to use static analysis to find bugs in your code. They should never fail
. y& Y& P7 h! L- Y" R        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);4 d# X; l. S! X
    }
/ Y0 v! D6 n  t4 K2 w    /**
# Y7 S5 s2 B. Q' z     * Transfer tokens
) f  j/ ?; l" P0 `( ^  |& S" ]3 X     *  }7 {$ z1 N* O
     * Send `_value` tokens to `_to` from your account
! e3 W8 I% Z: W9 [0 m! F4 S( W     *
7 i  ?  O5 W2 Y! R( h9 b6 x     * @param _to The address of the recipient
6 f. |/ ?' a. B( A0 X$ K  s     * @param _value the amount to send
5 Y3 |! C2 r% e2 c5 N6 X, A2 S     */3 _' J) }: D" G0 Y
    function transfer(address _to, uint256 _value)
- R2 w' s. H$ w1 |4 b! u        public
8 U  C/ w" \$ |; V- S/ e/ K5 g        returns (bool success) , Z$ c: l' T3 N" |
    {
8 @  W9 U5 }$ c2 ~+ P- [        _transfer(msg.sender, _to, _value);
7 k( h! `4 f# u0 h6 q' c        return true;
5 M9 v1 r& Q+ M7 @    }
1 t6 j6 n! j, i7 S: c2 A( Y    /**
# Y5 H) g- {; Y0 J4 M8 a: W     * Destroy tokens5 E0 n+ ?# e6 X) c' u
     *& ^. c0 o( u+ d& p* g2 Z
     * Remove `_value` tokens from the system irreversibly
8 V$ V. @. c4 T; ]     *
+ ?! V6 ?' B! m( K     * @param _value the amount of money to burn
$ {/ L8 T3 m/ ]& S: D3 r     */
6 i7 c: a' `* i6 i4 a  x  S    function burn(uint256 _value)2 A. Z/ w0 F% j2 T7 C5 ]& v' ^5 w
        public
2 ?* W0 G8 v( c# @        returns (bool success)7 K+ ^9 C* f) `$ |
    {/ @% H- }/ a% Y
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
$ a3 o% H  Q! o        balanceOf[msg.sender] -= _value;            // Subtract from the sender$ |. {. M. P$ \6 U$ g: M
        totalSupply -= _value;                      // Updates totalSupply
' T( U$ U) w: J2 @; e3 _# l, Q        emit Burn(msg.sender, _value);+ V  V' K+ T% u; _
        return true;
5 q% }+ q. j0 ~$ u- B. d    }" Q' p! O+ g8 p( r7 g% ?
}
; \8 _6 u8 P3 T# K1 h; u部署的话
: P  u6 f9 R  Q5 ~, `+ [" v
+ y4 ^5 Z6 y7 @1 o三个参数,代币总金额,名字,代币符号(随你写了)7 y9 `9 a% ~$ |% h" B
8 Y9 \% k, d- }8 B/ G
拷贝合约地址0 X9 x6 J% e! N: j4 G

' T7 ?3 r9 v) ^  B1 V/ J( a添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1