我正在尝试通过将一些以太币发送到合同中,更改要拒绝的状态并基本上将退款(转移合同余额)发回给请求者来处理solidity文档中突出显示的提款方式。合同在混音中部署良好,并且大多数功能和吸气剂都在起作用。但是,当我尝试在混音中调用退款功能时,我在eth_sendRawTransaction上收到了一个json-rpc错误,说不出话了。我试图在ganache 1.3上使用在带有metamask的chrome上注入web3来做到这一点。有谁知道我如何使它工作?谢谢!
pragma solidity >=0.5.4 <0.6.0;
contract TestContract {
uint public value;
address payable public requestor;
enum State { Requested, Approved, Rejected, Completed }
State public state;
mapping (address => uint) pendingWithdrawals;
constructor(
address payable _requestor
) public payable {
requestor = _requestor;
state = State.Requested;
value = msg.value;
}
modifier onlyRequestor() {
require(
msg.sender == requestor,
"Only requestor can call this."
);
_;
}
modifier inState(State _state) {
require(
state == _state,
"Invalid state."
);
_;
}
event Rejected();
event Approved();
event DataReceived();
function reject() public inState(State.Requested) {
emit Rejected();
state = State.Rejected;
pendingWithdrawals[requestor] += address(this).balance;
}
function refund() public onlyRequestor inState(State.Rejected) {
uint amount = pendingWithdrawals[msg.sender];
pendingWithdrawals[msg.sender] = 0;
msg.sender.transfer(amount);
}