// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract CrowdFunding
{
struct Investor
{
address addr;
uint amount;
}
address public owner;
uint public NumOfInvestors;
uint public deadline;
string public status;
bool public isOver;
uint public goalAmount;
uint public totalAmount;
mapping (uint => Investor) public investors;
modifier onlyOwner ()
{
require(msg.sender == owner);
_;
}
function crowdfunding(uint _duration, uint _goalAmount) public
{
owner == msg.sender;
deadline = now + _duration;
goalAmount = _goalAmount;
status = "Funding";
isOver = "false";
NumOfInvestors = 0;
totalAmount = 0;
}
function fund() payable public
{
require(!isOver);
Investor inv = investors[NumOfInvestors++];
inv.addr = msg.sender;
inv.amount = msg.value;
totalAmount += inv.amount;
}
function checkGoalReached () public onlyOwner
{
require(!isOver);
require(now >= deadline);
if(totalAmount >= goalAmount)
{
status = "Campaign succeeded.";
isOver = true;
if(!owner.send(this.balance))
{
throw;
}
else
{
uint i = 0;
status = "Campaign Failed.";
isOver = true;
while (i <= NumOfInvestors)
{
if(!investors[i].addr.send(investors[i].amount))
{
throw;
}
i++;
}
}
}
function kill() public onlyOwner
{
selfdestrct(owner);
}
}
}
我写了一个契约,但是在函数kill()的第一和第二行出现了错误。
显示"期望'(',但得到标识符"在第一行,和"解析错误:不匹配的输入"{"期望{";","="}[undefined] "在第二行
我仔细检查了我的代码结构,并阅读了一些来自StackOverflow的文章。
对于第一行显示的问题,那些文章说可能遗漏了"{"或"}"。但我没有射偏。(如果我真的想念他们,我就是一个该死的傻瓜。
关于第二行显示的问题,我只是不知道为什么它是错误的。会是版本问题吗?
请帮帮我。请求帮助。
您的
function kill() public onlyOwner
{
selfdestrct(owner);
}
在另一个函数内部,将其移到外部,一切都会正常工作。请检查闭合括号}
。此外,您还需要修复selfdestruct
的错字。工作代码:
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract CrowdFunding
{
struct Investor
{
address payable addr;
uint amount;
}
address payable public owner;
uint public NumOfInvestors;
uint public deadline;
string public status;
bool public isOver;
uint public goalAmount;
uint public totalAmount;
mapping (uint => Investor) public investors;
modifier onlyOwner ()
{
require(msg.sender == owner);
_;
}
function crowdfunding(uint _duration, uint _goalAmount) public
{
owner == msg.sender;
deadline = block.timestamp + _duration;
goalAmount = _goalAmount;
status = "Funding";
isOver = false;
NumOfInvestors = 0;
totalAmount = 0;
}
function fund() payable public
{
require(!isOver);
Investor storage inv = investors[NumOfInvestors++];
inv.addr = payable(msg.sender);
inv.amount = msg.value;
totalAmount += inv.amount;
}
function checkGoalReached () public onlyOwner
{
require(!isOver);
require(block.timestamp >= deadline);
if(totalAmount >= goalAmount)
{
status = "Campaign succeeded.";
isOver = true;
address payable self = payable(address(this));
uint256 balance = self.balance;
if(!owner.send(balance))
{
revert();
}
else
{
uint i = 0;
status = "Campaign Failed.";
isOver = true;
while (i <= NumOfInvestors)
{
if(!investors[i].addr.send(investors[i].amount))
{
revert();
}
i++;
}
}
}
}
function kill() public onlyOwner
{
selfdestruct(owner);
}
}