区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
163
0
0
*1.
启动网页的remix-ide即可
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js' D9 P ?: r O1 M3 K/ O+ c9 a
pragma solidity ^0.5.10;8 H/ W$ ?$ F- d6 M- r- ^* P
/**. e* R' O; M2 D3 K$ F3 ^9 N5 B0 v
* @title Token ERC20 implementation! Q9 i- c3 m, G* u2 G$ R _
* @dev Simplified version - Contract allows only Transfer and Burn Tokens
* @dev source: https://www.ethereum.org/token0 _: j3 m3 C8 X* y+ S+ N
*/
contract TokenERC20 {9 J. g# r3 l& c0 [0 x5 |. g4 ]
// Public variables of the token
string public name;
string public symbol;9 ]9 O8 T3 `0 F9 n3 p
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;# O' \, x2 o& a; f4 X
// This creates an array with all balances( a! k/ g6 S& {- e1 p% b/ P7 ~) Y8 w
mapping (address => uint256) public balanceOf;
// This generates a public event on the blockchain that will notify clients! h( _" P- b/ \! A! c
event Transfer(address indexed from, address indexed to, uint256 value);5 u8 V9 i2 f; T/ N. d- c- D$ H
// This notifies clients about the amount burnt% l0 n( i2 C' y$ ]5 [, a
event Burn(address indexed from, uint256 value);
/**; m! g5 m( T8 k$ J
* Constructor function: m0 g) X8 n) ^9 F
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)* C, ~- `/ A( I$ Y
public
{7 K f3 j1 {8 U1 b8 S9 Y/ `
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens) G1 i4 G* K6 v/ l; n6 `
name = tokenName; // Set the name for display purposes; f. b" N: s% b0 k( m, X
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**/ M6 _0 ^0 }+ M$ L/ k2 ]
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value)
internal2 Z$ R$ V) w ?" ]( H
{
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != address(0x0));" f B& S2 q; D8 g. _
// Check if the sender has enough9 z# [* U2 ]4 `" ~1 Q' v' p0 u8 g! g
require(balanceOf[_from] >= _value); l$ _6 M- }# o# o' q
// Check for overflows
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];9 v: o+ Z$ w+ h W! N$ Z" f
// Subtract from the sender3 U7 Y1 R u' Z# q2 X
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail, N, w% }4 h; [# Z& z- Z0 `
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);3 s6 V7 d3 r3 Y. H
}, O' q* P8 L% F$ x5 g" U
/**' l4 { C4 ]6 r+ N8 [
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account! K$ Z; [, c5 Q
*5 D9 K. D4 ?1 h' {" Q
* @param _to The address of the recipient5 A! G; X; Z0 h: t& Y8 @) I# I" n
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value)( e2 J, M4 r$ [
public
returns (bool success) ' \/ U \+ h- @" X+ `/ Y
{0 M; a: c$ i( j9 V6 O. b6 M. J# M
_transfer(msg.sender, _to, _value);3 t$ b& @0 g& F0 ]- U9 w' k3 t8 P; ^
return true;: g1 \4 h2 x7 I3 e, D r* t
}/ S3 m, J9 k, x! ]3 R
/**# C& o/ B* O5 g" E
* Destroy tokens/ ~ v- Y. `# y2 {: r& o% N
*
* Remove `_value` tokens from the system irreversibly0 H2 s1 _ _" }7 f& w
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value)3 w( i. G7 G3 E1 |$ w
public6 J2 y( U; ~9 ]6 G1 p( c9 H4 B
returns (bool success)$ W; ^: ? }1 ]9 e K
{9 o8 P; ]# T' k
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough- W9 j; F9 J, {' B
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;# M, ]+ H w' s8 F
}
}
部署的话
三个参数,代币总金额,名字,代币符号(随你写了)' q( Q/ i0 M+ H2 M4 f: [6 e2 _
8 t' X+ I( W. C; M; m$ `' g
拷贝合约地址
添加代币,成功后就像我这样!
成为第一个吐槽的人