区块链代币ERC-20源码,如此简单
朱丹铎
发表于 2023-1-11 19:10:41
119
0
0
*1.
启动网页的remix-ide即可6 l* m5 j& ]" @
https://remix.ethereum.org/#appVersion=0.7.7&optimize=false&version=soljson-v0.5.1+commit.c8a2cb62.js |' ?; h+ q0 k# l! |& J# y! N9 x/ i9 u
pragma solidity ^0.5.10;
/**
* @title Token ERC20 implementation7 f3 C# V4 z5 d5 b6 D$ u# N
* @dev Simplified version - Contract allows only Transfer and Burn Tokens; z2 X# l/ I+ n/ S9 ~
* @dev source: https://www.ethereum.org/token
*/
contract TokenERC20 {) X' Z1 v6 c& C: o8 f) z, X% I: e
// Public variables of the token
string public name;$ _" z# h$ G- {* X9 F Q$ d& S
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances! ~( z% H3 d- x/ {5 l. Y4 j9 T
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);% a+ [1 t2 C' j+ A$ g/ G# ]" Y
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
/**
* Constructor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/. a2 S3 R' w9 i
constructor(uint256 initialSupply, string memory tokenName, string memory tokenSymbol)
public: B0 w8 v6 ^5 i
{
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
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 purposes5 @5 |! z& F' N! s! [6 q- l/ v7 W
}
/**
* Internal transfer, only can be called by this contract
*/5 A) n3 _9 ]+ l; m+ c
function _transfer(address _from, address _to, uint _value)
internal
{
// Prevent transfer to 0x0 address. Use burn() instead( D3 V# S f) Z
require(_to != address(0x0));* }4 m+ r: F# H; {9 L+ m
// Check if the sender has enough
require(balanceOf[_from] >= _value);5 ?! g) X0 L! l* M) [) G# T; g
// Check for overflows1 w0 F4 ~" M3 X" `+ Y
require(balanceOf[_to] + _value >= balanceOf[_to]);; o) m1 ~8 t7 U' j% W
// Save this for an assertion in the future3 V* F2 f" r1 e: J* p1 _; a) }
uint previousBalances = balanceOf[_from] + balanceOf[_to];( |* l F# \" R( U
// Subtract from the sender
balanceOf[_from] -= _value;+ w5 ^* r8 v/ D+ }# k
// Add the same to the recipient
balanceOf[_to] += _value;8 [* G) u; d8 v4 F* Y @
emit Transfer(_from, _to, _value);' _# G' E4 I9 C$ T; T
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);/ y& i" O: [7 W) d2 |
}
/**
* Transfer tokens
*; V' ?& i8 W$ c. i
* Send `_value` tokens to `_to` from your account" J5 e: t% W# {1 n2 S
*
* @param _to The address of the recipient
* @param _value the amount to send6 E9 f. ], J, d/ @ g
*/
function transfer(address _to, uint256 _value)
public 6 I M8 c2 Q# w6 W4 v2 U6 p
returns (bool success) 3 n j4 {7 K2 e; I' n: k
{
_transfer(msg.sender, _to, _value);
return true;
}
/**' ?! S5 e7 p2 O i7 M K. ~4 ?5 O
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly+ J5 l0 p, K8 ~0 v N& i5 J
*! O( K8 l! f* F7 j
* @param _value the amount of money to burn! `0 Z$ o7 U% n/ n; l
*/5 I8 d t: o$ s
function burn(uint256 _value)- S1 \6 s- ?4 b+ e9 d+ K
public
returns (bool success)7 A! P) K% f N9 c9 K4 M
{8 G$ K, `& _. X; |( A* [
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough4 H8 J* u& x: d4 J4 f+ P2 t4 A$ ^$ J
balanceOf[msg.sender] -= _value; // Subtract from the sender7 S& }+ v( }. B, i6 F1 l. |
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;
}( v( f1 f! C2 O) C; b
}
部署的话9 m% X) z( a6 ^; T+ ^% Q: R
7 z% e' z0 d! |. D8 k R
三个参数,代币总金额,名字,代币符号(随你写了)
! _& @/ F# X* l: ~, y3 ^: S7 o+ n
拷贝合约地址8 f- t4 B0 }' p0 n
添加代币,成功后就像我这样!
成为第一个吐槽的人