Hi 游客

更多精彩,请登录!

比特池塘 区块链技术 正文

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

朱丹铎
190 0 0
什么都不需要准备8 z, ~/ U1 Y( |1 E2 v4 h. P  }
*1.3 ?& B- L0 T) ]2 K# j
启动网页的remix-ide即可
% b0 g7 ]. k& K& w8 i3 g- P) f8 Q0 @https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js4 m! s: }4 i, G( W, E/ ?( H
8 \) e* I. V5 D. O5 W
pragma solidity ^0.5.10;, N$ f: R, c9 R0 Q5 H
/**
& q! g! ~5 L3 Q- j6 H' y * @title Token ERC20 implementation
" O4 m( U& c( S1 V * @dev Simplified version - Contract allows only Transfer and Burn Tokens7 q2 l, g( B/ M( R# r3 B
* @dev source: https://www.ethereum.org/token6 H) R9 p* x* f* M( i$ @- [
*/4 C0 _; |: e) i% r5 n- a, ~7 o
contract TokenERC20 {
# V: ^' G8 D8 |0 d    // Public variables of the token3 S& Y; N  W" ^( g
    string public name;( U1 ~, I% j0 s$ O: f4 N* J
    string public symbol;/ N3 V: B; s, l9 m
    uint8 public decimals = 18;
# d1 a3 {7 \5 r# x    // 18 decimals is the strongly suggested default, avoid changing it
) U2 T4 ^! f- j" @    uint256 public totalSupply;2 m7 k1 ?+ q0 N
    // This creates an array with all balances( k! E( [, B4 \2 I9 `+ P; _
    mapping (address => uint256) public balanceOf;, x. @/ C# Y: a2 T" M: B
    // This generates a public event on the blockchain that will notify clients5 i* E4 `* F2 Q- P( }% L9 u5 M- \- y
    event Transfer(address indexed from, address indexed to, uint256 value);8 X% F9 a: `) z
    // This notifies clients about the amount burnt( M/ W+ G6 x! z# h  v; F* n2 v! R
    event Burn(address indexed from, uint256 value);. Y& o+ I* y+ L, o5 W3 A8 u" ]
    /**$ U! r3 v; x0 v% v% v' \) F
     * Constructor function3 j' i2 B. G0 \  S8 @  M' ?
     *
- R% D) [9 Z0 h4 o& _) o8 N     * Initializes contract with initial supply tokens to the creator of the contract: l6 D3 F( @. {
     */
  X+ Y7 }. r% {' V5 N+ d' i9 ?" H% i    constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
( D% F! q+ c8 F8 G6 R! e5 M        public
* x+ O5 Z/ P' ]# c    {
1 t# _5 K( r/ Q8 }* t/ I/ Y2 O( e        totalSupply = initialSupply * 10 ** uint256(decimals);  // Update total supply with the decimal amount1 \+ I+ x0 e1 c- n
        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
/ m8 U  s3 v# s) O* W        name = tokenName;                                   // Set the name for display purposes
7 r3 u+ F  P2 b$ T+ c# I' t        symbol = tokenSymbol;                               // Set the symbol for display purposes
$ {  ~( Y, o: }' n& ~! X! u4 L    }. f) }& d0 ], O% |+ `6 t- Z8 A
    /**, N$ |$ t' l! d5 M# D+ o/ G
     * Internal transfer, only can be called by this contract; w: E$ r4 |4 B  _1 T& ~5 B4 E1 S
     */5 c( J, w# ]) V0 ]2 U
    function _transfer(address _from, address _to, uint _value)
% q9 d* i1 O$ D* T9 I% \7 T        internal% ]7 o9 x7 g! j6 i" i
    {
/ u. l# j: r% c4 N        // Prevent transfer to 0x0 address. Use burn() instead1 p+ [3 ^: y9 A! U# U
        require(_to != address(0x0));* ^% l! m# ?3 H' `
        // Check if the sender has enough
& G: n* e+ T% ~6 ?( R- r; V  V: U        require(balanceOf[_from] >= _value);9 r+ N+ T$ c! y3 n: u& K# L
        // Check for overflows( ?- t+ K5 c% ~( v" u' G
        require(balanceOf[_to] + _value >= balanceOf[_to]);
& F6 X: T$ U+ c$ n2 @8 u        // Save this for an assertion in the future
- T9 t& n7 U3 q+ X8 u        uint previousBalances = balanceOf[_from] + balanceOf[_to];
4 D/ n4 u8 R, u% C2 X        // Subtract from the sender
- P2 V. `' i0 K3 W) i. ]        balanceOf[_from] -= _value;
5 a3 |" c" r: @( v4 J) \% `        // Add the same to the recipient/ ]0 M5 M' [" P4 V, d/ A7 P5 I0 Z* P7 q
        balanceOf[_to] += _value;8 r% P; J, c% ?- }
        emit Transfer(_from, _to, _value);
) r+ e. @; X: q/ E        // Asserts are used to use static analysis to find bugs in your code. They should never fail
# n3 ~1 ~7 ~/ \2 d        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);- \) r+ O6 G# R, ^0 k. p7 I
    }
& j# f: O# N) s4 w2 T) ^  q    /**; {. j- B1 A& g! c
     * Transfer tokens
1 @, B; c3 {( i     *2 e( K0 g7 A, Z( k
     * Send `_value` tokens to `_to` from your account
4 l/ N; x" N/ z9 V; u- }5 o     *
% k; k' n' R* ~' r2 r0 A! T     * @param _to The address of the recipient
  f% \+ ~9 d6 k5 t3 b" e     * @param _value the amount to send. w* v5 U- n7 ~' b5 h- C* J( Q
     */
7 E; {  D- ?3 ~8 c( ]  P; l! l    function transfer(address _to, uint256 _value)
) B0 F  U6 C" E% @0 V* R7 f" \! r        public
! R/ W: E. @( s4 f0 x) e        returns (bool success) % Q/ x1 f% B5 q7 h8 v9 B
    {
3 J6 l! \) D+ s' o        _transfer(msg.sender, _to, _value);1 K# C( S' v. ]8 p
        return true;& s9 A& `7 ]+ [8 Q( H: K( s
    }
: g1 [" a  x' E. K2 {; W    /**
$ Z) n2 R  A& Y) a" ^     * Destroy tokens
0 O2 I+ l# G3 D& A" h     *
& A2 Q+ `0 i" C     * Remove `_value` tokens from the system irreversibly
7 X$ s) ?/ N; G2 C' E1 z6 G( B     *
, {" n" h" w7 a( F     * @param _value the amount of money to burn3 d0 W# `9 o% T
     */
' t5 _* J) n/ e/ o! |+ N3 h3 D7 m    function burn(uint256 _value), G$ t/ f" `% \
        public, H/ {0 J% I& B( K2 `
        returns (bool success). Z  ~- _* m# w6 I6 a
    {! J% o6 b2 ]) l$ e* u3 _; _
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough9 o" ^7 @, ~) Y7 |
        balanceOf[msg.sender] -= _value;            // Subtract from the sender
9 ~% Z1 F' L7 Y( D        totalSupply -= _value;                      // Updates totalSupply
" n& m, L6 @8 h+ y        emit Burn(msg.sender, _value);
7 s* F$ e+ J9 s3 w  G2 q7 S        return true;
8 \$ F1 O) C  I6 X    }4 N! g7 z& j* P" L8 @9 I
}
2 d9 J% G6 C1 j- |; P: T部署的话
7 F( Y1 z6 z7 N* T: A
# m. j$ x5 e  h3 g2 `4 z6 j5 ]三个参数,代币总金额,名字,代币符号(随你写了)! E% P6 J8 a( F3 G1 _3 q

' h" c, B7 o! s) ], n: d# V) o( A, w拷贝合约地址  A# f' O/ @; X' l

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

本版积分规则

成为第一个吐槽的人

朱丹铎 小学生
  • 粉丝

    0

  • 关注

    0

  • 主题

    1