@@ -3,6 +3,7 @@ import Promise from 'bluebird'
33import { assertEqualBN } from './helper/assert'
44import {
55 bufToStr ,
6+ getBalance ,
67 htlcArrayToObj ,
78 isSha256Hash ,
89 newSecretHashPair ,
@@ -15,11 +16,11 @@ import {
1516
1617const HashedTimelock = artifacts . require ( './HashedTimelock.sol' )
1718
18- const REQUIRE_FAILED_MSG = 'VM Exception while processing transaction: revert'
19+ const REQUIRE_FAILED_MSG = 'Returned error: VM Exception while processing transaction: revert'
1920
2021const hourSeconds = 3600
2122const timeLock1Hour = nowSeconds ( ) + hourSeconds
22- const oneFinney = web3 . toWei ( 1 , 'finney' )
23+ const oneFinney = web3 . utils . toWei ( web3 . utils . toBN ( 1 ) , 'finney' )
2324
2425contract ( 'HashedTimelock' , accounts => {
2526 const sender = accounts [ 1 ]
@@ -44,15 +45,15 @@ contract('HashedTimelock', accounts => {
4445
4546 assert . equal ( logArgs . sender , sender )
4647 assert . equal ( logArgs . receiver , receiver )
47- assert . equal ( logArgs . amount , oneFinney )
48+ assertEqualBN ( logArgs . amount , oneFinney )
4849 assert . equal ( logArgs . hashlock , hashPair . hash )
4950 assert . equal ( logArgs . timelock , timeLock1Hour )
5051
5152 const contractArr = await htlc . getContract . call ( contractId )
5253 const contract = htlcArrayToObj ( contractArr )
5354 assert . equal ( contract . sender , sender )
5455 assert . equal ( contract . receiver , receiver )
55- assert . equal ( contract . amount , oneFinney )
56+ assertEqualBN ( contract . amount , oneFinney )
5657 assert . equal ( contract . hashlock , hashPair . hash )
5758 assert . equal ( contract . timelock . toNumber ( ) , timeLock1Hour )
5859 assert . isFalse ( contract . withdrawn )
@@ -73,7 +74,7 @@ contract('HashedTimelock', accounts => {
7374 } )
7475 assert . fail ( 'expected failure due to 0 value transferred' )
7576 } catch ( err ) {
76- assert . equal ( err . message , REQUIRE_FAILED_MSG )
77+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
7778 }
7879 } )
7980
@@ -89,7 +90,7 @@ contract('HashedTimelock', accounts => {
8990
9091 assert . fail ( 'expected failure due past timelock' )
9192 } catch ( err ) {
92- assert . equal ( err . message , REQUIRE_FAILED_MSG )
93+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
9394 }
9495 } )
9596
@@ -109,7 +110,7 @@ contract('HashedTimelock', accounts => {
109110 } )
110111 assert . fail ( 'expected failure due to duplicate request' )
111112 } catch ( err ) {
112- assert . equal ( err . message , REQUIRE_FAILED_MSG )
113+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
113114 }
114115 } )
115116
@@ -127,19 +128,20 @@ contract('HashedTimelock', accounts => {
127128 )
128129
129130 const contractId = txContractId ( newContractTx )
130- const receiverBalBefore = web3 . eth . getBalance ( receiver )
131+ const receiverBalBefore = await getBalance ( receiver )
131132
132133 // receiver calls withdraw with the secret to get the funds
133134 const withdrawTx = await htlc . withdraw ( contractId , hashPair . secret , {
134135 from : receiver ,
135136 } )
137+ const tx = await web3 . eth . getTransaction ( withdrawTx . tx )
136138
137139 // Check contract funds are now at the receiver address
138140 const expectedBal = receiverBalBefore
139- . plus ( oneFinney )
140- . minus ( txGas ( withdrawTx ) )
141+ . add ( oneFinney )
142+ . sub ( txGas ( withdrawTx , tx . gasPrice ) )
141143 assertEqualBN (
142- web3 . eth . getBalance ( receiver ) ,
144+ await getBalance ( receiver ) ,
143145 expectedBal ,
144146 "receiver balance doesn't match"
145147 )
@@ -170,7 +172,7 @@ contract('HashedTimelock', accounts => {
170172 await htlc . withdraw ( contractId , wrongSecret , { from : receiver } )
171173 assert . fail ( 'expected failure due to 0 value transferred' )
172174 } catch ( err ) {
173- assert . equal ( err . message , REQUIRE_FAILED_MSG )
175+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
174176 }
175177 } )
176178
@@ -192,7 +194,7 @@ contract('HashedTimelock', accounts => {
192194 await htlc . withdraw ( contractId , hashPair . secret , { from : someGuy } )
193195 assert . fail ( 'expected failure due to wrong receiver' )
194196 } catch ( err ) {
195- assert . equal ( err . message , REQUIRE_FAILED_MSG )
197+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
196198 }
197199 } )
198200
@@ -222,7 +224,7 @@ contract('HashedTimelock', accounts => {
222224 new Error ( 'expected failure due to withdraw after timelock expired' )
223225 )
224226 } catch ( err ) {
225- assert . equal ( err . message , REQUIRE_FAILED_MSG )
227+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
226228 resolve ( )
227229 }
228230 } , 1000 )
@@ -249,12 +251,13 @@ contract('HashedTimelock', accounts => {
249251 return new Promise ( ( resolve , reject ) =>
250252 setTimeout ( async ( ) => {
251253 try {
252- const balBefore = web3 . eth . getBalance ( sender )
253- const tx = await htlc . refund ( contractId , { from : sender } )
254+ const balBefore = await getBalance ( sender )
255+ const refundTx = await htlc . refund ( contractId , { from : sender } )
256+ const tx = await web3 . eth . getTransaction ( refundTx . tx )
254257 // Check contract funds are now at the senders address
255- const expectedBal = balBefore . plus ( oneFinney ) . minus ( txGas ( tx ) )
258+ const expectedBal = balBefore . add ( oneFinney ) . sub ( txGas ( refundTx , tx . gasPrice ) )
256259 assertEqualBN (
257- web3 . eth . getBalance ( sender ) ,
260+ await getBalance ( sender ) ,
258261 expectedBal ,
259262 "sender balance doesn't match"
260263 )
@@ -286,7 +289,7 @@ contract('HashedTimelock', accounts => {
286289 await htlc . refund ( contractId , { from : sender } )
287290 assert . fail ( 'expected failure due to timelock' )
288291 } catch ( err ) {
289- assert . equal ( err . message , REQUIRE_FAILED_MSG )
292+ assert . isTrue ( err . message . startsWith ( REQUIRE_FAILED_MSG ) )
290293 }
291294 } )
292295
0 commit comments