区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
195
0
0
*1., Z% L- p; q1 u8 _! M
启动网页的remix-ide即可. i8 Q: y- \# s. e7 G
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js! d0 U5 Z/ L0 b- ^# u& W/ @
pragma solidity ^0.5.10;
/**
* @title Token ERC20 implementation. N) s: ]2 w8 E
* @dev Simplified version - Contract allows only Transfer and Burn Tokens, M( c0 K. C- ^+ L m
* @dev source: https://www.ethereum.org/token3 M! r, p# n' Z* [6 D
*/
contract TokenERC20 {; ^; O) J1 Q' R$ l
// Public variables of the token
string public name;
string public symbol;+ {* r+ I0 N8 t0 b
uint8 public decimals = 18;8 ]$ l7 Q' k; J2 n& k# P
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
// This generates a public event on the blockchain that will notify clients4 o! M& v$ {& s. i* ]5 Q( K/ V
event Transfer(address indexed from, address indexed to, uint256 value);; d- s/ _1 T; r. c& N; g/ m
// This notifies clients about the amount burnt% u1 N; ` [# j- F* b
event Burn(address indexed from, uint256 value);
/**
* Constructor function. N; M6 R( A' X2 c7 w
*
* Initializes contract with initial supply tokens to the creator of the contract( V5 K/ n+ y4 T. ]" m
*/
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)4 r5 }3 d( _7 j* n% ^
public
{$ {9 j; v- B7 \0 ^% T
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens; o. p3 e6 z8 w+ d3 `& }7 y! \. ~
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}3 ?6 L0 z+ i, q) p
/**
* Internal transfer, only can be called by this contract) p1 a9 P& ?$ i& @) R& C m
*/9 ^/ d8 s, A+ ? {8 H- ]
function _transfer(address _from, address _to, uint _value)2 F) `9 N0 D/ k6 o
internal o+ L1 i8 L, q+ z2 ]
{( s9 p" I1 _* P
// Prevent transfer to 0x0 address. Use burn() instead& z2 j* D/ Z, S. S! |9 `6 _
require(_to != address(0x0));/ K) M5 h0 M5 V4 A
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value >= balanceOf[_to]);- H3 R+ Z; S% ?- N
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;9 J% \4 h$ b9 g
// Add the same to the recipient
balanceOf[_to] += _value;) F- U- g5 J- j# Z! ]
emit Transfer(_from, _to, _value);& V4 y3 t; p$ d0 T4 J/ i- |
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);" s6 j3 V7 L) ~ H' W
}
/**5 k% g- _* X9 l. w; M4 t# w
* Transfer tokens
*5 [* c( k! {" k
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value)
public " ]$ q, j' \4 D
returns (bool success)
{$ v: h/ H5 z/ K' V& P' X4 j, v6 x, }
_transfer(msg.sender, _to, _value);
return true;7 V, O* y: w9 B0 j, t( m, u
}/ U" O* t& T" p. B% D0 m7 f
/**" P* Q! n: k* w# r% L5 b" j
* Destroy tokens
*# y# }7 g) ?9 S3 u
* Remove `_value` tokens from the system irreversibly
*8 r9 s/ m3 X* x) J7 ~# M/ c, F+ a! w
* @param _value the amount of money to burn Y4 V# A) y4 p+ m$ y+ B7 w
*/
function burn(uint256 _value)& Q! U& T6 }; `1 }
public
returns (bool success)
{ j; A9 C: R) l
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough4 V& {6 k; }0 ?# ^
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);) y9 _; Y) S3 y0 E- o
return true;
}3 X! J l* t, Q5 T* ?4 \
}' G( m: n* ]/ ~, ^9 U& {5 T
部署的话
三个参数,代币总金额,名字,代币符号(随你写了)
( O+ }0 ?' @ A0 F. p- z9 d
拷贝合约地址& d" w3 s% D1 s. I' p( J. q
+ O: u( M( p5 [8 w t0 E; Z
添加代币,成功后就像我这样!
成为第一个吐槽的人