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