区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
121
0
0
*1.
启动网页的remix-ide即可% [5 I+ K% A. g7 b0 e
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js2 N, h' _- [5 [
pragma solidity ^0.5.10;
/**
* @title Token ERC20 implementation4 j& D+ ~4 F0 g# { n
* @dev Simplified version - Contract allows only Transfer and Burn Tokens
* @dev source: https://www.ethereum.org/token; y/ q; |6 z v7 m; f9 Y
*/
contract TokenERC20 {" `0 U7 [/ Z, z4 h$ A$ a' k
// Public variables of the token# y8 ]6 p7 f# V( F# Z! ?
string public name;
string public symbol;: N/ j; ^2 I# J1 r4 e
uint8 public decimals = 18;3 P7 e7 J. q& n2 H3 d
// 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 clients' h) B1 y4 Y! Z/ S' ?
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt" s1 c9 V, C. c% |
event Burn(address indexed from, uint256 value);# s( E+ h4 W1 t; N
/**
* Constructor function7 R7 I; o1 z+ [- B; `5 N$ N! t& K$ ?
*
* Initializes contract with initial supply tokens to the creator of the contract+ ?. x- }0 C0 B& L4 S, D7 a9 I- a
*/# L+ M$ _2 K; i
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public
{, K) ~' e7 u1 s4 H% Q
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount. l( @0 O; I7 y
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes4 o8 m$ C1 y" |/ w/ z+ s7 D" d
}
/**
* Internal transfer, only can be called by this contract
*/, s. P1 h0 j$ S: E
function _transfer(address _from, address _to, uint _value)% h+ {9 G- ?; b1 s" R, ]
internal
{5 t5 x. z8 ]/ ]4 A N* F# Z s
// Prevent transfer to 0x0 address. Use burn() instead/ j* H( n N1 W4 p# c
require(_to != address(0x0));1 @ q* v' u' N$ J* c5 F
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows) W& q. o8 D$ }& i9 b2 q; r
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];. |# [. ^& W( I1 h" Z% v7 y
// Subtract from the sender* v7 z+ z6 _. `: M( V6 ^
balanceOf[_from] -= _value;! M9 v' v( }) J ~- c0 L" m8 p
// Add the same to the recipient$ b r% Q/ z# |6 m! V
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);, |7 V" f6 ?: x
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);4 d# X; l. S! X
}
/**
* Transfer tokens
* }7 {$ z1 N* O
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/3 _' J) }: D" G0 Y
function transfer(address _to, uint256 _value)
public
returns (bool success) , Z$ c: l' T3 N" |
{
_transfer(msg.sender, _to, _value);
return true;
}
/**
* Destroy tokens5 E0 n+ ?# e6 X) c' u
*& ^. c0 o( u+ d& p* g2 Z
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value)2 A. Z/ w0 F% j2 T7 C5 ]& v' ^5 w
public
returns (bool success)7 K+ ^9 C* f) `$ |
{/ @% H- }/ a% Y
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender$ |. {. M. P$ \6 U$ g: M
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);+ V V' K+ T% u; _
return true;
}" Q' p! O+ g8 p( r7 g% ?
}
部署的话
三个参数,代币总金额,名字,代币符号(随你写了)7 y9 `9 a% ~$ |% h" B
8 Y9 \% k, d- }8 B/ G
拷贝合约地址0 X9 x6 J% e! N: j4 G
添加代币,成功后就像我这样!
成为第一个吐槽的人