Skip to content

Commit ee2e7e2

Browse files
committed
Add missing test cases
1 parent 0c76753 commit ee2e7e2

File tree

4 files changed

+188
-3
lines changed

4 files changed

+188
-3
lines changed

test/wrapper/htlc-wrapper-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,12 @@ contract('HashedTimelockWrapper', accounts => {
139139
const address = await HtlcWrapper.deployContract(HashedTimelock, null, null)
140140
htlcWrapper.setAddress(address.address)
141141

142-
const timelock1Second = nowSeconds() + 1
142+
const timelock10Minutes = nowSeconds() + 600
143143

144144
await htlcWrapper.newContract(
145145
receiver,
146146
hashPair.hash,
147-
timelock1Second,
147+
timelock10Minutes,
148148
sender,
149149
oneFinney
150150
)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
const HtlcWrapperErc20 = require('../../wrapper/htlc-wrapper-erc20')
2+
3+
const {assertEqualBN} = require('../helper/assert')
4+
const {
5+
htlcERC20ArrayToObj,
6+
isSha256Hash,
7+
newSecretHashPair,
8+
nowSeconds,
9+
txLoggedArgs,
10+
} = require('../helper/utils')
11+
12+
const HashedTimelockERC20 = artifacts.require('./HashedTimelockERC20.sol')
13+
const AliceERC20 = artifacts.require('./helper/AliceERC20.sol')
14+
15+
// some testing data
16+
const hourSeconds = 3600
17+
const timeLock1Hour = nowSeconds() + hourSeconds
18+
const tokenAmount = 5
19+
20+
contract('HashedTimelockErc20Wrapper', accounts => {
21+
const sender = accounts[1]
22+
const receiver = accounts[2]
23+
const tokenSupply = 1000
24+
const senderInitialBalance = 100
25+
const provider = new web3.providers.HttpProvider("http://localhost:7545");
26+
27+
let htlcWrapper
28+
let token
29+
30+
const assertTokenBal = async (addr, tokenAmount, msg) =>
31+
assertEqualBN(
32+
await token.balanceOf.call(addr),
33+
tokenAmount,
34+
msg ? msg : 'wrong token balance'
35+
)
36+
37+
before(async () => {
38+
htlcWrapper = new HtlcWrapperErc20(HashedTimelockERC20, provider, null);
39+
let address = await HashedTimelockERC20.new()
40+
htlcWrapper.setAddress(address.address)
41+
token = await AliceERC20.new(tokenSupply)
42+
await token.transfer(sender, senderInitialBalance)
43+
await assertTokenBal(
44+
sender,
45+
senderInitialBalance,
46+
'balance not transferred in before()'
47+
)
48+
})
49+
50+
it('newContract() in wrapper should create new contract and store correct details', async () => {
51+
const hashPair = newSecretHashPair()
52+
await token.approve(htlcWrapper.address, tokenAmount, {from: sender})
53+
const newContractTx = await htlcWrapper.newContract(
54+
receiver,
55+
hashPair.hash,
56+
timeLock1Hour,
57+
token.address,
58+
tokenAmount,
59+
sender
60+
)
61+
62+
// check token balances
63+
assertTokenBal(sender, senderInitialBalance - tokenAmount)
64+
assertTokenBal(htlcWrapper.address, tokenAmount)
65+
66+
// check event logs
67+
const logArgs = txLoggedArgs(newContractTx)
68+
69+
const contractId = logArgs.contractId
70+
assert(isSha256Hash(contractId))
71+
72+
assert.equal(logArgs.sender, sender)
73+
assert.equal(logArgs.receiver, receiver)
74+
assert.equal(logArgs.tokenContract, token.address)
75+
assert.equal(logArgs.amount.toNumber(), tokenAmount)
76+
assert.equal(logArgs.hashlock, hashPair.hash)
77+
assert.equal(logArgs.timelock, timeLock1Hour)
78+
79+
// check htlc record
80+
const contractArr = await htlcWrapper.getContract(contractId)
81+
const contract = htlcERC20ArrayToObj(contractArr)
82+
assert.equal(contract.sender, sender)
83+
assert.equal(contract.receiver, receiver)
84+
assert.equal(contract.token, token.address)
85+
assert.equal(contract.amount.toNumber(), tokenAmount)
86+
assert.equal(contract.hashlock, hashPair.hash)
87+
assert.equal(contract.timelock.toNumber(), timeLock1Hour)
88+
assert.isFalse(contract.withdrawn)
89+
assert.isFalse(contract.refunded)
90+
assert.equal(
91+
contract.preimage,
92+
'0x0000000000000000000000000000000000000000000000000000000000000000'
93+
)
94+
})
95+
})
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
const HtlcWrapperErc721 = require('../../wrapper/htlc-wrapper-erc721')
2+
3+
const {assertEqualBN} = require('../helper/assert')
4+
const {
5+
htlcERC20ArrayToObj,
6+
isSha256Hash,
7+
newSecretHashPair,
8+
nowSeconds,
9+
txLoggedArgs,
10+
} = require('../helper/utils')
11+
12+
const HashedTimelockERC721 = artifacts.require('./HashedTimelockERC721.sol')
13+
const AliceERC721 = artifacts.require('./helper/AliceERC721.sol')
14+
15+
// some testing data
16+
const hourSeconds = 3600
17+
const timeLock1Hour = nowSeconds() + hourSeconds
18+
19+
contract('HashedTimelockErc721Wrapper', accounts => {
20+
const sender = accounts[1]
21+
const receiver = accounts[2]
22+
const tokenSupply = 1000
23+
const provider = new web3.providers.HttpProvider("http://localhost:7545");
24+
25+
let htlcWrapper
26+
let token
27+
28+
const assertTokenBal = async (token, addr, tokenAmount, msg) => {
29+
assertEqualBN(
30+
await token.balanceOf.call(addr),
31+
tokenAmount,
32+
msg ? msg : 'wrong token balance'
33+
)
34+
}
35+
36+
before(async () => {
37+
htlcWrapper = new HtlcWrapperErc721(HashedTimelockERC721, provider, null);
38+
let address = await HashedTimelockERC721.new()
39+
htlcWrapper.setAddress(address.address)
40+
token = await AliceERC721.new(tokenSupply)
41+
await token.mint(sender, 1)
42+
await assertTokenBal(
43+
token,
44+
sender,
45+
1,
46+
'balance not transferred to Alice in before()'
47+
)
48+
})
49+
50+
it('newContract() in wrapper should create new contract and store correct details', async () => {
51+
const hashPair = newSecretHashPair()
52+
await token.approve(htlcWrapper.address, 1, {from: sender})
53+
const newContractTx = await htlcWrapper.newContract(
54+
receiver,
55+
hashPair.hash,
56+
timeLock1Hour,
57+
token.address,
58+
1,
59+
sender
60+
)
61+
62+
// check event logs
63+
const logArgs = txLoggedArgs(newContractTx)
64+
65+
const contractId = logArgs.contractId
66+
assert(isSha256Hash(contractId))
67+
68+
assert.equal(logArgs.sender, sender)
69+
assert.equal(logArgs.receiver, receiver)
70+
assert.equal(logArgs.tokenContract, token.address)
71+
assert.equal(logArgs.hashlock, hashPair.hash)
72+
assert.equal(logArgs.timelock, timeLock1Hour)
73+
74+
// check htlc record
75+
const contractArr = await htlcWrapper.getContract(contractId)
76+
const contract = htlcERC20ArrayToObj(contractArr)
77+
assert.equal(contract.sender, sender)
78+
assert.equal(contract.receiver, receiver)
79+
assert.equal(contract.token, token.address)
80+
assert.equal(contract.amount.toNumber(), 1)
81+
assert.equal(contract.hashlock, hashPair.hash)
82+
assert.equal(contract.timelock.toNumber(), timeLock1Hour)
83+
assert.isFalse(contract.withdrawn)
84+
assert.isFalse(contract.refunded)
85+
assert.equal(
86+
contract.preimage,
87+
'0x0000000000000000000000000000000000000000000000000000000000000000'
88+
)
89+
})
90+
})

wrapper/htlc-wrapper-erc20.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class HtlcErc20Wrapper extends BaseWrapper {
1515
*/
1616
newContract(receiverAddress, hashlock, timelock, tokenContract, amount, sender) {
1717
return this.getContractInstance().then((instance) => {
18-
return instance.newContract(receiverAddress, hashlock, timelock, tokenContract, {
18+
return instance.newContract(receiverAddress, hashlock, timelock, tokenContract, amount, {
1919
from: sender
2020
})
2121
})

0 commit comments

Comments
 (0)