我正在Remix建立一个批准合同,并且在这个问题上被困住了。
当我试图使用address.transfer(value)传输魏时,它会抛出一个错误。我已经在下面的代码中标记了它。
也许这个错误与构造函数中声明的struct Request有关,因为当.transfer()失败时,混合调试器就会在那里结束,但是我找不到解释。
pragma solidity ^0.4.17;
contract Campaign {
struct Request {
string description;
uint32 value;
address recipient;
bool complete;
uint approvalCount;
mapping(address => bool) approvals;
}
Request[] public requests;
address public manager;
uint32 public minimumContribution;
// tutorial uses 'approvers' not 'contributors'
mapping(address => bool) public contributors;
uint32 public contributorCount;
modifier restricted {
require(msg.sender == manager );
_;
}
constructor(uint32 minimum) public {
manager = msg.sender;
minimumContribution = minimum;
}
function contribute() public payable{
require(msg.value > minimumContribution);
contributors[msg.sender] = true; // because contributor is
contributorCount++;
}
function createRequest(string description, uint32 value, address recipient)
public restricted {
Request memory newRequest = Request({
description: description,
value: value,
recipient: recipient,
complete: false,
approvalCount: 0
});
requests.push(newRequest);
}
function approveRequest(uint16 index) public {
Request storage r = requests[index];
// make sure the address has contributed, but has not approved this one yet
require(contributors[msg.sender]);
require(!r.approvals[msg.sender]);
r.approvals[msg.sender] = true;
r.approvalCount++;
}
function finalizeRequest(uint16 index) public restricted {
Request storage r = requests[index];
// make sure more than 50% of contributors approved this request
require(r.approvalCount > (contributorCount / 2));
//make sure that this request has not been completed before
require(!r.complete);
// FAILS ON THIS LINE. Remix debugger goes through this line then ends on line 3 of the contract, in the constructor function.
r.recipient.transfer(r.value);
// mark this request as completed
r.complete = true;
}
}注意,在.transfer()上失败后,调试器跳转到构造函数,其中创建了struct Request {...}。(这就是问题所在吗?)
我认为它可能是调用.transfer()的函数,所以我试图通过在一个单独的函数中调用它来隔离传输,但是它仍然失败了。
不确定是否有用,但我输入的地址(address.transfer()中的地址)是从javascriptVM中混合创建的备用地址中复制的。
发布于 2018-06-03 08:01:28
r.value规定的魏氏金额超过了合同中规定的数额。我在合同中增加了一行,以确保合同中有足够的钱:
require(address(this).balance > r.value);
https://ethereum.stackexchange.com/questions/50230
复制相似问题