Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
194 0 0
什么都不需要准备
( K& h' {: h& |' z*1.; a. \, ?3 a( j8 v) n' Y* ]
启动网页的remix-ide即可
; A; d( w, k/ k- N0 Nhttps://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
% p4 f/ z" o* m1 y2 K/ _" U7 {; p" A+ h0 _& X- t* n/ `
pragma solidity ^0.5.10;
; n! b+ H; y6 {5 m/**. y, R+ i% _8 h7 S# G9 K3 r
* @title Token ERC20 implementation
8 `" O; n) m7 g * @dev Simplified version - Contract allows only Transfer and Burn Tokens
- l0 [4 X3 a) U& c * @dev source: https://www.ethereum.org/token
7 h; U- \: Q2 R$ t0 x' X  t  U */
* i" Q" p: a) K) u; ucontract TokenERC20 {  S' v/ a6 p0 z* ^
    // Public variables of the token: W# Y0 S( N/ @0 i
    string public name;
3 f, J# T5 l& X- }2 z1 q/ V# P    string public symbol;
" C$ I" t# S+ a4 q: N; n, z    uint8 public decimals = 18;: ]# l' Y: f6 p9 Q( v) |" c
    // 18 decimals is the strongly suggested default, avoid changing it
: d; e1 Z0 M5 T    uint256 public totalSupply;. f# }1 }7 u, S4 B5 H6 _
    // This creates an array with all balances
) ^3 @" H8 G; L- [    mapping (address => uint256) public balanceOf;: m( \7 H+ u7 c, Q
    // This generates a public event on the blockchain that will notify clients" q0 w% o# j9 d1 x" f
    event Transfer(address indexed from, address indexed to, uint256 value);
$ b' u; d7 q7 G* G    // This notifies clients about the amount burnt
) q! t( R6 r, k! ?* P2 G* n    event Burn(address indexed from, uint256 value);4 D' A4 \: O' X1 c
    /**
# M; S8 n6 s6 t: K     * Constructor function% i2 M2 K2 n* q/ X/ R1 x( S4 e
     *
# H( P. Y4 k& z: {9 o! ~% m" h     * Initializes contract with initial supply tokens to the creator of the contract" U5 k: W* K' v* @# \5 U
     */* ?& J- _* ?" Y% @4 n
    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
4 N7 [8 X5 F6 @1 y/ B        public: ~6 @& J$ }: n0 g
    {! R- S7 M/ A3 @
        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
# `2 H1 j3 a6 h) ~8 s% ^        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens+ g! B$ `2 G, c/ l/ n! x
        name = tokenName;                                   // Set the name for display purposes
7 B" x5 u+ Z: ~+ I$ ]        symbol = tokenSymbol;                               // Set the symbol for display purposes" p: v3 ]8 b$ O
    }% T+ \, s, C& ~/ y
    /**9 l8 t5 t) O% V, l3 `
     * Internal transfer, only can be called by this contract
2 k8 l8 h& A6 i5 ^+ X     */
# _, X! h$ n: V* K' C    function _transfer(address _from, address _to, uint _value)) L% U& b3 J6 W% W8 I: O; y
        internal2 z4 R8 T5 N8 e& U9 B* Z; n
    {
' C" n! [( t6 y, E3 _        // Prevent transfer to 0x0 address. Use burn() instead
) [, a$ _. O4 T7 X0 Z        require(_to != address(0x0));) ^+ c% l( V* N7 ~) g- w
        // Check if the sender has enough
, Y0 K/ ]% u) T8 o% m2 `8 E3 E        require(balanceOf[_from] >= _value);
! u- x6 l9 A, t0 q$ H* f  {& i        // Check for overflows2 L9 _* u3 f" i2 v
        require(balanceOf[_to] + _value >= balanceOf[_to]);
6 E4 t1 v) X# F+ {+ O1 Y+ |& h6 y        // Save this for an assertion in the future
  l2 x) }3 `) g  v# }3 f; Q( L9 }        uint previousBalances = balanceOf[_from] + balanceOf[_to];, Q! w: u' |0 Z' s  v. X
        // Subtract from the sender
7 n! V+ L& `! Y) W9 _! I, ^        balanceOf[_from] -= _value;
3 F9 A* a! f, G+ {- B* [( `  S        // Add the same to the recipient) }# a6 X8 v+ p) R& [0 t
        balanceOf[_to] += _value;
) i* B1 |" j" f- F0 m0 v5 U        emit Transfer(_from, _to, _value);
: \' C8 u* t! K) ]! U" J2 P+ j% h- W% d        // Asserts are used to use static analysis to find bugs in your code. They should never fail1 t+ P5 R' _7 a2 M8 K5 _; x. w
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);# _& i2 A( S& E2 l
    }
+ ]. X2 D; M; D+ \! j- I( s5 L    /**" k$ W% i" [, K- G
     * Transfer tokens* L% n  \% G" m4 Q3 S4 J3 x" o
     *
3 _2 V* T  Y0 T     * Send `_value` tokens to `_to` from your account2 L* G5 ]3 p# h
     *
, X" _( h  x2 b, T" V     * @param _to The address of the recipient
# j' h' r7 t0 F+ V     * @param _value the amount to send% k( I, N: ^0 c$ _/ W0 _3 g# o( t. N
     */
  n" x) }! ?1 T" e. B. k/ ~    function transfer(address _to, uint256 _value)
7 V5 m6 H9 G3 X% i        public
& v. w7 O, r9 u" N, ^+ [        returns (bool success) % p( w* M& g( X& k/ ^! T
    {4 m( w9 e$ v  ^6 g! C1 S  u
        _transfer(msg.sender, _to, _value);1 L9 e7 q3 @5 R
        return true;- f: ], \0 r) E6 B2 s
    }
3 m! r& D. l8 R! x1 ]    /**" S3 D3 b% a- g3 E, }' n
     * Destroy tokens
+ e/ [$ \) a0 n1 Z     *- ~9 A& o$ k' d2 r( i9 ~
     * Remove `_value` tokens from the system irreversibly7 @7 B' H$ P! X
     *
0 R3 H' w" U% \# p     * @param _value the amount of money to burn
9 `$ B! w' |4 P, t% B% A     */3 i$ \1 r0 l6 }; F
    function burn(uint256 _value)
( J3 G4 r9 L# ^# o6 f0 A" p  F8 @8 ^        public, _! _% t1 H, h4 K, h& W
        returns (bool success)
% S. m5 e% I4 i& d3 O    {! S, z: \; n# l! c  ~  }; u
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough6 C8 z" O: Y) L+ M$ F- @
        balanceOf[msg.sender] -= _value;            // Subtract from the sender; L4 e8 I3 n5 Y  N' p& c: Y& [
        totalSupply -= _value;                      // Updates totalSupply
$ B6 H$ ~( t8 o7 v# Y2 C9 R        emit Burn(msg.sender, _value);& G2 w# z  v9 O; _5 F9 o+ a5 Z$ m
        return true;2 d9 m( x! F) W  E  `
    }8 d2 i' s5 T% B4 W9 Y
}2 W- Y0 v. x" \  p
部署的话7 g, h& q% o: a0 t) }! O# ~

8 J6 N; i+ M  z  v: |1 ?; Y三个参数,代币总金额,名字,代币符号(随你写了)
. C$ V2 W7 j0 L" q" c# S* i5 m. o8 @0 _# H% ]' x
拷贝合约地址
% o& p: Z7 u$ @
3 l" h7 h1 L4 y% s. C添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1