1
- pragma solidity ^ 0.4.11 ;
1
+ pragma solidity ^ 0.4.18 ;
2
2
3
- import "zeppelin-solidity/contracts/ownership/Ownable.sol " ;
3
+ import "./Job.sol " ;
4
+ import "zeppelin-solidity/contracts/token/ERC20.sol " ;
4
5
5
- contract Escrow is Ownable {
6
6
7
+ contract Escrow is Job {
8
+ /**
9
+ * @dev Kovan token.
10
+ * https://kovan.etherscan.io/token/0x3b226ff6aad7851d3263e53cb7688d13a07f6e81#readContract
11
+ */
12
+ ERC20 public token;
7
13
8
- address public beneficiary;
14
+ /**
15
+ * @dev Fund Timelock in milliseconds
16
+ */
17
+ uint256 public timelock;
18
+
19
+ /**
20
+ * @dev Deposited event
21
+ */
22
+ event Deposited (address payer , uint256 amount );
9
23
10
- event Deposited (address indexed from , uint amount );
24
+ /**
25
+ * @dev Deposited event
26
+ */
27
+ event Withdrew (address payee , uint256 amount );
11
28
12
- function Escrow (address _beneficiary ) {
13
- beneficiary = _beneficiary;
29
+ /**
30
+ * @dev Escrow constructor.
31
+ * @param _payer An agent who pays the job.
32
+ * @param _payee An agent to whom the job is paid.
33
+ * @param _timelock how many seconds after end should be the window for rejections
34
+ * @param _validator who should check if rejected
35
+ * @param _reward reward for validator
36
+ */
37
+ function Escrow (
38
+ address _token ,
39
+ address _payer ,
40
+ address _payee ,
41
+ uint256 _timelock ,
42
+ address _validator ,
43
+ uint256 _reward ) public
44
+ {
45
+ token = ERC20 (_token);
46
+ payer = _payer;
47
+ payee = _payee;
48
+
49
+ timelock = _timelock;
50
+ //Reward for the validator in case of dispute
51
+ validator = _validator;
52
+ reward = _reward;
53
+
54
+ //Who actually should deliver the job, capable of setResult and close the job
55
+ //Beaware that who can withdraw money is still the payee, not the provider
56
+ provider = payee;
57
+
58
+ }
59
+
60
+ /**
61
+ * @dev Deposit function.
62
+ * @param _amount Token amount to fill the price
63
+ * @param _descriptor bytes hash that represent the input for the job
64
+ */
65
+ function deposit (uint256 _amount , bytes32 _descriptor ) public {
66
+ require (msg .sender == payer);
67
+ require (token.transferFrom (msg .sender , this , _amount));
68
+ Deposited (msg .sender , _amount);
69
+
70
+ descriptor = _descriptor;
71
+ price = _amount;
72
+ start = now ;
73
+
74
+ Started (payer, payee, descriptor, address (this ));
14
75
}
15
76
16
- function () payable {
17
- if (msg .value <= 0 ) {
18
- return ;
19
- }
77
+ /**
78
+ * @dev withdraw all funds from escrow
79
+ */
80
+ function withdraw () public {
81
+ require (msg .sender == payee);
82
+ require (timelockExpired ());
83
+ require (! isRejected);
20
84
21
- Deposited (msg .sender , msg .value );
85
+ uint256 balance = token.balanceOf (this );
86
+ require (balance > 0 );
87
+
88
+ require (token.transfer (msg .sender , balance));
89
+ Withdrew (msg .sender , balance);
22
90
}
23
91
24
- function releaseFunds () onlyOwner {
25
- beneficiary.transfer (this .balance);
92
+ function timelockExpired () internal constant returns (bool ) {
93
+ bool isExpired = (end + timelock) <= block .timestamp ;
94
+ return isCompleted && isExpired ;
26
95
}
27
- }
96
+
97
+
98
+ }
0 commit comments