checks/effects/patterns 的 Solidity 安全用户提款
Solidity secure user withdrawal with checks/effects/patterns
checks/effects/interactions 模式能否用于安全地向用户退款(代替单独的提款功能)?
EG 在投标系统的情况下,如果用户 2 的出价高于用户 1 并且用户 1 收到了退款,checks/effects/patterns 是否足够安全以将用户 1 的款项退回?
例如,
contract auction {
address highestBidder;
uint highestBid;
function bid() payable external {
require(msg.value >= highestBid);
prevHighestBidder = highestBidder
prevHighestBid = highestBid
highestBidder = msg.sender;
highestBid = msg.value;
(bool success, ) = prevHighestBidder.call.value(prevHighestBid)("");
require(success, "Transfer failed.")
}
}
我知道最好创建一个单独的提款功能,但我希望能降低用户的 gas 成本。
如果接收方(prevHighestBidder
)是拒绝交易的合约怎么办?这将有效地破坏你的游戏,因为在他们成功调用 bid()
函数之后没有人。
他们的合同可能就这么简单
contract BlockBids {
function callBid() payable {
// call the bid() function with value
address(<yourContract>).call{value: msg.value}('0x1998aeef');
}
receive () external payable {
revert();
}
}
我的建议是不要检查prevHighestBidder
是否收到了ETH。或者添加一个回退机制,在接收者拒绝交易的情况下将 ETH 提取到您的地址,这样您就可以在他们证明对获胜地址的所有权后将其发送给他们(例如通过签署消息)。
checks/effects/interactions 模式能否用于安全地向用户退款(代替单独的提款功能)?
EG 在投标系统的情况下,如果用户 2 的出价高于用户 1 并且用户 1 收到了退款,checks/effects/patterns 是否足够安全以将用户 1 的款项退回?
例如,
contract auction {
address highestBidder;
uint highestBid;
function bid() payable external {
require(msg.value >= highestBid);
prevHighestBidder = highestBidder
prevHighestBid = highestBid
highestBidder = msg.sender;
highestBid = msg.value;
(bool success, ) = prevHighestBidder.call.value(prevHighestBid)("");
require(success, "Transfer failed.")
}
}
我知道最好创建一个单独的提款功能,但我希望能降低用户的 gas 成本。
如果接收方(prevHighestBidder
)是拒绝交易的合约怎么办?这将有效地破坏你的游戏,因为在他们成功调用 bid()
函数之后没有人。
他们的合同可能就这么简单
contract BlockBids {
function callBid() payable {
// call the bid() function with value
address(<yourContract>).call{value: msg.value}('0x1998aeef');
}
receive () external payable {
revert();
}
}
我的建议是不要检查prevHighestBidder
是否收到了ETH。或者添加一个回退机制,在接收者拒绝交易的情况下将 ETH 提取到您的地址,这样您就可以在他们证明对获胜地址的所有权后将其发送给他们(例如通过签署消息)。