区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
194
0
0
*1.; a. \, ?3 a( j8 v) n' Y* ]
启动网页的remix-ide即可
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js
7 {; p" A+ h0 _& X- t* n/ `
pragma solidity ^0.5.10;
/**. y, R+ i% _8 h7 S# G9 K3 r
* @title Token ERC20 implementation
* @dev Simplified version - Contract allows only Transfer and Burn Tokens
* @dev source: https://www.ethereum.org/token
*/
contract TokenERC20 { S' v/ a6 p0 z* ^
// Public variables of the token: W# Y0 S( N/ @0 i
string public name;
string public symbol;
uint8 public decimals = 18;: ]# l' Y: f6 p9 Q( v) |" c
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;. f# }1 }7 u, S4 B5 H6 _
// This creates an array with all balances
mapping (address => uint256) public balanceOf;: m( \7 H+ u7 c, Q
// This generates a public event on the blockchain that will notify clients" q0 w% o# j9 d1 x" f
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);4 D' A4 \: O' X1 c
/**
* Constructor function% i2 M2 K2 n* q/ X/ R1 x( S4 e
*
* Initializes contract with initial supply tokens to the creator of the contract" U5 k: W* K' v* @# \5 U
*/* ?& J- _* ?" Y% @4 n
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public: ~6 @& J$ }: n0 g
{! R- S7 M/ A3 @
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens+ g! B$ `2 G, c/ l/ n! x
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes" p: v3 ]8 b$ O
}% T+ \, s, C& ~/ y
/**9 l8 t5 t) O% V, l3 `
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value)) L% U& b3 J6 W% W8 I: O; y
internal2 z4 R8 T5 N8 e& U9 B* Z; n
{
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != address(0x0));) ^+ c% l( V* N7 ~) g- w
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows2 L9 _* u3 f" i2 v
require(balanceOf[_to] + _value >= balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];, Q! w: u' |0 Z' s v. X
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient) }# a6 X8 v+ p) R& [0 t
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail1 t+ P5 R' _7 a2 M8 K5 _; x. w
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);# _& i2 A( S& E2 l
}
/**" k$ W% i" [, K- G
* Transfer tokens* L% n \% G" m4 Q3 S4 J3 x" o
*
* Send `_value` tokens to `_to` from your account2 L* G5 ]3 p# h
*
* @param _to The address of the recipient
* @param _value the amount to send% k( I, N: ^0 c$ _/ W0 _3 g# o( t. N
*/
function transfer(address _to, uint256 _value)
public
returns (bool success) % p( w* M& g( X& k/ ^! T
{4 m( w9 e$ v ^6 g! C1 S u
_transfer(msg.sender, _to, _value);1 L9 e7 q3 @5 R
return true;- f: ], \0 r) E6 B2 s
}
/**" S3 D3 b% a- g3 E, }' n
* Destroy tokens
*- ~9 A& o$ k' d2 r( i9 ~
* Remove `_value` tokens from the system irreversibly7 @7 B' H$ P! X
*
* @param _value the amount of money to burn
*/3 i$ \1 r0 l6 }; F
function burn(uint256 _value)
public, _! _% t1 H, h4 K, h& W
returns (bool success)
{! S, z: \; n# l! c ~ }; u
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough6 C8 z" O: Y) L+ M$ F- @
balanceOf[msg.sender] -= _value; // Subtract from the sender; L4 e8 I3 n5 Y N' p& c: Y& [
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);& G2 w# z v9 O; _5 F9 o+ a5 Z$ m
return true;2 d9 m( x! F) W E `
}8 d2 i' s5 T% B4 W9 Y
}2 W- Y0 v. x" \ p
部署的话7 g, h& q% o: a0 t) }! O# ~
三个参数,代币总金额,名字,代币符号(随你写了)
* i5 m. o8 @0 _# H% ]' x
拷贝合约地址
添加代币,成功后就像我这样!
成为第一个吐槽的人