亚洲日本永久一区二区_国产精品k频道网址导航_首页aⅴ色老汉中文字幕_免费深夜全片观看_9久久9毛片又大又硬又粗_国产精品成亚洲电影_日韩不用播放器的av_欧美特色特黄视频

區(qū)塊鏈開發(fā):如何從 Solidity 智能合約中發(fā)送和取款

區(qū)塊鏈開發(fā):如何從 Solidity 智能合約中發(fā)送和取款

在本文中,將展示一個(gè)智能合約的示例,可以向該智能合約發(fā)送交易,驗(yàn)證其余額,然后僅當(dāng)調(diào)用該函數(shù)的地址是所有者時(shí)才將資金提取到一個(gè)地址,通常稱為所有者模型。

與所有者創(chuàng)建智能合約

好的,首先要做的事情是:創(chuàng)建一個(gè)新文件,將其命名為 MySmartContract.sol(或者理想情況下,想要的任何名稱)并添加許可證和 Solidity 版本語句,如下所示:

然后在下面創(chuàng)建一個(gè)地址變量來存儲合約的所有者:

接下來,將創(chuàng)建一個(gè)構(gòu)造函數(shù),將部署合約的地址存儲為所有者。構(gòu)造函數(shù)是一個(gè)特殊函數(shù),僅在部署合約時(shí)調(diào)用一次。將使用 msg 對象的 sender 屬性。msg.sender 是一個(gè)內(nèi)置變量,用于存儲在任何給定時(shí)間與智能合約交互的地址。

// SPDX-License-Identifier:GPL-3.0
pragma solidity
contract MySmartContract {
    // owner of this contract
    address public contractOwner;
    // constructor is called during contract deployment
    constructor(){
        // assign the address that is creating
        // the transaction for deploying contract
        contractOwner = msg.sender'
    }
}

繼續(xù)保存合約,編譯和部署。點(diǎn)擊 contractOwner 按鈕,可以看到部署合約的地址現(xiàn)在是 owner 了。

向智能合約匯款

接下來,將創(chuàng)建一個(gè)函數(shù),允許任何地址向合約匯款。

// SPDX-License-Identifier:GPL-3.0
pragma solidity
contract MySmartContract {
    // owner of this contract
    address public contractOwner;
    // constructor is called during contract deployment
    constructor(){
        // assign the address that is creating
        // the transaction for deploying contract
        contractOwner = msg.sender'
    }

    function sendMoneyToContract() public payable {}
    function getBalance() public view returns(uint){
        return address(this).balance;
    }
}

使用關(guān)鍵字 payable 來指定函數(shù)可以接收以太幣。

在 getBalance 函數(shù)中,關(guān)鍵字 this 指的是這個(gè)特定的合約(MySmartContract)。

現(xiàn)在將繼續(xù)部署合約并測試是否可以將以太幣發(fā)送到合約并檢查合約的余額。

從智能合約中提取所有資金

現(xiàn)在,創(chuàng)建一個(gè) withdrawAll 函數(shù)來傳遞希望匯款的地址。

// SPDX-License-Identifier:GPL-3.0
pragma solidity
contract MySmartContract {
    // owner of this contract
    address public contractOwner;
    // constructor is called during contract deployment
    constructor(){
        // assign the address that is creating
        // the transaction for deploying contract
        contractOwner = msg.sender'
    }

    function sendMoneyToContract() public payable {}
    function getBalance() public view returns(uint){
        return address(this).balance;
    }
    function withdrawAll(address payable _to) public {
        _to.transfer(address(this).balance);
    }
}

當(dāng)部署和測試合約時(shí),會注意到任何人都可以運(yùn)行這個(gè) withdrawAll 函數(shù),這是不安全的,下面增加限制只允許 sendcontract 所有者執(zhí)行此功能。使用 require 來檢查條件,如果條件不滿足則拋出異常。

在這種情況下,檢查調(diào)用函數(shù)的地址是否與合約所有者相同。如果失敗,它將回滾事務(wù)。該合約現(xiàn)在滿足 Owner 模型,在該模型中,它檢查調(diào)用函數(shù)的地址是否是所有者,然后才允許它繼續(xù)進(jìn)行。

// SPDX-License-Identifier:GPL-3.0
pragma solidity
contract MySmartContract {
    // owner of this contract
    address public contractOwner;
    // constructor is called during contract deployment
    constructor(){
        // assign the address that is creating
        // the transaction for deploying contract
        contractOwner = msg.sender'
    }

    function sendMoneyToContract() public payable {}
    function getBalance() public view returns(uint){
        return address(this).balance;
    }
    function withdrawAll(address payable _to) public {
        require(contractOwner == _to);
        _to.transfer(address(this).balance);
    }
}

這就是這個(gè)簡單的智能合約,它允許向它發(fā)送資金,并且只允許所有者從智能合約中提取所有資金。