区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
115
0
0
*1.
启动网页的remix-ide即可* n% \) e& {' @# m1 n& d' P
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js2 S- B9 B% ]& q3 x1 ^# @% o
pragma solidity ^0.5.10;3 X) Q$ s0 u% s
/**
* @title Token ERC20 implementation
* @dev Simplified version - Contract allows only Transfer and Burn Tokens% J) V+ H# U# S. ]
* @dev source: https://www.ethereum.org/token2 l' k$ v% h7 N
*/( H$ n$ T- f J/ d2 T" M1 m7 R
contract TokenERC20 {6 H: o4 q- u1 Y( x5 z
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;. `8 i- U, @( G4 z' S
// 18 decimals is the strongly suggested default, avoid changing it. t( w; J2 W% X1 O
uint256 public totalSupply;
// This creates an array with all balances$ J. R! r! P+ ]+ k
mapping (address => uint256) public balanceOf;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);$ V* z, t" J6 c
/**5 H# P% i' ^- G
* Constructor function# C! N8 {; P$ y G& L
*
* Initializes contract with initial supply tokens to the creator of the contract! W, _% p9 w- B2 }0 w( _! O/ t
*/
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)7 x2 ^4 K0 m. f; e0 c2 D
public
{
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount% I7 [; {& V) n+ b1 [6 K
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes$ ^' N: k. O a4 x5 T
symbol = tokenSymbol; // Set the symbol for display purposes9 ]# X7 T( v. Q' C4 W/ B
}( }& Z/ [/ ~8 p) ]- i9 L& z4 _
/**& w: H6 B! c ^( s
* Internal transfer, only can be called by this contract1 T' |& g/ L' o; N% G9 E9 S
*/
function _transfer(address _from, address _to, uint _value)2 s1 _6 o/ m% ~" C- d
internal
{
// Prevent transfer to 0x0 address. Use burn() instead7 j2 d, ]1 m& j3 a* |
require(_to != address(0x0));
// Check if the sender has enough. _6 N) O( i6 O. k9 P
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value >= balanceOf[_to]);6 L. C- o' P% Q* u6 W( V: h
// Save this for an assertion in the future( ^1 N4 N; ~& c7 ^) G" }
uint previousBalances = balanceOf[_from] + balanceOf[_to]; g. F% J- W; w9 T# H
// Subtract from the sender( {: B( f9 E6 T7 v4 f" p
balanceOf[_from] -= _value;$ {6 g: s4 H9 O8 ]
// 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 fail4 B4 A0 O* v3 w* _+ G0 i% X7 L
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}$ o9 G* ^$ |1 A
/**9 b3 \, R7 a. _& e
* Transfer tokens
*! ?( k K$ O$ k
* Send `_value` tokens to `_to` from your account1 b$ g$ p4 q* T7 d
*+ u6 ~$ |" W6 X C6 W
* @param _to The address of the recipient
* @param _value the amount to send, Y7 S6 t7 a7 ^/ n! h3 @: i
*/0 ^, H2 W2 H0 U2 D1 V: o* K
function transfer(address _to, uint256 _value)% W/ L1 T2 S' |$ u Y/ Z7 H! G: {
public ! R% ^4 P F- s7 Z" n
returns (bool success)
{
_transfer(msg.sender, _to, _value);
return true;3 R! |& t7 n! f+ L/ O! J
}4 _$ s* d: L* J- T/ l
/**
* Destroy tokens; V6 K" B% q( J
*0 `# \, ?3 m% s6 c. {/ p3 T
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/* U9 o3 T- e5 q
function burn(uint256 _value)
public
returns (bool success)* P# p( R, J) E5 A; ~2 m9 D
{
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;, c8 u2 s i1 o7 x( S8 J5 D+ z! l
}- b+ D5 s( L& s. Z. j
}) n$ r( f! {' e. b" X1 r
部署的话4 Q0 u, _ L* z# @( W7 T
三个参数,代币总金额,名字,代币符号(随你写了)
拷贝合约地址
* g6 z7 c0 O/ R
添加代币,成功后就像我这样!
成为第一个吐槽的人