首页 > 解决方案 > 如何在不调用任何函数的情况下让合约采取行动?

问题描述

这是一个很难解释的问题,我会尽力而为:

我写了一份更新房地产所有权的合同。我有卖家(所有者)、买家和其他一些功能。

但我希望这份合同是一份临时合同。由于卖方部署它,买方有 30 秒(例如)签署合同。如果买方不及时签订合同,合同就会做一些事情(例如自毁)。签名是将 bool 属性从 false 更改为 true。

我写了一个修饰符来解释我的目标:

 modifier upTo30Seconds
    {
        if( now-timeOfContractCreation > 30)
        {
            if(isContractPayed && buyerSign == false)
            {
                emit  notifyCancelOffer(address(this), buyerAddress , oldOwnerAddress); //notifyCancelOffer 
                selfdestruct(buyerAddress); //Destruct the contract and return ether to the buyer

            }
        }
        _;
    } 
    //'timeOfContractCreation' is another property, initialized in the constructor, and its means the time of deployment (since 01/01/1970)

    //'now' is a solidity method which returns the time passed in seconds since 01/01/1970 

此修饰符在函数调用中效果很好,但我希望这些操作能够自动执行。

我的要求有点类似于线程运行。是否可以使用solidity来做到这一点?

标签: solidity

解决方案


没有交易就不可能自动更改合约状态/数据。但是您可以在收到下一个事务时检查时间戳,如果它从创建开始超过 30 秒,则忽略它并运行自毁。

另外,请注意,以太坊中的时间戳不是交易创建的实际时间戳,而是该块的时间戳。


推荐阅读