-
Notifications
You must be signed in to change notification settings - Fork 5
/
04.stateMachinePattern.sol
102 lines (93 loc) · 2.18 KB
/
04.stateMachinePattern.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
contract LoanContract {
enum LoanState {
NONE,
INITIATED,
COLLATERAL_RCVD,
FUNDED,
REPAYMENT,
FINISHED
}
address public borrower;
address public lender;
IERC20 token;
uint256 collateralAmount;
uint256 loanAmount;
LoanState public currentState;
modifier onlyBorrower() {
require(msg.sender == borrower);
_;
}
modifier atState(LoanState loanState) {
require(currentState == loanState);
_;
}
modifier transitionToState(LoanState nextState) {
_;
currentState = nextState;
}
constructor(
IERC20 _token,
uint256 _collateralAmount,
uint256 _loanAmount
) public transitionToState(LoanState.INITIATED) {
borrower = msg.sender;
token = _token;
collateralAmount = _collateralAmount;
loanAmount = _loanAmount;
}
function stateInitialized()
public
onlyBorrower
atState(LoanState.INITIATED)
transitionToState(LoanState.COLLATERAL_RCVD)
{
require(
IERC20(token).transferFrom(
borrower,
address(this),
collateralAmount
)
);
}
function putCollateral2()
public
onlyBorrower
atState(LoanState.COLLATERAL_RCVD)
transitionToState(LoanState.FUNDED)
{
require(
IERC20(token).transferFrom(
borrower,
address(this),
collateralAmount
)
);
}
function putCollateral2()
public
onlyBorrower
atState(LoanState.FUNDED)
transitionToState(LoanState.FINISHED)
{
require(
IERC20(token).transferFrom(
borrower,
address(this),
collateralAmount
)
);
}
function putCollateralFinished()
public
onlyBorrower
atState(LoanState.FINISHED)
{
require(
IERC20(token).transferFrom(
borrower,
address(this),
collateralAmount
)
);
}
}