-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSaferMoney.sol
33 lines (26 loc) · 1.19 KB
/
SaferMoney.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
pragma solidity 0.5.0;
contract sendYou{
function transfer(address payable _receiver, uint256 _value) payable external {
address(_receiver).transfer(_value);
}
function send(address payable _receiver, uint256 _value) payable external{
(bool success, ) = address(_receiver).call.value(_value)("");
require(success, "Transfer failed.");
}
//To avoid Re-entrancy in using call.value
function withdraw() external{
uint256 amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0; // typical safeguard for re-entrancy
(bool success, ) = msg.sender.call.value(amount)("");
require(success, "Transfer failed.");
}
/**
If msg.sender is a smart contract, it has an opportunity on line 19 to call withdraw() again before line 20 happens.
In that second call, balanceOf[msg.sender] is still the original amount, so it will be transferred again.
This can be repeated as many times as necessary to drain the smart contract.
NB: you can use https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ReentrancyGuard.sol
**/
function () payable external{
//this seems to be needed for v0.5.0
}
}