溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ERC20代幣標(biāo)準(zhǔn)怎么實現(xiàn)

發(fā)布時間:2021-12-16 16:39:45 來源:億速云 閱讀:119 作者:iii 欄目:互聯(lián)網(wǎng)科技

本篇內(nèi)容介紹了“ERC20代幣標(biāo)準(zhǔn)怎么實現(xiàn)”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

pragma solidity ^0.4.20;

// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
contract NanToken {

    //Token
    //Methods
    //NOTE: Callers MUST handle false from returns (bool success). Callers MUST NOT assume that false is never returned!
    // 所有 returns (bool success) 的方法,都要考慮返回false的邏輯;

    //name
    //Returns the name of the token - e.g. "MyToken".
    //
    //OPTIONAL - This method can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present.
    function name() view returns (string name);

    //symbol
    //Returns the symbol of the token.E.g. "HIX".
    //
    //OPTIONAL - This method can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present.
    function symbol() view returns (string symbol);


    //decimals
    //Returns the number of decimals the token uses - e.g. 8, means to divide the token amount by 100000000 to get its user representation.
    //
    //OPTIONAL - This method can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present.
    // 精度
    function decimals() view returns (uint8 decimals);
    //totalSupply
    //Returns the total token supply.

    function totalSupply() view returns (uint256 totalSupply);
    //balanceOf
    //Returns the account balance of another account with address _owner.
    // 總量
    function balanceOf(address _owner) view returns (uint256 balance);

    //transfer
    //Transfers _value amount of tokens to address _to, and MUST fire the Transfer event. The function SHOULD throw if the _from account balance does not have enough tokens to spend.
    //
    //Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.
    // 從當(dāng)前地址轉(zhuǎn)指定量到指定地址
    // 若當(dāng)前地址沒有足夠的余額,應(yīng)該拋出異常
    // 每次調(diào)用成功,哪怕數(shù)量是0,都必須觸發(fā) Transfer 事件;
    function transfer(address _to, uint256 _value) returns (bool success);

    //transferFrom
    //Transfers _value amount of tokens from address _from to address _to, and MUST fire the Transfer event.
    //
    //The transferFrom method is used for a withdraw workflow, allowing contracts to transfer tokens on your behalf. This can be used for example to allow a contract to transfer tokens on your behalf and / or to charge fees in sub- currencies. The function SHOULD throw unless the _from account has deliberately authorized the sender of the message via some mechanism.
    //
    //Note Transfers of 0 values MUST be treated as normal transfers and fire the Transfer event.
    // 用于提幣邏輯;
    // 每次調(diào)用成功必須觸發(fā) Transfer 事件;哪怕數(shù)量是0;
    function transferFrom(address _from, address _to, uint256 _value) returns (bool success);

    //approve
    //Allows _spender to withdraw from your account multiple times, up to the _value amount. If this function is called again it overwrites the current allowance with _value.
    //
    //NOTE : To prevent attack vectors like the one described here[https://docs.google.com/document/d/1YLPtQxZu1UAvO9cZ1O2RPXBbT0mooh5DYKjA_jp-RLM/] and discussed here[https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729], clients SHOULD make sure to create user interfaces in such a way that they set the allowance first to 0 before setting it to another value for the same spender. THOUGH The contract itself shouldn't enforce it, to allow backwards compatibility with contracts deployed before
    // 允許指定的地址 分一次或多次從當(dāng)前地址提幣;若再次調(diào)用此方法,則重置數(shù)量
    // 為避免攻擊,客戶端實現(xiàn)時應(yīng)該先向某地址設(shè)置數(shù)量為0,再給相同的地址設(shè)置數(shù)量為 實際的數(shù)量;
    // 這為了向后兼容之前部署的合約,雖然不是強制要求
    function approve(address _spender, uint256 _value) returns (bool success);

    //allowance
    //Returns the amount which _spender is still allowed to withdraw from _owner.
    // 查詢 owner 授權(quán)給 spender 的剩余提幣數(shù)量
    function allowance(address _owner, address _spender) view returns (uint256 remaining);

    //Events
    //Transfer
    //MUST trigger when tokens are transferred, including zero value transfers.
    //
    //A token contract which creates new tokens SHOULD trigger a Transfer event with the _from address set to 0x0 when tokens are created.
    // token 被 成功 transfer 后,需要觸發(fā)Transfer 事件,哪怕數(shù)量為0;
    // 當(dāng)創(chuàng)建一個新的token后,應(yīng)該觸發(fā)Transfer 事件,
    event Transfer(address indexed _from, address indexed _to, uint256 _value);

    //Approval
    //MUST trigger on any successful call to approve(address _spender, uint256 _value).
    // 每次approve方法被成功調(diào)用后,都必須觸發(fā) Approval事件;
    event Approval(address indexed _owner, address indexed _spender, uint256 _value)};
}

“ERC20代幣標(biāo)準(zhǔn)怎么實現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI