-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.sol
38 lines (33 loc) · 980 Bytes
/
StateMachine.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
34
35
36
37
38
pragma solidity ^0.4.17;
contract DepositLock {
enum Stages {
AcceptingDeposits,
FreezingDeposits,
ReleasingDeposits
}
Stages public stage = Stages.AcceptingDeposits;
uint public creationTime = now;
mapping (address => uint) balances;
modifier atStage(Stages _stage) {
require(stage == _stage);
_;
}
modifier timedTransitions() {
if (stage == Stages.AcceptingDeposits && now >= creationTime + 1 days)
nextStage();
if (stage == Stages.FreezingDeposits && now >= creationTime + 8 days)
nextStage();
_;
}
function nextStage() internal {
stage = Stages(uint(stage) + 1);
}
function deposit() public payable timedTransitions atStage(Stages.AcceptingDeposits) {
balances[msg.sender] += msg.value;
}
function withdraw() public timedTransitions atStage(Stages.ReleasingDeposits) {
uint amount = balances[msg.sender];
balances[msg.sender] = 0;
msg.sender.transfer(amount);
}
}