区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
194
0
0
*1.( |( K# Z ?: d/ k7 n3 a
启动网页的remix-ide即可: P3 r" W( A& b+ H5 ]
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
pragma solidity ^0.5.10;
/**! w d6 B. Z3 M- {( ?* v) S
* @title Token ERC20 implementation
* @dev Simplified version - Contract allows only Transfer and Burn Tokens
* @dev source: https://www.ethereum.org/token9 ~$ J: Z' O" _) b8 n# p
*// i, [9 s1 v: E8 Y1 J% L
contract TokenERC20 {& c1 [. N9 P" ~, D8 N7 k5 j
// Public variables of the token/ I8 h E1 [3 J) J
string public name;4 _5 e% r0 A$ d/ e' O) G; R* _3 @
string public symbol;& k. y: N5 i6 r- G8 j4 g
uint8 public decimals = 18;/ B' r4 Y2 Z' I( X0 ~
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;+ u8 O6 s% H& ^$ h6 F# t. A+ a. l- Q
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
// This generates a public event on the blockchain that will notify clients& F: L# t9 y+ \. j P* T
event Transfer(address indexed from, address indexed to, uint256 value);) E- T E7 X2 W7 G1 K
// This notifies clients about the amount burnt1 Z+ R$ p6 j6 _; q, j5 u
event Burn(address indexed from, uint256 value);/ \0 L; m* n* D
/**
* Constructor function
*
* Initializes contract with initial supply tokens to the creator of the contract* i9 H5 r: c0 K0 f2 e$ g! }
*/
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public
{
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount) c* \, [7 ]5 T7 ^2 j
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens$ g8 v& H) Z; ~% k- {9 q
name = tokenName; // Set the name for display purposes0 \9 n- p" T3 v& B0 j- s
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value)" s# G0 ?7 W- C5 d z
internal
{. N) N; V4 E* }) }$ b
// Prevent transfer to 0x0 address. Use burn() instead9 |! i" O V0 i7 h: p$ `5 _$ J
require(_to != address(0x0));. P8 d3 U6 ]0 P' ], d$ I
// Check if the sender has enough6 Z1 x6 t8 i& {0 |
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Save this for an assertion in the future7 d8 u9 l m; Q
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;) D f2 m: c$ ], {6 G' M
emit Transfer(_from, _to, _value);. l7 ]( |7 L3 Q" ~5 R: t' L
// Asserts are used to use static analysis to find bugs in your code. They should never fail& I: |) S: O+ l2 E1 D
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);) I3 i7 @$ y0 {
}- m% p3 Y# k! ^2 o9 ^& T. j/ |) _: f5 m
/**
* Transfer tokens
*' b( U4 r, z0 W0 ?2 c
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send/ O. y, Q/ `7 q: A! C& j% K0 M
*/
function transfer(address _to, uint256 _value)
public 6 x( x" p/ i$ e7 s, Z$ F
returns (bool success)
{9 i% |! w. Z' ]% o0 H. R
_transfer(msg.sender, _to, _value);. S) q; y$ n7 C' \) h* p3 A: o
return true;
}5 {2 X; w0 |! d3 G; |+ R$ j; _+ Z# ]
/**3 ] Q6 I) N8 {3 ]# `
* Destroy tokens- a$ k! I/ _7 ^8 C7 e; v; ^3 |
*! O+ @+ S* ?1 g& u) ~; F0 n
* Remove `_value` tokens from the system irreversibly
*. W; ]4 J. ]) U5 O1 r
* @param _value the amount of money to burn% L0 X: a, k. K
*/& N! E. }% [5 j/ j8 C6 k- c
function burn(uint256 _value)
public
returns (bool success)
{
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;( M$ o6 K) Z' L! J& M5 ^
}
}, N/ f$ y; M! D+ f6 [3 E9 a3 P" }
部署的话# }" Y3 [, R9 s4 I6 B
% @. g5 m& ?4 ?, A6 [2 K
三个参数,代币总金额,名字,代币符号(随你写了)8 K9 e# m( n6 P, C
9 D3 A4 n* |/ g" g. N) E
拷贝合约地址
添加代币,成功后就像我这样!
成为第一个吐槽的人