Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
143 0 0
什么都不需要准备
1 r. `$ J3 y8 s: |5 V*1.
# m3 |; N8 N3 k) z启动网页的remix-ide即可
9 s/ F) _0 M4 s4 t: W: @" r- b& yhttps://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
+ V" {, c# R, Z6 ^5 g7 j
$ o0 B8 E/ Q% vpragma solidity ^0.5.10;6 U- t8 w4 Q9 G) X
/**% Z2 h* Z% |1 c4 D. [
* @title Token ERC20 implementation, n' I( ]( A2 S7 k
* @dev Simplified version - Contract allows only Transfer and Burn Tokens+ Y8 a3 j* a0 E+ a2 T. W1 x  {
* @dev source: https://www.ethereum.org/token
6 m; u) n9 {) T* ^( o6 @ */2 x  B) b1 V* J) H3 q" C, [. _( I$ Z
contract TokenERC20 {
* g; E# N5 K/ x+ j% W6 z    // Public variables of the token% w6 I( f- Y: p$ J/ F# u
    string public name;
" _6 x7 C4 O- f    string public symbol;
- R' I8 f: F% V" [& \    uint8 public decimals = 18;
4 c0 E: Z% V1 G5 b' c& q& r    // 18 decimals is the strongly suggested default, avoid changing it! C( }. ^0 e  I  L+ C, e# ?
    uint256 public totalSupply;
7 u9 {/ f+ Z8 V    // This creates an array with all balances. Y* m% j; g9 ^) n1 C0 y
    mapping (address => uint256) public balanceOf;
+ W( R# h6 {, v/ Z9 n    // This generates a public event on the blockchain that will notify clients! H- U+ P- s( ^& t
    event Transfer(address indexed from, address indexed to, uint256 value);
+ a3 ?; _! \. l4 V; \    // This notifies clients about the amount burnt0 ]: O$ h" m# r
    event Burn(address indexed from, uint256 value);, t! ^% a6 q# M5 q! V' ^& ]( C
    /**
) ~& F( G4 f) F1 v9 g/ R+ \  b3 R) _     * Constructor function2 o" a  Z# }; F; r/ H8 r
     *
* h1 m" K; ?& @# L+ ?+ @     * Initializes contract with initial supply tokens to the creator of the contract
- C6 \3 l1 b4 j     */
/ P% X7 r6 g' {' R) M& B    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
( L( k- C( k! [, Z" q' b        public8 n4 T$ ]/ `! }8 J8 u3 g
    {
! ]+ `; S5 C, @* b8 y        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount
5 _9 T9 h# p) ]# c1 \1 n7 i$ ~* d        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens2 C4 @7 m& x* w7 G6 Z1 r/ Z$ M; }
        name = tokenName;                                   // Set the name for display purposes- R' u' l& b! g/ Y. |+ M
        symbol = tokenSymbol;                               // Set the symbol for display purposes
. I; `# s3 b$ j5 a% x7 p* E" K0 a  o    }4 R9 l1 A, m! k3 X  Y" @, o6 Q
    /**& R( {0 ~3 T; f: P7 q- i
     * Internal transfer, only can be called by this contract
7 c! V8 O% c* @3 }5 B! ^; z     */, q1 j* z7 `8 P! l$ T2 ~. s+ o) z( _. ~
    function _transfer(address _from, address _to, uint _value)6 l) c: s+ K: ~- }5 ?
        internal6 P+ K; [) N: s. K5 ^
    {
$ k' @! e: U  `7 Y6 K2 ?! O        // Prevent transfer to 0x0 address. Use burn() instead
1 r6 g1 }( @: D7 c        require(_to != address(0x0));
! L3 t& U+ D3 T( i  w: c' Z# Q4 ]( l$ C        // Check if the sender has enough
# `% p# G4 Y  E        require(balanceOf[_from] >= _value);/ q' T! ?% e1 H$ A7 }* k
        // Check for overflows
$ a6 d$ a8 A) B6 t) y        require(balanceOf[_to] + _value >= balanceOf[_to]);
1 }. q2 G, {) P) g# E* B0 p1 T        // Save this for an assertion in the future
' F6 l6 I3 ~+ r' J7 G        uint previousBalances = balanceOf[_from] + balanceOf[_to];- v$ o$ D! c! f0 q+ q, [, D' F4 N2 c
        // Subtract from the sender
, x+ r: P: h  d9 p5 p7 V* R/ l7 k        balanceOf[_from] -= _value;
, e/ B8 j9 ]4 I5 i- T' g" L" K. \3 Q) u        // Add the same to the recipient
  P1 `) }: C8 b' ~# a; I        balanceOf[_to] += _value;
* A% b0 Y" R: }, K8 {        emit Transfer(_from, _to, _value);
9 p# L5 b, _; F4 [/ c8 @        // Asserts are used to use static analysis to find bugs in your code. They should never fail. B3 d$ @' V. J) x( j
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
6 v! _3 `3 O+ w8 W8 Q( t& _+ n    }* W: _+ M! U. y6 ^. V- u9 h
    /**+ a2 a2 s  ]$ U; O0 a' L& \
     * Transfer tokens; p+ x1 W4 U) Q; o% @+ g, q
     *
( ~+ w1 B, |: o+ j7 x/ u     * Send `_value` tokens to `_to` from your account8 `* b0 C  F( w
     *$ N8 `9 S) G) B% x5 _0 g9 v2 i
     * @param _to The address of the recipient% z5 K6 H4 z; i* Q  V# l
     * @param _value the amount to send& X! {: Q8 Y. y5 D
     */
) B/ M: s: U" G2 I    function transfer(address _to, uint256 _value)5 `% S! i  V. A: ], d
        public   Q9 k: ^" w( c9 y, I7 E, M7 l6 Z
        returns (bool success)
, j) e$ Z' R4 S9 w+ q% Z) l( i    {4 F9 ^2 }- {* Q+ I8 m- J5 l- t$ J
        _transfer(msg.sender, _to, _value);* k, b/ J, e. l5 Y! d8 \/ a
        return true;) k( G: B4 ?  D8 \% N/ _! L, G7 w  s
    }
; O; _- @: _. o4 h5 D- S    /**: g1 o! @0 Z! q, |  t* a. e% g2 F
     * Destroy tokens
# K/ }4 e) R& }' P& q, _* r1 h     *0 l& P, L' n2 Q
     * Remove `_value` tokens from the system irreversibly
3 V% L9 S/ l" E# B     *- n: g0 U0 N# }
     * @param _value the amount of money to burn- Y/ K7 z0 j+ |) b
     */
/ n9 P1 j& ^. f0 |; x, t7 _* W    function burn(uint256 _value)$ m5 ?+ t0 S) C; l, ]" Y
        public
) r) e6 Y+ P# A* k- w        returns (bool success). \  U  X: A$ T. h3 ?' L/ H. E
    {. k0 j4 A* ^, C
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
+ T7 a: @7 `- h$ h; m        balanceOf[msg.sender] -= _value;            // Subtract from the sender9 P$ p7 }+ o3 O* X. ]
        totalSupply -= _value;                      // Updates totalSupply, Y. m5 Z/ R4 P% D6 K
        emit Burn(msg.sender, _value);
& i- p, a2 h% i( f; v3 t6 N* x6 ?        return true;% X7 @. {. q: z9 w; M# `- Y% S3 M
    }6 r) o' N$ x5 {1 q! @+ Y8 G2 w5 _8 ~$ \
}, _9 z$ |5 U$ f0 E. J, Z
部署的话0 m+ K6 G% b7 y: F
' K& O3 _2 a( z3 C* Z6 l7 y- g( s
三个参数,代币总金额,名字,代币符号(随你写了)6 F7 @1 ?, R! Y  H4 h* K

1 Q- |1 V! r5 m: ~8 C拷贝合约地址2 }* }% \! a. m* [7 n! {/ _' |

" p# I7 \5 X% ]! H7 g添加代币,成功后就像我这样!
BitMere.com 比特池塘系信息发布平台,比特池塘仅提供信息存储空间服务。
声明:该文观点仅代表作者本人,本文不代表比特池塘立场,且不构成建议,请谨慎对待。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1