Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
119 0 0
什么都不需要准备9 V+ {9 _! \3 L' a" O+ A
*1.
+ Z6 v" E" I* j: S$ M启动网页的remix-ide即可6 l* m5 j& ]" @
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js  |' ?; h+ q0 k# l! |& J# y! N9 x/ i9 u

0 z+ @; }8 b2 A. o1 ?# npragma solidity ^0.5.10;
' c- Y4 l% H0 p6 n/**
6 l2 G5 g( i3 g * @title Token ERC20 implementation7 f3 C# V4 z5 d5 b6 D$ u# N
* @dev Simplified version - Contract allows only Transfer and Burn Tokens; z2 X# l/ I+ n/ S9 ~
* @dev source: https://www.ethereum.org/token
- Q+ `1 Z5 H5 Q! \( y */
1 I& j* B/ T- x7 G  hcontract TokenERC20 {) X' Z1 v6 c& C: o8 f) z, X% I: e
    // Public variables of the token
  ?8 V1 b# e! t& P/ w    string public name;$ _" z# h$ G- {* X9 F  Q$ d& S
    string public symbol;
, t5 {- k" l/ X2 |. H' N    uint8 public decimals = 18;
; n+ f" {; }& S7 D- s; d    // 18 decimals is the strongly suggested default, avoid changing it
+ x5 T/ D+ _1 X: j% K    uint256 public totalSupply;
5 m8 l& W9 Z1 X    // This creates an array with all balances! ~( z% H3 d- x/ {5 l. Y4 j9 T
    mapping (address => uint256) public balanceOf;
/ \8 }0 q4 t  H: N. m! i    // This generates a public event on the blockchain that will notify clients
3 N" E& D7 L8 W3 @- @    event Transfer(address indexed from, address indexed to, uint256 value);% a+ [1 t2 C' j+ A$ g/ G# ]" Y
    // This notifies clients about the amount burnt
' m) a$ V4 d) U    event Burn(address indexed from, uint256 value);
/ ?- K4 Q6 L- R8 }# }) Q    /**
, |+ J! M; e/ O# K2 P" d     * Constructor function
$ o; V0 d- z1 n& c) h     *
, [( C8 d4 ~1 T2 Z% T     * Initializes contract with initial supply tokens to the creator of the contract
/ z" o% R0 K" {+ ], l: Y     */. a2 S3 R' w9 i
    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
; X3 z( x6 L9 R3 v; Q( S+ [; |, X' u        public: B0 w8 v6 ^5 i
    {
! c0 [  P" u+ N3 e0 t5 f# T        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
3 w* c! a' P3 K1 `1 x% D4 T, O        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
) K. }! W$ c7 y; a" x        name = tokenName;                                   // Set the name for display purposes
$ q; ?; V2 \4 o0 F6 C; y5 B        symbol = tokenSymbol;                               // Set the symbol for display purposes5 @5 |! z& F' N! s! [6 q- l/ v7 W
    }
* [0 t/ _7 X  [3 A2 Y3 U# D- }! c    /**
( ], U2 H) m# g; I1 M8 p) j- o     * Internal transfer, only can be called by this contract
: s" c/ [" X$ V! P7 a5 ?3 |     */5 A) n3 _9 ]+ l; m+ c
    function _transfer(address _from, address _to, uint _value)
* s2 }  M' a  y9 q5 U* H* }        internal
# g2 F  ~" {% |( \1 o8 I. U/ |  p    {
' `* A- o: a. e: A4 p. b- h/ U; d        // Prevent transfer to 0x0 address. Use burn() instead( D3 V# S  f) Z
        require(_to != address(0x0));* }4 m+ r: F# H; {9 L+ m
        // Check if the sender has enough
2 y4 m0 b3 @: t. E! H' n        require(balanceOf[_from] >= _value);5 ?! g) X0 L! l* M) [) G# T; g
        // Check for overflows1 w0 F4 ~" M3 X" `+ Y
        require(balanceOf[_to] + _value >= balanceOf[_to]);; o) m1 ~8 t7 U' j% W
        // Save this for an assertion in the future3 V* F2 f" r1 e: J* p1 _; a) }
        uint previousBalances = balanceOf[_from] + balanceOf[_to];( |* l  F# \" R( U
        // Subtract from the sender
5 X1 N. x6 z% v8 d; H        balanceOf[_from] -= _value;+ w5 ^* r8 v/ D+ }# k
        // Add the same to the recipient
; m: X+ a. S3 T( \$ ^8 o        balanceOf[_to] += _value;8 [* G) u; d8 v4 F* Y  @
        emit Transfer(_from, _to, _value);' _# G' E4 I9 C$ T; T
        // Asserts are used to use static analysis to find bugs in your code. They should never fail
& `. G7 q2 d) F9 b2 x( }* [        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);/ y& i" O: [7 W) d2 |
    }
0 a9 C" q! `7 v    /**
1 E3 D* P! m7 h3 L( a* v! F- v     * Transfer tokens
" M3 r* m! d; B& I1 y4 w! c, P  {     *; V' ?& i8 W$ c. i
     * Send `_value` tokens to `_to` from your account" J5 e: t% W# {1 n2 S
     *
( \+ _. B. @% K' R+ ~7 s     * @param _to The address of the recipient
9 J; `; C; J- @8 ?0 ?" F" p     * @param _value the amount to send6 E9 f. ], J, d/ @  g
     */
7 E$ D' @/ R( H+ _9 n    function transfer(address _to, uint256 _value)
% l0 d" x) K( b1 e        public 6 I  M8 c2 Q# w6 W4 v2 U6 p
        returns (bool success) 3 n  j4 {7 K2 e; I' n: k
    {
; w  p% d1 t0 c  m        _transfer(msg.sender, _to, _value);
' N; X* U+ J$ _" W) F8 B1 s        return true;
( L8 h) j7 V% B5 Z. I    }
5 u- P( Y2 K; j; E- U  G1 d9 y    /**' ?! S5 e7 p2 O  i7 M  K. ~4 ?5 O
     * Destroy tokens
  ~. J- ~, a# A3 @3 f     *
1 t1 G0 v( t$ B; a9 @- B) c     * Remove `_value` tokens from the system irreversibly+ J5 l0 p, K8 ~0 v  N& i5 J
     *! O( K8 l! f* F7 j
     * @param _value the amount of money to burn! `0 Z$ o7 U% n/ n; l
     */5 I8 d  t: o$ s
    function burn(uint256 _value)- S1 \6 s- ?4 b+ e9 d+ K
        public
# A8 E; t. @& Z* p- R+ V& i- h        returns (bool success)7 A! P) K% f  N9 c9 K4 M
    {8 G$ K, `& _. X; |( A* [
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough4 H8 J* u& x: d4 J4 f+ P2 t4 A$ ^$ J
        balanceOf[msg.sender] -= _value;            // Subtract from the sender7 S& }+ v( }. B, i6 F1 l. |
        totalSupply -= _value;                      // Updates totalSupply
+ F/ D7 {& P% S) r% U& v        emit Burn(msg.sender, _value);
9 n& N+ h2 [$ y; h        return true;
+ y. Z6 ^6 p. ^    }( v( f1 f! C2 O) C; b
}
/ t) ^- S5 x. ?部署的话9 m% X) z( a6 ^; T+ ^% Q: R
7 z% e' z0 d! |. D8 k  R
三个参数,代币总金额,名字,代币符号(随你写了)
2 i/ z4 Q$ j! w% Z, u  k! _& @/ F# X* l: ~, y3 ^: S7 o+ n
拷贝合约地址8 f- t4 B0 }' p0 n

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

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1