1
+ const HtlcWrapper = require ( '../../wrapper/htlc-wrapper' )
2
+
3
+ const { assertEqualBN} = require ( '../helper/assert' )
4
+ const {
5
+ getBalance,
6
+ htlcArrayToObj,
7
+ isSha256Hash,
8
+ newSecretHashPair,
9
+ nowSeconds,
10
+ txContractId,
11
+ txGas,
12
+ txLoggedArgs,
13
+ } = require ( '../helper/utils' )
14
+
15
+ const HashedTimelock = artifacts . require ( './HashedTimelock.sol' )
16
+
17
+ const hourSeconds = 3600
18
+ const timeLock1Hour = nowSeconds ( ) + hourSeconds
19
+ const oneFinney = web3 . utils . toWei ( web3 . utils . toBN ( 1 ) , 'finney' )
20
+
21
+ contract ( 'HashedTimelockWrapper' , accounts => {
22
+ const sender = accounts [ 1 ]
23
+ const receiver = accounts [ 2 ]
24
+ const provider = new web3 . providers . HttpProvider ( "http://localhost:7545" ) ;
25
+
26
+ it ( 'newContract() and getContract() in wrapper should create new contract and store correct details' , async ( ) => {
27
+ const hashPair = newSecretHashPair ( )
28
+ const htlcWrapper = new HtlcWrapper ( HashedTimelock , provider , null , false , null , null , null )
29
+ const txReceipt = await htlcWrapper . newContract (
30
+ receiver ,
31
+ hashPair . hash ,
32
+ timeLock1Hour ,
33
+ sender ,
34
+ oneFinney
35
+ )
36
+ const logArgs = txLoggedArgs ( txReceipt )
37
+
38
+ const contractId = logArgs . contractId
39
+ assert ( isSha256Hash ( contractId ) )
40
+
41
+ assert . equal ( logArgs . sender , sender )
42
+ assert . equal ( logArgs . receiver , receiver )
43
+ assertEqualBN ( logArgs . amount , oneFinney )
44
+ assert . equal ( logArgs . hashlock , hashPair . hash )
45
+ assert . equal ( logArgs . timelock , timeLock1Hour )
46
+
47
+ const contractArr = await htlcWrapper . getContract ( contractId )
48
+ const contract = htlcArrayToObj ( contractArr )
49
+ assert . equal ( contract . sender , sender )
50
+ assert . equal ( contract . receiver , receiver )
51
+ assertEqualBN ( contract . amount , oneFinney )
52
+ assert . equal ( contract . hashlock , hashPair . hash )
53
+ assert . equal ( contract . timelock . toNumber ( ) , timeLock1Hour )
54
+ assert . isFalse ( contract . withdrawn )
55
+ assert . isFalse ( contract . refunded )
56
+ assert . equal (
57
+ contract . preimage ,
58
+ '0x0000000000000000000000000000000000000000000000000000000000000000'
59
+ )
60
+ } )
61
+
62
+ it ( 'withdraw() contract in wrapper should withdraw amount correctly' , async ( ) => {
63
+ const hashPair = newSecretHashPair ( )
64
+ const htlcWrapper = new HtlcWrapper ( HashedTimelock , provider , null )
65
+ const newContractTx = await htlcWrapper . newContract (
66
+ receiver ,
67
+ hashPair . hash ,
68
+ timeLock1Hour ,
69
+ sender ,
70
+ oneFinney
71
+ )
72
+
73
+ const contractId = txContractId ( newContractTx )
74
+ const receiverBalBefore = await getBalance ( receiver )
75
+
76
+ // receiver calls withdraw with the secret to get the funds
77
+ const withdrawTx = await htlcWrapper . withdraw ( contractId , hashPair . secret , receiver )
78
+ const tx = await web3 . eth . getTransaction ( withdrawTx . tx )
79
+
80
+ // Check contract funds are now at the receiver address
81
+ const expectedBal = receiverBalBefore
82
+ . add ( oneFinney )
83
+ . sub ( txGas ( withdrawTx , tx . gasPrice ) )
84
+ assertEqualBN (
85
+ await getBalance ( receiver ) ,
86
+ expectedBal ,
87
+ "receiver balance doesn't match"
88
+ )
89
+ const contractArr = await htlcWrapper . getContract ( contractId )
90
+ const contract = htlcArrayToObj ( contractArr )
91
+ assert . isTrue ( contract . withdrawn ) // withdrawn set
92
+ assert . isFalse ( contract . refunded ) // refunded still false
93
+ assert . equal ( contract . preimage , hashPair . secret )
94
+ } )
95
+
96
+ it ( 'refund() in wrapper should pass after timelock expiry' , async ( ) => {
97
+ const hashPair = newSecretHashPair ( )
98
+ const htlcWrapper = new HtlcWrapper ( HashedTimelock , provider , null )
99
+ const timelock1Second = nowSeconds ( ) + 1
100
+
101
+ const newContractTx = await htlcWrapper . newContract (
102
+ receiver ,
103
+ hashPair . hash ,
104
+ timelock1Second ,
105
+ sender ,
106
+ oneFinney
107
+ )
108
+ const contractId = txContractId ( newContractTx )
109
+
110
+ // wait one second so we move past the timelock time
111
+ return new Promise ( ( resolve , reject ) =>
112
+ setTimeout ( async ( ) => {
113
+ try {
114
+ const balBefore = await getBalance ( sender )
115
+ const refundTx = await htlcWrapper . refund ( contractId , sender )
116
+ const tx = await web3 . eth . getTransaction ( refundTx . tx )
117
+ // Check contract funds are now at the senders address
118
+ const expectedBal = balBefore . add ( oneFinney ) . sub ( txGas ( refundTx , tx . gasPrice ) )
119
+ assertEqualBN (
120
+ await getBalance ( sender ) ,
121
+ expectedBal ,
122
+ "sender balance doesn't match"
123
+ )
124
+ const contract = await htlcWrapper . getContract ( contractId )
125
+ assert . isTrue ( contract [ 6 ] ) // refunded set
126
+ assert . isFalse ( contract [ 5 ] ) // withdrawn still false
127
+ resolve ( )
128
+ } catch ( err ) {
129
+ reject ( err )
130
+ }
131
+ } , 1000 )
132
+ )
133
+ } )
134
+
135
+ it ( 'retrieve contract from address in wrapper' , async ( ) => {
136
+ const hashPair = newSecretHashPair ( )
137
+ const htlcWrapper = new HtlcWrapper ( HashedTimelock , provider , null )
138
+
139
+ const address = await HtlcWrapper . deployContract ( HashedTimelock , null , null )
140
+ htlcWrapper . setAddress ( address . address )
141
+
142
+ const timelock1Second = nowSeconds ( ) + 1
143
+
144
+ await htlcWrapper . newContract (
145
+ receiver ,
146
+ hashPair . hash ,
147
+ timelock1Second ,
148
+ sender ,
149
+ oneFinney
150
+ )
151
+ } )
152
+ } )
0 commit comments