定时广告稳定性合同



基本上,我试图使智能合约只有在冷却计时器设置为0时才能获得0.05 eth。我正在创建的Dapp是一个先到先得的eth广告服务,用户可以在Dapp中上传图像或gif,然后支付0.05 eth来触发广告运行x时间。当时间用完后,下一个用户可以购买他们的广告位。

计时器似乎工作,但我不能让计时器开始支付。

我真的很感激你的帮助,这是我到目前为止所得到的:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract AdEth {
//This sets up the name of the ad and if it is running, set to false by default  
//Variables for start, end, cooldown (cool down can be changed here)
address payable public owner;
uint _start;
uint _end;
uint cooldownTime = 1 minutes;
constructor() {
owner = payable(msg.sender);
}
modifier timerOver {
require(block.timestamp <= _end, "The Cooldown is over");
_;
}
modifier onlyWhileOpen {
require(block.timestamp >= _start && block.timestamp <= _end);
_;
}
function start() public {
_start = block.timestamp;
end(cooldownTime);
}
function end(uint totalTime) public {
_end = totalTime + _start;
}
function getTimeLeft() public view returns(uint) {
return _end - block.timestamp;
}
receive() external payable {

}
function receiveAdPayment() payable public returns (bool) {
require(msg.value == 0.05 ether, "Not enough ether. 0.05 Needed.");
require(cooldownTime == 0, "There is currently an add running. Please wait until the cooldown is finished.");
msg.sender.transfer(0.05 ether);
start();
return true;
}
function withdrawAll(uint _amount) external {
require(msg.sender == owner, "Caller is not the owner.");
payable(msg.sender).transfer(_amount);
}
function getBalance() external view returns (uint) {
return address(this).balance;
}
}

------------ 编辑5.11.21 --------------

我已经为block交换了所有的时间戳。数字和所有东西都按预期工作。但是,我希望智能合约在cooldownTime到期时自动调用setRunning()函数。这可能吗?或者这是最好的结果?

感谢所有的帮助!

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract AdEth {
//This sets up the name of the ad and if it is running, set to false by default
//Variables for start, end, cooldown (cool down can be changed here)
address payable public owner;
uint _start;
uint _end;
//The number is in BLOCKS (~15 sec each, rinkeby approx ~1 min)
uint cooldownTime = 4;
bool running;
constructor() {
owner = payable(msg.sender);
}
function start() internal {
_start = block.number;
_end = block.number + cooldownTime;
running = true;
}

function getTimeLeft() public view returns(uint) {
return _end - block.number;
}
//This allows the owner to set "running" to false only if the required amount of cooldown blocks is reached.
function setRunning() public {
require(msg.sender == owner, "You are not the owner.");
require(block.number > _end, "Wait for the cooldown to expire before you can reset running to false.");
running = false;
}
function isRunning() public view returns (bool) {
return running;
}
function receiveAdPayment() payable public {
require(msg.value >= 0.05 ether, "At Least 0.05 ETH Needed.");
require(block.number > _end, "There is currently an ad running. Please wait until the cooldown is finished.");
require(running != true, "The ad time may have run out, but has not been reset by Admin.");
start();
}
function withdraw(uint _amount) external {
require(msg.sender == owner, "Caller is not the owner.");
payable(msg.sender).transfer(_amount);
}
function getBalance() external view returns (uint) {
return address(this).balance;
}
}

——编辑5.18.22

好的,这是最终产品。被推到一堆方向,选择了下面的代码。它在remix中运行良好。

谢谢大家!

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import '@openzeppelin/contracts/access/Ownable.sol';
contract AdEth is Ownable {
//This sets up the name of the ad and if it is running, set to false by default
//Variables for start, end, cooldown (cool down can be changed here)
uint _start;
uint _end;
uint cooldownTime = 3 minutes;
uint runNumber = 0;
function start() internal {
_start = block.timestamp;
_end = block.timestamp + cooldownTime;
runNumber++;
}
function getTimeLeft() public view returns(uint) {
return _end - block.timestamp;
}

function adRunNumber() public view returns (uint) {
return runNumber;
}
function receiveAdPayment() payable public {
require(msg.value >= 0.05 ether, "At Least 0.05 ETH Needed.");
require(block.timestamp > _end, "There is currently an ad running. Please wait until the cooldown is finished.");
start();
}
function withdraw(uint _amount) external onlyOwner {
payable(msg.sender).transfer(_amount);
}
function getBalance() external view returns (uint) {
return address(this).balance;
}
}

第一件事:永远不要使用block.timestamp。它没有经过验证,可能会被滥用。使用块号作为时间戳的一种形式。

第二件事:start()应该是内部的。目前,任何人都可以调用它,这似乎不是故意的行为。

最后,你的问题是:看起来你有两种付款方式。第一个是将被调用的。删除receive()功能

最新更新