区块链技术以太坊开发区块链研习社

ETH智能合约的调试手段

2018-03-12  本文已影响137人  剑有偏锋

一 调试手段

《1 变量查看

(1)变量可设置为public
address public withDrawAddress= 0x0;

(2)在rimix函数变量调试栏,点击变量同名按钮,即可查看当前值


image.png

《2 日志查看

(1)增加event事件的事件声明
event TransferWithDraw(uint balance);

(2)在要调用日志的地方,执行事件调用
TransferWithDraw(withDraw);

(3)执行函数时,remix在logs列,有TransferWithDraw日志输出语句


image.png

《3 使用remix的Debug功能

(1)在remix右侧,点击执行要执行的函数


image.png

(2)查看remix下方,有执行结果输出


image.png

(3)点击remix下半部分的“Debug”按钮,在右侧弹出Debug工具栏


image.png

主要关心当前的调试步进,还有当前的变量值。

二 完整实例代码

pragma solidity ^0.4.14;

contract testFundContract {
    address owner;
    address public withDrawAddress = 0x0;
    
    event TransferWithDraw(uint balance);
    
    function testFundContract() {
        owner = msg.sender;
    }
    
    function withDrawAddress(address a) {
        require(msg.sender == owner);
        withDrawAddress = a;
        
    }
    
    function addFund() payable returns (uint) {
        return this.balance;
    }
    
    function withDraw() {
        require(this.balance > 0);
        
        if (0x0  == withDrawAddress ){
            revert();
        }
        
        uint withDraw = this.balance;
        withDrawAddress.transfer(withDraw);
        TransferWithDraw(withDraw);
        
    }
    
}

上一篇 下一篇

猜你喜欢

热点阅读