区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
190
0
0
*1.3 ?& B- L0 T) ]2 K# j
启动网页的remix-ide即可
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js4 m! s: }4 i, G( W, E/ ?( H
8 \) e* I. V5 D. O5 W
pragma solidity ^0.5.10;, N$ f: R, c9 R0 Q5 H
/**
* @title Token ERC20 implementation
* @dev Simplified version - Contract allows only Transfer and Burn Tokens7 q2 l, g( B/ M( R# r3 B
* @dev source: https://www.ethereum.org/token6 H) R9 p* x* f* M( i$ @- [
*/4 C0 _; |: e) i% r5 n- a, ~7 o
contract TokenERC20 {
// Public variables of the token3 S& Y; N W" ^( g
string public name;( U1 ~, I% j0 s$ O: f4 N* J
string public symbol;/ N3 V: B; s, l9 m
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;2 m7 k1 ?+ q0 N
// This creates an array with all balances( k! E( [, B4 \2 I9 `+ P; _
mapping (address => uint256) public balanceOf;, x. @/ C# Y: a2 T" M: B
// This generates a public event on the blockchain that will notify clients5 i* E4 `* F2 Q- P( }% L9 u5 M- \- y
event Transfer(address indexed from, address indexed to, uint256 value);8 X% F9 a: `) z
// This notifies clients about the amount burnt( M/ W+ G6 x! z# h v; F* n2 v! R
event Burn(address indexed from, uint256 value);. Y& o+ I* y+ L, o5 W3 A8 u" ]
/**$ U! r3 v; x0 v% v% v' \) F
* Constructor function3 j' i2 B. G0 \ S8 @ M' ?
*
* Initializes contract with initial supply tokens to the creator of the contract: l6 D3 F( @. {
*/
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public
{
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount1 \+ I+ x0 e1 c- n
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 purposes
}. f) }& d0 ], O% |+ `6 t- Z8 A
/**, N$ |$ t' l! d5 M# D+ o/ G
* Internal transfer, only can be called by this contract; w: E$ r4 |4 B _1 T& ~5 B4 E1 S
*/5 c( J, w# ]) V0 ]2 U
function _transfer(address _from, address _to, uint _value)
internal% ]7 o9 x7 g! j6 i" i
{
// Prevent transfer to 0x0 address. Use burn() instead1 p+ [3 ^: y9 A! U# U
require(_to != address(0x0));* ^% l! m# ?3 H' `
// Check if the sender has enough
require(balanceOf[_from] >= _value);9 r+ N+ T$ c! y3 n: u& K# L
// Check for overflows( ?- t+ K5 c% ~( v" u' G
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient/ ]0 M5 M' [" P4 V, d/ A7 P5 I0 Z* P7 q
balanceOf[_to] += _value;8 r% P; J, c% ?- }
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);- \) r+ O6 G# R, ^0 k. p7 I
}
/**; {. j- B1 A& g! c
* Transfer tokens
*2 e( K0 g7 A, Z( k
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send. w* v5 U- n7 ~' b5 h- C* J( Q
*/
function transfer(address _to, uint256 _value)
public
returns (bool success) % Q/ x1 f% B5 q7 h8 v9 B
{
_transfer(msg.sender, _to, _value);1 K# C( S' v. ]8 p
return true;& s9 A& `7 ]+ [8 Q( H: K( s
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn3 d0 W# `9 o% T
*/
function burn(uint256 _value), G$ t/ f" `% \
public, H/ {0 J% I& B( K2 `
returns (bool success). Z ~- _* m# w6 I6 a
{! J% o6 b2 ]) l$ e* u3 _; _
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough9 o" ^7 @, ~) Y7 |
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;
}4 N! g7 z& j* P" L8 @9 I
}
部署的话
三个参数,代币总金额,名字,代币符号(随你写了)! E% P6 J8 a( F3 G1 _3 q
拷贝合约地址 A# f' O/ @; X' l
添加代币,成功后就像我这样!
成为第一个吐槽的人