Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
194 0 0
什么都不需要准备
; ^7 {4 _5 W6 d*1.( |( K# Z  ?: d/ k7 n3 a
启动网页的remix-ide即可: P3 r" W( A& b+ H5 ]
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
& c1 J! s" W' o; A: P9 w
( F( d3 V' i+ ~. Q0 {3 rpragma solidity ^0.5.10;
3 w) x9 D9 ~5 @) I: q5 R! F/**! w  d6 B. Z3 M- {( ?* v) S
* @title Token ERC20 implementation
0 i" d2 g% o1 |. ~0 x- `$ A * @dev Simplified version - Contract allows only Transfer and Burn Tokens
3 n9 g7 Z# S9 |3 j$ U3 W  d * @dev source: https://www.ethereum.org/token9 ~$ J: Z' O" _) b8 n# p
*// i, [9 s1 v: E8 Y1 J% L
contract TokenERC20 {& c1 [. N9 P" ~, D8 N7 k5 j
    // Public variables of the token/ I8 h  E1 [3 J) J
    string public name;4 _5 e% r0 A$ d/ e' O) G; R* _3 @
    string public symbol;& k. y: N5 i6 r- G8 j4 g
    uint8 public decimals = 18;/ B' r4 Y2 Z' I( X0 ~
    // 18 decimals is the strongly suggested default, avoid changing it
; @# ~/ U; F, U3 }3 ?; X    uint256 public totalSupply;+ u8 O6 s% H& ^$ h6 F# t. A+ a. l- Q
    // This creates an array with all balances
$ |9 U3 k1 u2 z5 F# O    mapping (address => uint256) public balanceOf;
' A: O/ @6 X; {) W2 v7 f& }. O    // This generates a public event on the blockchain that will notify clients& F: L# t9 y+ \. j  P* T
    event Transfer(address indexed from, address indexed to, uint256 value);) E- T  E7 X2 W7 G1 K
    // This notifies clients about the amount burnt1 Z+ R$ p6 j6 _; q, j5 u
    event Burn(address indexed from, uint256 value);/ \0 L; m* n* D
    /**
3 Y3 ]( W% y. \3 o     * Constructor function
* U4 ?) C' C5 r( J& K6 ]5 ^0 d2 ]     *
7 g1 d2 W) i% |     * Initializes contract with initial supply tokens to the creator of the contract* i9 H5 r: c0 K0 f2 e$ g! }
     */
; O6 G4 g) {& B1 Y    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
$ f- a! U/ d2 }        public
8 U2 b! o. d% F6 s% A    {
- e) o. ~* r8 V$ V$ S- }        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount) c* \, [7 ]5 T7 ^2 j
        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens$ g8 v& H) Z; ~% k- {9 q
        name = tokenName;                                   // Set the name for display purposes0 \9 n- p" T3 v& B0 j- s
        symbol = tokenSymbol;                               // Set the symbol for display purposes
  h1 S- t) ?+ u7 i/ Y' `5 D7 o) M! |- q    }
# j2 [, D5 j! ?    /**
' w( p: H1 f3 ^3 i, o& B" f     * Internal transfer, only can be called by this contract
+ W1 k4 M% [  T8 z* T2 x     */
# V; _5 l4 O0 c4 L- u    function _transfer(address _from, address _to, uint _value)" s# G0 ?7 W- C5 d  z
        internal
0 J6 s: X! E( b    {. N) N; V4 E* }) }$ b
        // Prevent transfer to 0x0 address. Use burn() instead9 |! i" O  V0 i7 h: p$ `5 _$ J
        require(_to != address(0x0));. P8 d3 U6 ]0 P' ], d$ I
        // Check if the sender has enough6 Z1 x6 t8 i& {0 |
        require(balanceOf[_from] >= _value);
! ~7 h+ F* @9 Z: D5 ^        // Check for overflows
0 a, R# c) N; K% h' W2 D        require(balanceOf[_to] + _value >= balanceOf[_to]);
. l, p$ d# ]; N3 H: _9 s& W        // Save this for an assertion in the future7 d8 u9 l  m; Q
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
) v0 \0 [6 O7 t+ E        // Subtract from the sender
( E9 b# B* `3 r9 ~        balanceOf[_from] -= _value;
- R3 W1 D1 ~" J# o; N  t% j  ^        // Add the same to the recipient
7 N! a" m: b$ R0 s; U; h6 I        balanceOf[_to] += _value;) D  f2 m: c$ ], {6 G' M
        emit Transfer(_from, _to, _value);. l7 ]( |7 L3 Q" ~5 R: t' L
        // Asserts are used to use static analysis to find bugs in your code. They should never fail& I: |) S: O+ l2 E1 D
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);) I3 i7 @$ y0 {
    }- m% p3 Y# k! ^2 o9 ^& T. j/ |) _: f5 m
    /**
& Q. E# U2 W# l* z     * Transfer tokens
- H: r% K6 w) D$ Y     *' b( U4 r, z0 W0 ?2 c
     * Send `_value` tokens to `_to` from your account
# y# g$ A8 C$ A4 d     *
! w, Y6 |7 v8 G. B& ?) d8 X1 n: w; }     * @param _to The address of the recipient
9 _$ H/ i# q* r, z6 K! s     * @param _value the amount to send/ O. y, Q/ `7 q: A! C& j% K0 M
     */
9 H& S1 D' `% w1 H# j    function transfer(address _to, uint256 _value)
, V8 v0 X  @2 w9 Z# q        public 6 x( x" p/ i$ e7 s, Z$ F
        returns (bool success)
. S7 R7 _5 s+ `8 i; n0 |7 Q    {9 i% |! w. Z' ]% o0 H. R
        _transfer(msg.sender, _to, _value);. S) q; y$ n7 C' \) h* p3 A: o
        return true;
! ]% k6 D; x' }5 K3 I" g* N    }5 {2 X; w0 |! d3 G; |+ R$ j; _+ Z# ]
    /**3 ]  Q6 I) N8 {3 ]# `
     * Destroy tokens- a$ k! I/ _7 ^8 C7 e; v; ^3 |
     *! O+ @+ S* ?1 g& u) ~; F0 n
     * Remove `_value` tokens from the system irreversibly
( N# ]' u9 ]; e. Z3 d     *. W; ]4 J. ]) U5 O1 r
     * @param _value the amount of money to burn% L0 X: a, k. K
     */& N! E. }% [5 j/ j8 C6 k- c
    function burn(uint256 _value)
8 x) O8 a' {, n' P        public
1 k3 }/ E7 F! t        returns (bool success)
1 q" I, C: P' w) O5 x    {
, I( R2 i5 a/ {$ j) [5 ~        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
- E  r; _( H8 ]4 R5 w9 }        balanceOf[msg.sender] -= _value;            // Subtract from the sender
8 `6 H5 M) H: j3 B$ T' z! e        totalSupply -= _value;                      // Updates totalSupply
7 x) M) M! B/ x: E        emit Burn(msg.sender, _value);
* Q9 v5 L  Q& Z; ~  p8 n! R        return true;( M$ o6 K) Z' L! J& M5 ^
    }
2 J9 q: B2 e' Q5 D6 f}, N/ f$ y; M! D+ f6 [3 E9 a3 P" }
部署的话# }" Y3 [, R9 s4 I6 B
% @. g5 m& ?4 ?, A6 [2 K
三个参数,代币总金额,名字,代币符号(随你写了)8 K9 e# m( n6 P, C
9 D3 A4 n* |/ g" g. N) E
拷贝合约地址
- ?; i2 z5 |* m  f
4 z" d! M6 V; P6 [添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1