Solidity__合約操作代幣合約

首先部署下面的代幣合約,創(chuàng)建一個(gè)5億的SB代幣

ERC20代幣并不能像Ether一樣使用sendTo.transfer(amt)來轉(zhuǎn)賬,ERC20代幣只能通過token中定義的transfer方法來轉(zhuǎn)賬,每個(gè)賬戶的余額信息也只保存在token合約的狀態(tài)變量中撬统。如果要使用除token合約之外的合約進(jìn)行ERC20代幣的轉(zhuǎn)賬轰异,那就需要這個(gè)合約能夠調(diào)用ERC20代幣合約中的transfer方法。

pragma solidity ^0.4.15;

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

contract RCCCToken {
    // Public variables of the token
    string public name = "SbToken";
    string public symbol = "SB";
    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 RCCCToken() public {
        totalSupply = 500000000 * 10 ** uint256(decimals);  // Update total supply with the decimal amount
        balanceOf[msg.sender] = totalSupply;                // Give the creator all initial tokens
    }

    /**
     * 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` on 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 on 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 on 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;
    }
}

接著創(chuàng)建另一個(gè)合約來調(diào)用該代幣合約的接口

pragma solidity ^ 0.4.0;
/*
+------------------------------------------------------------------------------+
|                                                                              |
|      XX               XXXXX XXXXXX          XXXXXXXXXXX        XXXXXXXXXX    |
|     XX XX           XXX   XXX   XXX         X         X        X        X    |
|    XX   XX          X      X      X         XXXXXXXXXXX        X        X    |
|   XXXXXXXXX         X             X         X                  X        X    |
|  XX       XX        X             X         X                  X        X    |
| XX          X       X             X         XXXXXXXXXXX        X        X    |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
*/
contract RCCCToken{      //interface也可以围段,目前還不知道其中區(qū)別,后期更新
    //以下是該合約實(shí)現(xiàn)的方法和公用變量
    string public name = "RCCC Token";
    string public symbol = "RCCC";
    uint8 public decimals = 18;
    uint256 public totalSupply;

    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    function transfer(address _to, uint256 _value) public;
    function transferFrom(address _from, address _to, uint256 _value) public returns (bool success);
    function approve(address _spender, uint256 _value) public;
    function approveAndCall(address _spender, uint256 _value, bytes _extraData);
    function burn(uint256 _value) public returns (bool success);
    function burnFrom(address _from, uint256 _value) public returns (bool success);
}
contract A {
    RCCCToken public RCCC = RCCCToken(0x3110700EC98A8dd4e576c3e82BB63007a688D997);//初始化該合約
    uint256 public a;//創(chuàng)建的合約代幣總數(shù)
    function getbalance() public payable returns(uint)
    {
        a=RCCC.totalSupply();//查詢該代幣總量賦值給a
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末投放,一起剝皮案震驚了整個(gè)濱河市奈泪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌灸芳,老刑警劉巖涝桅,帶你破解...
    沈念sama閱讀 221,695評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異烙样,居然都是意外死亡冯遂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,569評論 3 399
  • 文/潘曉璐 我一進(jìn)店門谒获,熙熙樓的掌柜王于貴愁眉苦臉地迎上來蛤肌,“玉大人壁却,你說我怎么就攤上這事÷阕迹” “怎么了展东?”我有些...
    開封第一講書人閱讀 168,130評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長炒俱。 經(jīng)常有香客問我盐肃,道長,這世上最難降的妖魔是什么权悟? 我笑而不...
    開封第一講書人閱讀 59,648評論 1 297
  • 正文 為了忘掉前任砸王,我火速辦了婚禮,結(jié)果婚禮上峦阁,老公的妹妹穿的比我還像新娘谦铃。我一直安慰自己,他們只是感情好榔昔,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,655評論 6 397
  • 文/花漫 我一把揭開白布驹闰。 她就那樣靜靜地躺著,像睡著了一般件豌。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上控嗜,一...
    開封第一講書人閱讀 52,268評論 1 309
  • 那天茧彤,我揣著相機(jī)與錄音,去河邊找鬼疆栏。 笑死曾掂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的壁顶。 我是一名探鬼主播珠洗,決...
    沈念sama閱讀 40,835評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼若专!你這毒婦竟也來了许蓖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,740評論 0 276
  • 序言:老撾萬榮一對情侶失蹤调衰,失蹤者是張志新(化名)和其女友劉穎膊爪,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嚎莉,經(jīng)...
    沈念sama閱讀 46,286評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡米酬,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,375評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了趋箩。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赃额。...
    茶點(diǎn)故事閱讀 40,505評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡加派,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出跳芳,到底是詐尸還是另有隱情芍锦,我是刑警寧澤,帶...
    沈念sama閱讀 36,185評論 5 350
  • 正文 年R本政府宣布筛严,位于F島的核電站醉旦,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏桨啃。R本人自食惡果不足惜车胡,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,873評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望照瘾。 院中可真熱鬧匈棘,春花似錦、人聲如沸析命。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,357評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鹃愤。三九已至簇搅,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間软吐,已是汗流浹背瘩将。 一陣腳步聲響...
    開封第一講書人閱讀 33,466評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留凹耙,地道東北人姿现。 一個(gè)月前我還...
    沈念sama閱讀 48,921評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像肖抱,于是被迫代替她去往敵國和親备典。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,515評論 2 359