Javascript 了解以太坊ERC20令牌创建代码

Javascript 了解以太坊ERC20令牌创建代码,javascript,ethereum,solidity,erc20,Javascript,Ethereum,Solidity,Erc20,如果初始供应量=21000000.000000000000000000000000 (2100万)为什么要乘以10 totalSupply = initialSupply * 10 ** uint256(decimals); pragma-solidity^0.4.16; 合同所有{ 向公众所有人致辞; 函数owned()public{ 所有者=msg.sender; } 仅修改器所有者{ 要求(msg.sender==所有者); _; } 功能转移所有权(地址newOwner)仅所有者公共

如果初始供应量=21000000.000000000000000000000000 (2100万)为什么要乘以10

totalSupply = initialSupply * 10 ** uint256(decimals); 
pragma-solidity^0.4.16;
合同所有{
向公众所有人致辞;
函数owned()public{
所有者=msg.sender;
}
仅修改器所有者{
要求(msg.sender==所有者);
_;
}
功能转移所有权(地址newOwner)仅所有者公共{
所有者=新所有者;
}
}
接口令牌接收者{函数receiveApproval(地址_from,uint256 _值,地址_令牌,字节_extraData)public;}
合同标记ERC20{
//令牌的公共变量
字符串公共名称;
字符串公共符号;
uint8公共小数=18;
//18位小数是强烈建议的默认值,请避免更改
uint256公共总供给;
//这将创建一个包含所有余额的数组
映射(地址=>uint256)的公共余额;
映射(地址=>映射(地址=>uint256))公共津贴;
//这将在区块链上生成一个公共事件,通知客户
事件传输(地址索引自,地址索引至,uint256值);
//这会通知客户燃烧的量
事件刻录(从uint256值索引的地址);
/**
*构造函数
*
*使用初始供应令牌向合同创建者初始化合同
*/
功能标记ERC20(
uint256初始电源,
字符串标记名,
字符串标记符号
)公开的{
totalSupply=initialSupply*10**uint256(小数);//使用小数金额更新总供给
balanceOf[msg.sender]=totalSupply;//给创建者所有初始令牌
name=tokenName;//为显示目的设置名称
symbol=tokenSymbol;//为显示目的设置符号
}
/**
*内部转让,仅可由本合同调用
*/
函数(地址(发件人),地址(收件人),uint(值)内部{
//阻止传输到0x0地址。请改用burn()
需要(_to!=0x0);
//检查发送者是否有足够的信息
要求(余额[\u from]>=\u值);
//检查溢出
要求(平衡[_至]+_值>平衡[_至]);
//将此保存为将来的断言
uint以前的余额=[_-from]的余额+[_-to]的余额;
//从发送者身上减去
[\u from]=\u值的平衡;
//将其添加到收件人
[\u至]+=\u值的平衡;
转移(_从,_到,_值);
//断言用于使用静态分析来发现代码中的错误。它们永远不会失败
断言(余额[\u-from]+余额[\u-to]==以前的余额);
}
/**
*转账代币
*
*从您的帐户向“%u to”发送“%u value”代币
*
*@param_发送至收件人的地址
*@param\u表示要发送的金额
*/
功能传输(地址到,uint256值)公共{
_传输(msg.sender,_-to,_-value);
}
/**
*从其他地址转移令牌
*
*代表来自的“”向“”发送“”值“”令牌`
*
*@param_从发件人的地址发送
*@param_发送至收件人的地址
*@param\u表示要发送的金额
*/
函数transferFrom(地址_from,地址_to,uint256 _值)公共返回(布尔成功){
require(_value=_value);//检查发送方是否有足够的
余额[msg.sender]=\u值;//从发送方减去
totalSupply-=\u值;//更新totalSupply
烧录(msg.sender,_值);
返回true;
}
/**
*从其他帐户销毁代币
*
*以不可逆的方式从系统中删除“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\。
*
*@param_从发件人的地址发送
*@param_估计要烧掉的钱的数量
*/
函数burnFrom(地址、uint256值)公共返回(布尔成功){
require(balanceOf[\u from]>=\u value);//检查目标余额是否足够

require(_value它不是乘以10。它是乘以(10^18)。该行用于处理十进制偏移量,因为它是稳定的

ERC20代币不需要使用18位小数(或任何小数),但建议使用某种精度级别,因为代币的值可能会显著波动。18在大多数示例中使用,因为接收以太的合同的值在wei级别(1以太=10^18 wei)下工作

pragma solidity ^0.4.16;

contract owned {
    address public owner;

    function owned() public {
        owner = msg.sender;
    }

    modifier onlyOwner {
        require(msg.sender == owner);
        _;
    }

    function transferOwnership(address newOwner) onlyOwner public {
        owner = newOwner;
    }
}

interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }

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 it
    uint256 public totalSupply;

    // This creates an array with all balances
    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    // This generates a public event on the blockchain that will notify clients
    event Transfer(address indexed from, address indexed to, uint256 value);

    // This notifies clients about the amount burnt
    event Burn(address indexed from, uint256 value);

    /**
     * Constrctor function
     *
     * Initializes contract with initial supply tokens to the creator of the contract
     */
    function TokenERC20(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) public {
        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 purposes
    }

    /**
     * Internal transfer, only can be called by this contract
     */
    function _transfer(address _from, address _to, uint _value) internal {
        // Prevent transfer to 0x0 address. Use burn() instead
        require(_to != 0x0);
        // Check if the sender has enough
        require(balanceOf[_from] >= _value);
        // Check for overflows
        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
        balanceOf[_to] += _value;
        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);
    }

    /**
     * Transfer tokens
     *
     * Send `_value` tokens to `_to` from your account
     *
     * @param _to The address of the recipient
     * @param _value the amount to send
     */
    function transfer(address _to, uint256 _value) public {
        _transfer(msg.sender, _to, _value);
    }

    /**
     * Transfer tokens from other address
     *
     * Send `_value` tokens to `_to` in behalf of `_from`
     *
     * @param _from The address of the sender
     * @param _to The address of the recipient
     * @param _value the amount to send
     */
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
        require(_value <= allowance[_from][msg.sender]);     // Check allowance
        allowance[_from][msg.sender] -= _value;
        _transfer(_from, _to, _value);
        return true;
    }

    /**
     * Set allowance for other address
     *
     * Allows `_spender` to spend no more than `_value` tokens in your behalf
     *
     * @param _spender The address authorized to spend
     * @param _value the max amount they can spend
     */
    function approve(address _spender, uint256 _value) public
        returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        return true;
    }

    /**
     * Set allowance for other address and notify
     *
     * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
     *
     * @param _spender The address authorized to spend
     * @param _value the max amount they can spend
     * @param _extraData some extra information to send to the approved contract
     */
    function approveAndCall(address _spender, uint256 _value, bytes _extraData)
        public
        returns (bool success) {
        tokenRecipient spender = tokenRecipient(_spender);
        if (approve(_spender, _value)) {
            spender.receiveApproval(msg.sender, _value, this, _extraData);
            return true;
        }
    }

    /**
     * Destroy tokens
     *
     * Remove `_value` tokens from the system irreversibly
     *
     * @param _value the amount of money to burn
     */
    function burn(uint256 _value) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value);   // Check if the sender has enough
        balanceOf[msg.sender] -= _value;            // Subtract from the sender
        totalSupply -= _value;                      // Updates totalSupply
        Burn(msg.sender, _value);
        return true;
    }

    /**
     * Destroy tokens from other account
     *
     * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
     *
     * @param _from the address of the sender
     * @param _value the amount of money to burn
     */
    function burnFrom(address _from, uint256 _value) public returns (bool success) {
        require(balanceOf[_from] >= _value);                // Check if the targeted balance is enough
        require(_value <= allowance[_from][msg.sender]);    // Check allowance
        balanceOf[_from] -= _value;                         // Subtract from the targeted balance
        allowance[_from][msg.sender] -= _value;             // Subtract from the sender's allowance
        totalSupply -= _value;                              // Update totalSupply
        Burn(_from, _value);
        return true;
    }
}

/******************************************/
/*       ADVANCED TOKEN STARTS HERE       */
/******************************************/

contract MyAdvancedToken is owned, TokenERC20 {

    uint256 public sellPrice;
    uint256 public buyPrice;

    mapping (address => bool) public frozenAccount;

    /* This generates a public event on the blockchain that will notify clients */
    event FrozenFunds(address target, bool frozen);

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyAdvancedToken(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}

    /* Internal transfer, only can be called by this contract */
    function _transfer(address _from, address _to, uint _value) internal {
        require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
        require (balanceOf[_from] >= _value);               // Check if the sender has enough
        require (balanceOf[_to] + _value > balanceOf[_to]); // Check for overflows
        require(!frozenAccount[_from]);                     // Check if sender is frozen
        require(!frozenAccount[_to]);                       // Check if recipient is frozen
        balanceOf[_from] -= _value;                         // Subtract from the sender
        balanceOf[_to] += _value;                           // Add the same to the recipient
        Transfer(_from, _to, _value);
    }

    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintToken(address target, uint256 mintedAmount) onlyOwner public {
        balanceOf[target] += mintedAmount;
        totalSupply += mintedAmount;
        Transfer(0, this, mintedAmount);
        Transfer(this, target, mintedAmount);
    }

    /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
    /// @param target Address to be frozen
    /// @param freeze either to freeze it or not
    function freezeAccount(address target, bool freeze) onlyOwner public {
        frozenAccount[target] = freeze;
        FrozenFunds(target, freeze);
    }

    /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
    /// @param newSellPrice Price the users can sell to the contract
    /// @param newBuyPrice Price users can buy from the contract
    function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
        sellPrice = newSellPrice;
        buyPrice = newBuyPrice;
    }

    /// @notice Buy tokens from contract by sending ether
    function buy() payable public {
        uint amount = msg.value / buyPrice;               // calculates the amount
        _transfer(this, msg.sender, amount);              // makes the transfers
    }

    /// @notice Sell `amount` tokens to contract
    /// @param amount amount of tokens to be sold
    function sell(uint256 amount) public {
        require(this.balance >= amount * sellPrice);      // checks if the contract has enough ether to buy
        _transfer(msg.sender, this, amount);              // makes the transfers
        msg.sender.transfer(amount * sellPrice);          // sends ether to the seller. It's important to do this last to avoid recursion attacks
    }
}