Skip to content

Commit cab4ff6

Browse files
committed
Update Tests and JobValidator logic
1 parent c03323e commit cab4ff6

File tree

7 files changed

+73
-258
lines changed

7 files changed

+73
-258
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ or
2828

2929
```bash
3030
truffle test
31-
```
31+
```
32+
33+
## Flattening
34+
35+
`solidity_flattener contracts/Contract.sol --solc-paths=zeppelin-solidity=$(pwd)/node_modules/zeppelin-solidity/ --output contract.sol`
36+

contracts/market/Escrow.sol

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ pragma solidity ^0.4.18;
33
import "./Job.sol";
44
import "zeppelin-solidity/contracts/token/ERC20.sol";
55

6+
67
contract Escrow is Job {
78
/**
8-
* @dev Kovan AGI token.
9+
* @dev Kovan token.
910
* https://kovan.etherscan.io/token/0x3b226ff6aad7851d3263e53cb7688d13a07f6e81#readContract
1011
*/
11-
ERC20 public constant AGI = ERC20(0x3b226ff6aad7851d3263e53cb7688d13a07f6e81);
12-
12+
ERC20 public token;
13+
1314
/**
14-
* @dev Vault mapping
15+
* @dev Fund Timelock in milliseconds
1516
*/
16-
mapping(address => mapping(address => uint256)) public vault;
17+
uint256 public timelock;
1718

1819
/**
1920
* @dev Deposited event
@@ -25,44 +26,73 @@ contract Escrow is Job {
2526
*/
2627
event Withdrew(address payee, uint256 amount);
2728

28-
/**
29+
/**
2930
* @dev Escrow constructor.
3031
* @param _payer An agent who pays the job.
3132
* @param _payee An agent to whom the job is paid.
32-
* @param _cost Cost in AGI
33+
* @param _timelock how many seconds after end should be the window for rejections
34+
* @param _validator who should check if rejected
3335
* @param _reward reward for validator
3436
*/
3537
function Escrow(
38+
address _token,
3639
address _payer,
37-
address _payee,
38-
bytes32 _descriptor,
39-
uint256 _cost,
40+
address _payee,
41+
uint256 _timelock,
42+
address _validator,
4043
uint256 _reward ) public
4144
{
45+
token = ERC20(_token);
4246
payer = _payer;
4347
payee = _payee;
44-
descriptor = _descriptor;
45-
cost = _cost;
48+
49+
timelock = _timelock;
50+
//Reward for the validator in case of dispute
51+
validator = _validator;
4652
reward = _reward;
4753

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
4856
provider = payee;
49-
start = now;
5057

51-
Started(payer, payee, descriptor, address(this));
5258
}
5359

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;
5473

55-
function deposit(uint256 amount) public {
56-
require(AGI.transferFrom(msg.sender, this, amount));
57-
58-
Deposited(msg.sender,amount);
74+
Started(payer, payee, descriptor, address(this));
5975
}
6076

77+
/**
78+
* @dev withdraw all funds from escrow
79+
*/
6180
function withdraw() public {
6281
require(msg.sender == payee);
63-
require(status == 1);
64-
uint256 balance = AGI.balanceOf(this);
65-
require(AGI.transfer(msg.sender, balance));
82+
require(timelockExpired());
83+
require(!isRejected);
84+
85+
uint256 balance = token.balanceOf(this);
86+
require (balance > 0);
87+
88+
require(token.transfer(msg.sender, balance));
6689
Withdrew(msg.sender, balance);
6790
}
91+
92+
function timelockExpired() internal constant returns(bool) {
93+
bool isExpired = (end + timelock) <= block.timestamp;
94+
return isCompleted && isExpired ;
95+
}
96+
97+
6898
}

contracts/market/Job.sol

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ import "./JobValidator.sol";
66
*/
77
contract Job is JobValidator {
88

9+
/**
10+
* @dev Determines if job is completed
11+
*/
12+
bool public isCompleted = false;
13+
914
/**
1015
* @dev Who can set the result
1116
*/
12-
address provider;
17+
address public provider;
18+
1319
/**
14-
* @dev Cost in AGI for the exection of the job
20+
* @dev Price in AGI for the exection of the job
1521
* (10**8 cogs === 1 AGI)
1622
*/
17-
uint256 public cost;
23+
uint256 public price;
1824

1925
/**
2026
* @dev Started event
@@ -35,6 +41,7 @@ contract Job is JobValidator {
3541
result = _result;
3642

3743
Completed();
44+
isCompleted = true;
3845

3946
return true;
4047
}

contracts/market/JobInterface.sol

Lines changed: 0 additions & 42 deletions
This file was deleted.

contracts/market/JobValidator.sol

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
pragma solidity ^0.4.18;
22

3-
import "./JobInterface.sol";
3+
import "./JobStandard.sol";
44

55
/**
66
* @title The Job Validator contract
77
*/
88

9-
contract JobValidator is JobInterface {
9+
contract JobValidator is JobStandard {
1010

1111
/**
1212
* @dev Reward for validator in AGI.
1313
*/
1414
address public validator;
1515

1616
/**
17-
* @dev Validation status
18-
* 0 == pending && 1 == accepted && 2 == rejected
17+
* @dev rejection status
1918
*/
20-
address public status = 0;
19+
bool public isRejected = false;
2120

2221
/**
2322
* @dev Reward for validator in AGI.
@@ -45,18 +44,15 @@ contract JobValidator is JobInterface {
4544
*/
4645
function accept() public {
4746
validator = msg.sender;
48-
status = 1;
49-
5047
Accepted();
5148
}
5249

5350
/**
5451
* @dev Reject job result
5552
*/
5653
function reject() public {
57-
validator = msg.sender;
58-
status = 2;
59-
54+
require(msg.sender==validator);
55+
isRejected = true;
6056
Rejected();
6157
}
6258

0 commit comments

Comments
 (0)