Skip to content

Commit 10878c1

Browse files
committed
修改课程2代码,更加安全
1 parent ca27490 commit 10878c1

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Diff for: w2_1_code/contracts/Bank.sol

+11-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@ contract Bank {
3131

3232

3333
// 提取金额
34-
function withdraw() public onlyOwner {
34+
function withdraw() onlyOwner public returns (bool) {
3535
require(address(this).balance > 0);
36-
payable(msg.sender).transfer(address(this).balance);
36+
// payable(msg.sender).transfer(address(this).balance);
37+
// safe transfer,忽略2300gas限制
38+
(bool success,) = msg.sender.call{value : address(this).balance}("");
3739
// 清除所有余额
38-
for (uint256 i = 0; i < included.length; i++) {
39-
isIncluded[included[i]] = false;
40-
balanceOf[included[i]] = 0;
40+
if(success){
41+
for (uint256 i = 0; i < included.length; i++) {
42+
isIncluded[included[i]] = false;
43+
balanceOf[included[i]] = 0;
44+
}
45+
delete included;
4146
}
42-
delete included;
47+
return success;
4348
}
4449
}

0 commit comments

Comments
 (0)