Skip to content

Commit 281d878

Browse files
committed
fix tests to be compatible with solidity 5
1 parent b2323bc commit 281d878

File tree

5 files changed

+51
-41
lines changed

5 files changed

+51
-41
lines changed

test/helper/ASEANToken.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
pragma solidity ^0.4.23;
1+
pragma solidity ^0.5.0;
22

3-
import "openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
3+
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
44

55
/**
66
* A basic token for testing the HashedTimelockERC20.
77
*/
8-
contract ASEANToken is StandardToken {
8+
contract ASEANToken is ERC20 {
99
string public constant name = "ASEAN Token";
1010
string public constant symbol = "ASEAN";
1111
uint8 public constant decimals = 18;
12-
13-
constructor(uint _initialBalance) public {
14-
balances[msg.sender] = _initialBalance;
12+
13+
constructor(uint256 _initialBalance) public {
14+
_mint(msg.sender, _initialBalance);
1515
}
1616
}

test/helper/assert.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
if (!global.assert) global.assert = require('chai').assert
22

33
const assertEqualBN = (actual, expected, msg = 'numbers not equal') => {
4+
if (!web3.utils.isBN(actual))
5+
actual = web3.utils.toBN(actual)
6+
if (!web3.utils.isBN(expected))
7+
expected = web3.utils.toBN(expected)
48
assert.isTrue(
5-
actual.equals(expected),
9+
actual.eq(expected),
610
`
711
\tmsg: ${msg}
812
\tactual: ${actual.toString()}

test/helper/utils.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const newSecretHashPair = () => {
2626

2727
const nowSeconds = () => Math.floor(Date.now() / 1000)
2828

29-
const gasPrice = 100000000000 // truffle fixed gas price
30-
const txGas = txReceipt => txReceipt.receipt.gasUsed * gasPrice
29+
const defaultGasPrice = 100000000000 // truffle fixed gas price
30+
const txGas = (txReceipt, gasPrice = defaultGasPrice) => web3.utils.toBN(txReceipt.receipt.gasUsed * gasPrice)
3131
const txLoggedArgs = txReceipt => txReceipt.logs[0].args
3232
const txContractId = txReceipt => txLoggedArgs(txReceipt).contractId
3333

@@ -58,8 +58,11 @@ const htlcERC20ArrayToObj = c => {
5858
}
5959
}
6060

61+
const getBalance = async (address) => web3.utils.toBN(await web3.eth.getBalance(address))
62+
6163
export {
6264
bufToStr,
65+
getBalance,
6366
htlcArrayToObj,
6467
htlcERC20ArrayToObj,
6568
isSha256Hash,

test/htlc.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Promise from 'bluebird'
33
import {assertEqualBN} from './helper/assert'
44
import {
55
bufToStr,
6+
getBalance,
67
htlcArrayToObj,
78
isSha256Hash,
89
newSecretHashPair,
@@ -15,11 +16,11 @@ import {
1516

1617
const 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

2021
const hourSeconds = 3600
2122
const timeLock1Hour = nowSeconds() + hourSeconds
22-
const oneFinney = web3.toWei(1, 'finney')
23+
const oneFinney = web3.utils.toWei(web3.utils.toBN(1), 'finney')
2324

2425
contract('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

test/htlcERC20.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
const HashedTimelockERC20 = artifacts.require('./HashedTimelockERC20.sol')
1414
const ASEANToken = artifacts.require('./helper/ASEANToken.sol')
1515

16-
const REQUIRE_FAILED_MSG = 'VM Exception while processing transaction: revert'
16+
const REQUIRE_FAILED_MSG = 'Returned error: VM Exception while processing transaction: revert'
1717

1818
// some testing data
1919
const hourSeconds = 3600
@@ -119,12 +119,12 @@ contract('HashedTimelockERC20', accounts => {
119119
it('newContract() should reject a duplicate contract request', async () => {
120120
const hashlock = newSecretHashPair().hash
121121
const timelock = timeLock1Hour + 5
122-
const balBefore = await token.balanceOf(htlc.address)
122+
const balBefore = web3.utils.toBN(await token.balanceOf(htlc.address))
123123

124124
await newContract({hashlock: hashlock, timelock: timelock})
125125
await assertTokenBal(
126126
htlc.address,
127-
balBefore.plus(tokenAmount),
127+
balBefore.add(web3.utils.toBN(tokenAmount)),
128128
'tokens not transfered to htlc contract'
129129
)
130130

@@ -172,7 +172,7 @@ contract('HashedTimelockERC20', accounts => {
172172
await htlc.withdraw(contractId, wrongSecret, {from: receiver})
173173
assert.fail('expected failure due to 0 value transferred')
174174
} catch (err) {
175-
assert.equal(err.message, REQUIRE_FAILED_MSG)
175+
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
176176
}
177177
})
178178

@@ -188,14 +188,14 @@ contract('HashedTimelockERC20', accounts => {
188188
await htlc.withdraw(contractId, hashPair.secret, {from: someGuy})
189189
assert.fail('expected failure due to wrong receiver')
190190
} catch (err) {
191-
assert.equal(err.message, REQUIRE_FAILED_MSG)
191+
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
192192
}
193193
})
194194

195195
it('withdraw() should fail after timelock expiry', async () => {
196196
const hashPair = newSecretHashPair()
197-
const curBlkTime = web3.eth.getBlock('latest').timestamp
198-
const timelock2Seconds = curBlkTime + 2
197+
const curBlock = await web3.eth.getBlock('latest')
198+
const timelock2Seconds = curBlock.timestamp + 2
199199

200200
const newContractTx = await newContract({
201201
hashlock: hashPair.hash,
@@ -213,7 +213,7 @@ contract('HashedTimelockERC20', accounts => {
213213
new Error('expected failure due to withdraw after timelock expired')
214214
)
215215
} catch (err) {
216-
assert.equal(err.message, REQUIRE_FAILED_MSG)
216+
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
217217
resolve({message: 'success'})
218218
}
219219
}, 2000)
@@ -222,8 +222,8 @@ contract('HashedTimelockERC20', accounts => {
222222

223223
it('refund() should pass after timelock expiry', async () => {
224224
const hashPair = newSecretHashPair()
225-
const curBlkTime = web3.eth.getBlock('latest').timestamp
226-
const timelock2Seconds = curBlkTime + 2
225+
const curBlock = await web3.eth.getBlock('latest')
226+
const timelock2Seconds = curBlock.timestamp + 2
227227

228228
await token.approve(htlc.address, tokenAmount, {from: sender})
229229
const newContractTx = await newContract({
@@ -243,7 +243,7 @@ contract('HashedTimelockERC20', accounts => {
243243
// Check tokens returned to the sender
244244
await assertTokenBal(
245245
sender,
246-
balBefore.plus(tokenAmount),
246+
balBefore.add(web3.utils.toBN(tokenAmount)),
247247
`sender balance unexpected`
248248
)
249249

@@ -266,7 +266,7 @@ contract('HashedTimelockERC20', accounts => {
266266
await htlc.refund(contractId, {from: sender})
267267
assert.fail('expected failure due to timelock')
268268
} catch (err) {
269-
assert.equal(err.message, REQUIRE_FAILED_MSG)
269+
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
270270
}
271271
})
272272

@@ -321,7 +321,7 @@ contract('HashedTimelockERC20', accounts => {
321321
)
322322
assert.fail(shouldFailMsg)
323323
} catch (err) {
324-
assert.equal(err.message, REQUIRE_FAILED_MSG)
324+
assert.isTrue(err.message.startsWith(REQUIRE_FAILED_MSG))
325325
}
326326
}
327327
})

0 commit comments

Comments
 (0)