-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEscrow.sol
363 lines (322 loc) · 11.9 KB
/
Escrow.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
// import "hardhat/console.sol";
import "./EscrowFactory.sol";
import "./Locker.sol";
contract Escrow is ReentrancyGuard {
enum MilestoneState {
Created,
Agreed,
Deposited,
Requested,
Released,
Disputed
}
struct Milestone {
address token;
address participant;
uint256 amount;
uint256 timestamp;
uint256 lockId;
string meta;
MilestoneState state;
}
/******************************* variables *******************************/
bool private isInitialized;
string private uri;
address private factory;
address private originator;
Milestone[] private milestones;
/******************************* events *******************************/
event MilestoneCreated(
uint256 mId,
address indexed token,
address indexed participant,
uint256 amount,
uint256 timestamp,
string meta
);
event MilestoneUpdated(
uint256 mId,
address indexed token,
address indexed participant,
uint256 amount,
uint256 timestamp,
string meta
);
event MilestoneStateUpdated(uint256 mId, MilestoneState state);
/******************************* modifiers *******************************/
/**
* @notice Check the milestone index
*/
modifier onlyIndex(uint256 mId) {
require(mId < milestones.length, "Escrow: invalid milestone index");
_;
}
/**
* @notice Only originator can call
*/
modifier onlyOriginator() {
require(originator == msg.sender, "Escrow: caller is not the originator");
_;
}
/**
* @notice Only participant can call
*/
modifier onlyParticipant(uint256 mId) {
require(milestones[mId].participant == msg.sender, "Escrow: caller is not the participant");
_;
}
/**
* @notice Check the milestone info to set
*/
modifier onlyMilestoneInfo(
address token,
address participant,
uint256 amount,
uint256 timestamp,
string memory meta
) {
require(token != address(0), "Escrow: token address is not defined");
require(participant != address(0), "Escrow: participant address is not defined");
require(amount > 0, "Escrow: token amount is zero");
require(block.timestamp < timestamp, "Escrow: due date has already passed");
require(bytes(meta).length > 0, "Escrow: meta is empty");
_;
}
/**
* @notice Call only after the dipuste creation
*/
modifier onlyDispute(uint256 mId) {
require(milestones[mId].state == MilestoneState.Disputed, "Escrow: there is no dispute");
_;
}
/******************************* functions *******************************/
/**
* @notice Initialize the contract
* @param _originator: Originator address
* @param _uri: Ecsrow uri
*/
function initialize(address _originator, string memory _uri) external {
require(!isInitialized, "Escrow: already initialized");
require(_originator != address(0), "Escrow: originator address is not defined");
require(bytes(_uri).length > 0, "Escrow: uri is empty");
isInitialized = true;
originator = _originator;
uri = _uri;
factory = msg.sender;
}
/**
* @notice Udpate metadata of an escrow
* @param _uri: escrow uri
*/
function updateMeta(string memory _uri) external onlyOriginator {
require(bytes(_uri).length > 0, "Escrow: uri is empty");
uri = _uri;
}
/**
* @notice Originator creates a new milestone.
* @param token: token address to fund
* @param participant: participant address
* @param amount: token amount to fund
* @param timestamp: timestamp of dueDate
* @param meta: milestone description
*/
function createMilestone(
address token,
address participant,
uint256 amount,
uint256 timestamp,
string memory meta
) external onlyOriginator onlyMilestoneInfo(token, participant, amount, timestamp, meta) nonReentrant {
milestones.push(
Milestone({
token: token,
participant: participant,
amount: amount,
timestamp: timestamp,
meta: meta,
lockId: type(uint256).max,
state: MilestoneState.Created
})
);
emit MilestoneCreated(milestones.length - 1, token, participant, amount, timestamp, meta);
}
/**
* @notice Originator updates a certain milestone.
* @param mId: milestone index
* @param token: token address
* @param participant: participant address
* @param amount: token amount
* @param timestamp: timestamp of dueDate
* @param meta: milestone description
*/
function updateMilestone(
uint256 mId,
address token,
address participant,
uint256 amount,
uint256 timestamp,
string memory meta
) external onlyIndex(mId) onlyOriginator onlyMilestoneInfo(token, participant, amount, timestamp, meta) {
Milestone storage m = milestones[mId];
require(m.state == MilestoneState.Created, "Escrow: milestone has already been agreed");
m.token = token;
m.participant = participant;
m.amount = amount;
m.timestamp = timestamp;
m.meta = meta;
emit MilestoneUpdated(mId, token, participant, amount, timestamp, meta);
}
/**
* @notice Originator accepts a certain milestone
* @param mId: milestone index
*/
function agreeMilestone(uint256 mId) external onlyIndex(mId) onlyParticipant(mId) {
Milestone storage m = milestones[mId];
require(m.state == MilestoneState.Created, "Escrow: milestone has already been agreed");
milestones[mId].state = MilestoneState.Agreed;
emit MilestoneStateUpdated(mId, milestones[mId].state);
}
/**
* @notice Originator deposits fund of milstone to this contract
* @param mId: milestone index
*/
function depositMilestone(uint256 mId) external onlyIndex(mId) onlyOriginator nonReentrant {
Milestone storage m = milestones[mId];
require(m.state == MilestoneState.Agreed, "Escrow: milestone is not agreed");
m.state = MilestoneState.Deposited;
IERC20(m.token).transferFrom(msg.sender, address(this), m.amount);
emit MilestoneStateUpdated(mId, m.state);
}
/**
* @notice Participant call this when the milestone isn't to released and it's over dueDate
* @param mId: milestone index
*/
function requestMilestone(uint256 mId) external onlyIndex(mId) onlyParticipant(mId) {
Milestone storage m = milestones[mId];
require(
m.state == MilestoneState.Agreed || m.state == MilestoneState.Deposited,
"Escrow: milestone is not deposited"
);
require(m.timestamp <= block.timestamp, "Escrow: milestone is not yet due date");
m.state = MilestoneState.Requested;
emit MilestoneStateUpdated(mId, m.state);
}
/**
* @dev Fund is transferred to Lock contract.
* @param mId: milestone index
*/
function releaseMilestone(uint256 mId) external onlyIndex(mId) onlyOriginator nonReentrant {
Milestone storage m = milestones[mId];
require(
m.state == MilestoneState.Deposited || m.state == MilestoneState.Requested,
"Escrow: milestone isn't deposited"
);
m.state = MilestoneState.Released;
(address feeRecipient, , uint256 feePercent, uint256 feeMultiplier) = EscrowFactory(factory).getFeeInfo();
uint256 feeAmount = (m.amount * feePercent) / feeMultiplier;
uint256 lockAmount = m.amount - feeAmount;
(address locker, ) = EscrowFactory(factory).getLockInfo();
m.lockId = Locker(locker).create(m.participant, m.token, lockAmount, block.timestamp, mId);
IERC20(m.token).transfer(feeRecipient, feeAmount);
IERC20(m.token).transfer(locker, lockAmount);
emit MilestoneStateUpdated(mId, m.state);
}
/**
* @notice Originator can file a dispute for released milestones.
* @param mId: milestone index
*/
function createDispute(uint256 mId) external onlyIndex(mId) onlyOriginator {
Milestone storage m = milestones[mId];
(, uint256 lockDuration) = EscrowFactory(factory).getLockInfo();
require(m.state == MilestoneState.Released, "Escrow: milestone is not released");
require(block.timestamp < m.timestamp + lockDuration, "Escrow: lock duration has already passed");
m.state = MilestoneState.Disputed;
emit MilestoneStateUpdated(mId, m.state);
}
/**
* @notice Only called by participant or operator
* @param mId: milestone index
*/
function resolveDispute(uint256 mId) external onlyIndex(mId) onlyDispute(mId) nonReentrant {
Milestone storage m = milestones[mId];
require(
msg.sender == m.participant || EscrowFactory(factory).isOperator(msg.sender),
"Escrow: caller has no role"
);
m.state = MilestoneState.Agreed;
(address locker, ) = EscrowFactory(factory).getLockInfo();
Locker(locker).withdraw(m.lockId);
emit MilestoneStateUpdated(mId, m.state);
}
/**
* @notice Only called by originator or operator
* @param mId: milestone index
*/
function cancelDispute(uint256 mId) external onlyIndex(mId) onlyDispute(mId) {
require(
msg.sender == originator || EscrowFactory(factory).isOperator(msg.sender),
"Escrow: caller has no role"
);
Milestone storage m = milestones[mId];
m.state = MilestoneState.Released;
emit MilestoneStateUpdated(mId, m.state);
}
/**
* @notice Pariticipant claim the fund in Lock contract
* @param mId: milestone index
*/
function claimPariticipant(uint256 mId) external onlyIndex(mId) onlyParticipant(mId) nonReentrant {
Milestone storage m = milestones[mId];
require(m.state == MilestoneState.Released, "Escrow: milestone is not yet released");
(address locker, ) = EscrowFactory(factory).getLockInfo();
Locker(locker).release(m.lockId);
}
/**
* @notice Destory self
*/
function destroy(uint256 eId, uint256 ownEId) external onlyOriginator nonReentrant {
uint256 i = 0;
for (i = 0; i < milestones.length; i++) {
if (milestones[i].state != MilestoneState.Released) break;
}
require(i == milestones.length, "Escrow: there are unreleased milestones");
EscrowFactory(factory).destroyEscrow(eId, ownEId, msg.sender);
selfdestruct(payable(factory));
}
/**
* @notice Get the meta
*/
function getMeta() external view returns (string memory) {
return uri;
}
/**
* @notice Get the originator address
*/
function getOriginator() external view returns (address) {
return originator;
}
/**
* @notice Get the lock duration
*/
function getLockDuration() external view returns (uint256 lockDuration) {
(, lockDuration) = EscrowFactory(factory).getLockInfo();
}
/**
* @notice Get count of any milstones(Created, Deposited, Released, Disputed)
*/
function getCountMilestones(MilestoneState state) external view returns (uint256 count) {
for (uint256 i = 0; i < milestones.length; i++) {
if (milestones[i].state == state) count++;
}
}
/**
* @notice Get the milestone
*/
function getMilestone(uint256 mId) external view returns (Milestone memory) {
return milestones[mId];
}
}