区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
116
0
0
*1. X) R( S! y; @* C- S+ y
启动网页的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;
/**
* @title Token ERC20 implementation
* @dev Simplified version - Contract allows only Transfer and Burn Tokens2 ~. I6 }% m B; |$ V7 ]1 o; n; E
* @dev source: https://www.ethereum.org/token
*/
contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it0 }! @3 e& P2 `& v$ |
uint256 public totalSupply;- p4 L* r/ @8 I5 @; Y; K
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);/ b& ~# a3 b6 l& V
// This notifies clients about the amount burnt. e% H4 g! W5 s. J, s# b3 r; ^/ n
event Burn(address indexed from, uint256 value);
/**4 y. N# ^3 i( a; I0 w# L
* Constructor function5 {1 W* v! N; F0 N( J5 k
*
* Initializes contract with initial supply tokens to the creator of the contract& ~; _8 }7 r! G ?; J
*/! F& c c; z% }6 l
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public: P% l) o2 k; S: h' {4 g
{
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount$ z) c! q1 W+ C
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes8 [/ X# ?# g4 i5 J6 N
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)
internal
{4 J$ o9 \( A3 L; k% e9 X
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != address(0x0));
// Check if the sender has enough7 v' e+ q. I: N; W1 f6 n
require(balanceOf[_from] >= _value);) J# U- y, ~4 \) Z# y* d0 _' [
// Check for overflows J4 ? N5 L* h2 j0 j; B
require(balanceOf[_to] + _value >= balanceOf[_to]);- b7 {4 O1 D3 Z* n* }
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender! h; J4 ]/ P" i$ d& R- e
balanceOf[_from] -= _value;
// Add the same to the recipient9 Z* t* V9 d: T. ?! B/ v
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);% u3 B6 M1 j" M j: ^$ Z' u" s
// Asserts are used to use static analysis to find bugs in your code. They should never fail+ z" R t0 B7 z
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}4 f; g5 a R$ ]: ]* h' }
/**! B$ J' D7 |7 p3 F) {/ R% }0 y0 k( Z
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account( Y" A% _5 Z: `
*
* @param _to The address of the recipient
* @param _value the amount to send
*/) V3 R a4 @2 l
function transfer(address _to, uint256 _value)
public
returns (bool success)
{
_transfer(msg.sender, _to, _value);
return true;% |4 }4 U2 W) Y) D
}" ]5 q/ U4 K+ O- O, m
/**. `8 N/ U9 Q! M9 O+ q, R2 E
* Destroy tokens' \9 K- ]+ w$ W% }+ i7 }2 e% i
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn% D9 Z3 v$ @8 w9 h8 X% A1 A
*/- ~$ p6 F H" h
function burn(uint256 _value)
public
returns (bool success): n9 H' q5 d0 q% _& w! S2 j
{2 v9 l! k: h' h* z
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough5 [% \4 C6 U# k7 ~
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply1 e' c8 N+ ^" m7 p: g( |: ^; l
emit Burn(msg.sender, _value);! H4 G) d$ ^9 l+ i
return true;9 P2 I, W! q8 n9 l3 {: d
}
}
部署的话2 I- J2 ]" j5 l- z4 v
; Y& `5 F4 P% b* A- @, m
三个参数,代币总金额,名字,代币符号(随你写了)
# r0 Z# B- i$ H; S+ l3 P
拷贝合约地址
1 @6 p. G8 |# [5 z. s+ k
添加代币,成功后就像我这样!
成为第一个吐槽的人