Skip to content

Commit daf3590

Browse files
authored
Merge pull request #9 from conr2d/compatible-with-solidity-5
Fix to compatible with solidity 5
2 parents e5370b0 + 281d878 commit daf3590

File tree

10 files changed

+67
-59
lines changed

10 files changed

+67
-59
lines changed

contracts/HashedTimelock.sol

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
pragma solidity ^0.4.24;
2-
pragma experimental "v0.5.0";
1+
pragma solidity ^0.5.0;
32

43
/**
54
* @title Hashed Timelock Contracts (HTLCs) on Ethereum ETH.
@@ -20,7 +19,7 @@ pragma experimental "v0.5.0";
2019
* back with this function.
2120
*/
2221
contract HashedTimelock {
23-
22+
2423
event LogHTLCNew(
2524
bytes32 indexed contractId,
2625
address indexed sender,
@@ -33,8 +32,8 @@ contract HashedTimelock {
3332
event LogHTLCRefund(bytes32 indexed contractId);
3433

3534
struct LockContract {
36-
address sender;
37-
address receiver;
35+
address payable sender;
36+
address payable receiver;
3837
uint amount;
3938
bytes32 hashlock; // sha-2 sha256 hash
4039
uint timelock; // UNIX timestamp seconds - locked UNTIL this time
@@ -92,7 +91,7 @@ contract HashedTimelock {
9291
* @return contractId Id of the new HTLC. This is needed for subsequent
9392
* calls.
9493
*/
95-
function newContract(address _receiver, bytes32 _hashlock, uint _timelock)
94+
function newContract(address payable _receiver, bytes32 _hashlock, uint _timelock)
9695
external
9796
payable
9897
fundsSent
@@ -199,7 +198,7 @@ contract HashedTimelock {
199198
)
200199
{
201200
if (haveContract(_contractId) == false)
202-
return;
201+
return (address(0), address(0), 0, 0, 0, false, false, 0);
203202
LockContract storage c = contracts[_contractId];
204203
return (c.sender, c.receiver, c.amount, c.hashlock, c.timelock,
205204
c.withdrawn, c.refunded, c.preimage);

contracts/HashedTimelockERC20.sol

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
pragma solidity ^0.4.24;
2-
pragma experimental "v0.5.0";
1+
pragma solidity ^0.5.0;
32

43
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
54

@@ -51,7 +50,7 @@ contract HashedTimelockERC20 {
5150
modifier tokensTransferable(address _token, address _sender, uint _amount) {
5251
require(_amount > 0, "token amount must be > 0");
5352
require(
54-
ERC20(_token).allowance(_sender, this) >= _amount,
53+
ERC20(_token).allowance(_sender, address(this)) >= _amount,
5554
"token allowance must be >= amount"
5655
);
5756
_;
@@ -136,9 +135,9 @@ contract HashedTimelockERC20 {
136135
revert();
137136

138137
// This contract becomes the temporary owner of the tokens
139-
if (!ERC20(_tokenContract).transferFrom(msg.sender, this, _amount))
138+
if (!ERC20(_tokenContract).transferFrom(msg.sender, address(this), _amount))
140139
revert();
141-
140+
142141
contracts[contractId] = LockContract(
143142
msg.sender,
144143
_receiver,
@@ -226,7 +225,7 @@ contract HashedTimelockERC20 {
226225
)
227226
{
228227
if (haveContract(_contractId) == false)
229-
return;
228+
return (address(0), address(0), address(0), 0, 0, 0, false, false, 0);
230229
LockContract storage c = contracts[_contractId];
231230
return (
232231
c.sender,

contracts/Migrations.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.23;
1+
pragma solidity ^0.5.0;
22

33
contract Migrations {
44
address public owner;

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"babel-polyfill": "^6.26.0",
3232
"babel-preset-env": "^1.7.0",
3333
"babel-register": "^6.26.0",
34-
"openzeppelin-solidity": "^1.9.0"
34+
"openzeppelin-solidity": "^2.1.2"
3535
},
3636
"devDependencies": {
3737
"bluebird": "^3.5.1"

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

0 commit comments

Comments
 (0)