我有一个简单的合同,我用它构建了一个小型的with应用程序。合同如下:
contract MyContract {
address public creator;
function MyContract() {
creator = msg.sender;
}
function reject() {
selfdestruct(creator);
}
function send(address target, uint256 amount) {
if (!target.send(amount)) throw;
}
function destroy(address target) {
selfdestruct(0x0000000000000000000000000000000000000000);
}
}这总是有效的--我正在使用MetaMask来签署付款协议和合同。从一周或更多的时间开始,我从Web3:Cannot send value to non-payable constructor收到了控制台中的一个错误
然后我开始四处搜索,发现一个函数需要是payable,并且在我的合同中应该有一个备用函数。所以我定制了一些合同,这就是我现在要做的:
pragma solidity ^0.4.9;
contract Oursurance {
address public creator;
uint x;
function() payable { x = 1; }
function Oursurance() payable {
creator = msg.sender;
}
function reject() payable {
selfdestruct(creator);
}
function send(address target, uint256 amount) payable {
if (!target.send(amount)) throw;
}
function destroy(address target) payable {
selfdestruct(0x0000000000000000000000000000000000000000);
}
}我知道并不是每个函数都应该是payable,但只是为了确保我已经将它添加到了所有的东西中。只是需要它再起作用。
我仍然得到这个错误与上述(编辑)合同。
发布于 2017-02-14 02:04:55
稍微解释一下。我把“拒绝”重新命名为“杀死”,并从我不认为应该拥有的函数中删除应付款。
向合同中注入资金有两种方式:
payable )将运行。您还可以触发来自JavaScript web3.eth.sendTransaction({from: me, to: contract, value: amount})或与contractAddress.send(amount)的另一项合同的退路。当未指定函数或指定的函数不存在时,契约将执行回退函数。
我在回退函数中添加了一个事件发射器,以展示如何将回退用于有用的东西。为了保持一致性,我也将其放入构造函数中,并为send()添加了类似的事件。
我们可以有把握地说,“必要的会计”(在这种情况下的日志)是对所有的资金到达和离开本合同。没有什么能通过像send()这样的不被设计用来处理收据的函数潜入。
pragma solidity ^0.4.9;
contract Oursurance {
address public creator;
event LogFundsReceived(address sender, uint amount);
event LogFundsSent(address receiver, uint amount);
function() payable {
LogFundsReceived(msg.sender, msg.value);
}
function Oursurance() payable {
creator = msg.sender;
LogFundsReceived(msg.sender, msg.value);
}
function kill() {
selfdestruct(creator);
}
function send(address target, uint256 amount) {
if (!target.send(amount)) throw;
LogFundsSent(target, amount);
}
}希望能帮上忙。
https://ethereum.stackexchange.com/questions/12119
复制相似问题