Skip to content

Commit c03323e

Browse files
committed
Escrow Deposit and Withdraw && Timelock window for rejections
1 parent e370331 commit c03323e

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

contracts/market/JobStandard.sol

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pragma solidity ^0.4.18;
2+
3+
/**
4+
* @title The Job Interface contract
5+
*/
6+
7+
contract JobStandard {
8+
/**
9+
* @dev Starting hash of the job
10+
*/
11+
bytes32 public descriptor;
12+
13+
/**
14+
* @dev Result hash of the job
15+
*/
16+
bytes32 public result;
17+
18+
/**
19+
* @dev Start time timestamp
20+
*/
21+
uint256 public start;
22+
23+
/**
24+
* @dev End time timestamp
25+
*/
26+
uint256 public end;
27+
28+
/**
29+
* @dev An agent who pays the job.
30+
*/
31+
address public payer;
32+
33+
/**
34+
* @dev An agent to whom the job is paid.
35+
*/
36+
address public payee;
37+
38+
/**
39+
* @dev Broadcast new hash as job result.
40+
*/
41+
event Result(bytes32 result);
42+
}

test/TestEscrow.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const { duration, latestTime } = require('./helpers/latestTime.js');
2+
3+
const Escrow = artifacts.require('market/Escrow.sol')
4+
const Token = artifacts.require('tokens/SingularityNetToken.sol')
5+
6+
7+
const assertFail = require('./helpers/assertFail.js')
8+
const { increaseTimeTo } = require('./helpers/increaseTime')
9+
10+
const amount = new web3.BigNumber(800)
11+
12+
13+
contract('Escrow', function ([payee, payer, validator]) {
14+
15+
beforeEach(async () => {
16+
this.timelock = 1000 * 60 * 5 // 5 minutes
17+
this.token = await Token.new({ from: payer })
18+
this.escrow = await Escrow.new(token.address, payer, payee, this.timelock, validator, 0)
19+
})
20+
21+
it('DEPOSIT and start the Job', async () => {
22+
const jobDescriptor = "0x01"
23+
//Complete jobs
24+
25+
await this.token.approve(this.escrow.address, amount, { from: payer })
26+
const result = await this.escrow.deposit(amount, jobDescriptor, { from: payer })
27+
28+
29+
const found2 = result.logs.find(e => e.event === 'Deposited')
30+
assert.strictEqual(found2.event, 'Deposited', 'Deposited event not fired')
31+
32+
const found = result.logs.find(e => e.event === 'Started')
33+
assert.strictEqual(found.event, 'Started', 'Started event not fired')
34+
35+
})
36+
37+
it('JOB COMPLETED - set result', async () => {
38+
const jobDescriptor = "0x01"
39+
const jobResult = "0x202"
40+
await this.token.approve(this.escrow.address, amount, { from: payer })
41+
await this.escrow.deposit(amount, jobDescriptor, { from: payer })
42+
43+
//Complete jobs
44+
const result = await this.escrow.setResult(jobResult, { from: payee })
45+
46+
const found = result.logs.find(e => e.event === 'Result')
47+
assert.strictEqual(found.event, 'Result', 'Result event not fired')
48+
49+
const found2 = result.logs.find(e => e.event === 'Completed')
50+
assert.strictEqual(found2.event, 'Completed', 'Completed event not fired')
51+
52+
})
53+
54+
it('WITHDRAWAL - try before job completion', async () => {
55+
const jobDescriptor = "0x01"
56+
const jobResult = "0x202"
57+
await this.token.approve(this.escrow.address, amount, { from: payer })
58+
await this.escrow.deposit(amount, jobDescriptor, { from: payer })
59+
await assertFail(async () => await this.escrow.withdraw({ from: payee }), 'should have thrown before')
60+
})
61+
62+
it('WITHDRAWAL - try before timelock expiration time', async () => {
63+
const jobDescriptor = "0x01"
64+
const jobResult = "0x202"
65+
await this.token.approve(this.escrow.address, amount, { from: payer })
66+
await this.escrow.deposit(amount, jobDescriptor, { from: payer })
67+
await this.escrow.setResult(jobResult, { from: payee })
68+
await assertFail(async () => await this.escrow.withdraw({ from: payee }), 'should have thrown before')
69+
})
70+
71+
it('WITHDRAWAL - allowed after timelock', async () => {
72+
const jobDescriptor = "0x01"
73+
const jobResult = "0x202"
74+
await this.token.approve(this.escrow.address, amount, { from: payer })
75+
await this.escrow.deposit(amount, jobDescriptor, { from: payer })
76+
await this.escrow.setResult(jobResult, { from: payee })
77+
const time = (await this.escrow.end.call()) + duration.minutes(10)
78+
await increaseTimeTo(time)
79+
let result = await this.escrow.withdraw({ from: payee })
80+
const found = result.logs.find(e => e.event === 'Withdrew')
81+
assert.strictEqual(found.event, 'Withdrew', 'Withdrew event not fired')
82+
83+
})
84+
85+
})

0 commit comments

Comments
 (0)