区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
143
0
0
*1.
启动网页的remix-ide即可
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
pragma 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
*/2 x B) b1 V* J) H3 q" C, [. _( I$ Z
contract TokenERC20 {
// Public variables of the token% w6 I( f- Y: p$ J/ F# u
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it! C( }. ^0 e I L+ C, e# ?
uint256 public totalSupply;
// This creates an array with all balances. Y* m% j; g9 ^) n1 C0 y
mapping (address => uint256) public balanceOf;
// 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);
// 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
/**
* Constructor function2 o" a Z# }; F; r/ H8 r
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public8 n4 T$ ]/ `! }8 J8 u3 g
{
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
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
}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
*/, 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 ^
{
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != address(0x0));
// Check if the sender has enough
require(balanceOf[_from] >= _value);/ q' T! ?% e1 H$ A7 }* k
// Check for overflows
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];- v$ o$ D! c! f0 q+ q, [, D' F4 N2 c
// Subtract from the sender
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. B3 d$ @' V. J) x( j
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}* W: _+ M! U. y6 ^. V- u9 h
/**+ a2 a2 s ]$ U; O0 a' L& \
* Transfer tokens; p+ x1 W4 U) Q; o% @+ g, q
*
* 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
*/
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)
{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
}
/**: g1 o! @0 Z! q, | t* a. e% g2 F
* Destroy tokens
*0 l& P, L' n2 Q
* Remove `_value` tokens from the system irreversibly
*- n: g0 U0 N# }
* @param _value the amount of money to burn- Y/ K7 z0 j+ |) b
*/
function burn(uint256 _value)$ m5 ?+ t0 S) C; l, ]" Y
public
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
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);
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
拷贝合约地址2 }* }% \! a. m* [7 n! {/ _' |
添加代币,成功后就像我这样!
成为第一个吐槽的人