Skip to content

Commit c950f39

Browse files
authored
Merge pull request #137 from liquality/swap-test-refactor
Swap test refactor
2 parents c048f52 + 536c1db commit c950f39

File tree

4 files changed

+47
-45
lines changed

4 files changed

+47
-45
lines changed

test/integration/swap/chainToChain.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
88

99
chai.use(chaiAsPromised)
1010

11-
async function testSwap (chain1Id, chain1, chain2Id, chain2) {
11+
async function testSwap (chain1, chain2) {
1212
console.log('\x1b[33m', `Generating secret: Watch for prompt`, '\x1b[0m')
13-
const secret = await chain1.generateSecret('test')
13+
const secret = await chain1.client.generateSecret('test')
1414
const secretHash = crypto.sha256(secret)
1515

16-
const chain1SwapParams = await getSwapParams(chain1Id, chain1)
17-
const chain2SwapParams = await getSwapParams(chain2Id, chain2)
16+
const chain1SwapParams = await getSwapParams(chain1)
17+
const chain2SwapParams = await getSwapParams(chain2)
1818

19-
const chain1InitiationTxId = await initiateAndVerify(chain1, chain1Id, secretHash, chain1SwapParams)
20-
const chain2InitiationTxId = await initiateAndVerify(chain2, chain2Id, secretHash, chain2SwapParams)
21-
const revealedSecret = await claimAndVerify(chain1, chain1Id, chain1InitiationTxId, secret, chain1SwapParams)
19+
const chain1InitiationTxId = await initiateAndVerify(chain1, secretHash, chain1SwapParams)
20+
const chain2InitiationTxId = await initiateAndVerify(chain2, secretHash, chain2SwapParams)
21+
const revealedSecret = await claimAndVerify(chain1, chain1InitiationTxId, secret, chain1SwapParams)
2222
expect(revealedSecret).to.equal(secret)
23-
await claimAndVerify(chain2, chain2Id, chain2InitiationTxId, revealedSecret, chain2SwapParams)
23+
await claimAndVerify(chain2, chain2InitiationTxId, revealedSecret, chain2SwapParams)
2424
}
2525

2626
describe('Swap Chain to Chain', function () {
@@ -33,7 +33,7 @@ describe('Swap Chain to Chain', function () {
3333
console.log('\x1b[36m', 'Starting MetaMask connector on http://localhost:3333 - Open in browser to continue', '\x1b[0m')
3434
await metaMaskConnector.start()
3535
interval = setInterval(() => {
36-
chains.bitcoinWithNode.generateBlock(1)
36+
chains.bitcoinWithNode.client.generateBlock(1)
3737
}, 1000)
3838
})
3939

@@ -43,11 +43,11 @@ describe('Swap Chain to Chain', function () {
4343
})
4444

4545
it('BTC (Ledger) - ETH (MetaMask)', async () => {
46-
await testSwap('Bitcoin Ledger', chains.bitcoinWithLedger, 'Ethereum MetaMask', chains.ethereumWithMetaMask)
46+
await testSwap(chains.bitcoinWithLedger, chains.ethereumWithMetaMask)
4747
})
4848

4949
it('ETH (MetaMask) - BTC (Ledger)', async () => {
50-
await testSwap('Ethereum MetaMask', chains.ethereumWithMetaMask, 'Bitcoin Ledger', chains.bitcoinWithLedger)
50+
await testSwap(chains.ethereumWithMetaMask, chains.bitcoinWithLedger)
5151
})
5252
})
5353

@@ -56,7 +56,7 @@ describe('Swap Chain to Chain', function () {
5656

5757
before(async () => {
5858
interval = setInterval(() => {
59-
chains.bitcoinWithNode.generateBlock(1)
59+
chains.bitcoinWithNode.client.generateBlock(1)
6060
}, 1000)
6161
})
6262

@@ -65,11 +65,11 @@ describe('Swap Chain to Chain', function () {
6565
})
6666

6767
it('BTC (Node) - ETH (Node)', async () => {
68-
await testSwap('Bitcoin Node', chains.bitcoinWithNode, 'Ethereum Node', chains.ethereumWithNode)
68+
await testSwap(chains.bitcoinWithNode, chains.ethereumWithNode)
6969
})
7070

7171
it('ETH (Node) - BTC (Node)', async () => {
72-
await testSwap('Ethereum Node', chains.ethereumWithNode, 'Bitcoin Node', chains.bitcoinWithNode)
72+
await testSwap(chains.ethereumWithNode, chains.bitcoinWithNode)
7373
})
7474
})
7575
})

test/integration/swap/common.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,20 @@ ethereumWithNode.addProvider(new providers.ethereum.EthereumRPCProvider(config.e
2626
ethereumWithNode.addProvider(new providers.ethereum.EthereumSwapProvider())
2727

2828
const chains = {
29-
bitcoinWithLedger,
30-
bitcoinWithNode,
31-
ethereumWithMetaMask,
32-
ethereumWithNode
29+
bitcoinWithLedger: { id: 'Bitcoin Ledger', name: 'bitcoin', client: bitcoinWithLedger },
30+
bitcoinWithNode: { id: 'Bitcoin Node', name: 'bitcoin', client: bitcoinWithNode },
31+
ethereumWithMetaMask: { id: 'Ethereum MetaMask', name: 'ethereum', client: ethereumWithMetaMask },
32+
ethereumWithNode: { id: 'Ethereum Node', name: 'ethereum', client: ethereumWithNode }
3333
}
3434

35-
async function getSwapParams (id, chain) {
36-
const unusedAddress = await chain.getUnusedAddress()
35+
async function getSwapParams (chain) {
36+
const unusedAddress = await chain.client.getUnusedAddress()
3737
const recipientAddress = unusedAddress.address
3838
const refundAddress = unusedAddress.address
3939
const expiration = parseInt(Date.now() / 1000) + parseInt(Math.random() * 1000000)
40-
const value = 10000
40+
const value = config[chain.name].value
4141

42-
console.log('\x1b[2m', `Swap Params for ${id}`, '\x1b[0m')
42+
console.log('\x1b[2m', `Swap Params for ${chain.id}`, '\x1b[0m')
4343
console.log('\x1b[2m', 'Recipient Address:', recipientAddress, '\x1b[0m')
4444
console.log('\x1b[2m', 'Refund Address:', refundAddress, '\x1b[0m')
4545
console.log('\x1b[2m', 'Expiry:', expiration, '\x1b[0m')
@@ -53,29 +53,29 @@ async function getSwapParams (id, chain) {
5353
}
5454
}
5555

56-
async function initiateAndVerify (chain, chainId, secretHash, swapParams) {
57-
console.log('\x1b[33m', `Initiating ${chainId}: Watch prompt on wallet`, '\x1b[0m')
56+
async function initiateAndVerify (chain, secretHash, swapParams) {
57+
console.log('\x1b[33m', `Initiating ${chain.id}: Watch prompt on wallet`, '\x1b[0m')
5858
const initiationParams = [swapParams.value, swapParams.recipientAddress, swapParams.refundAddress, secretHash, swapParams.expiration]
5959
const [ initiationTx, initiationTxId ] = await Promise.all([
60-
chain.findInitiateSwapTransaction(...initiationParams),
61-
chain.initiateSwap(...initiationParams)
60+
chain.client.findInitiateSwapTransaction(...initiationParams),
61+
chain.client.initiateSwap(...initiationParams)
6262
])
6363
expect(initiationTx.hash).to.equal(initiationTxId)
64-
const isVerified = await chain.verifyInitiateSwapTransaction(initiationTxId, ...initiationParams)
64+
const isVerified = await chain.client.verifyInitiateSwapTransaction(initiationTxId, ...initiationParams)
6565
expect(isVerified).to.equal(true)
66-
console.log(`${chainId} Initiated ${initiationTxId}`)
66+
console.log(`${chain.id} Initiated ${initiationTxId}`)
6767
return initiationTxId
6868
}
6969

70-
async function claimAndVerify (chain, chainId, initiationTxId, secret, swapParams) {
71-
console.log('\x1b[33m', `Claiming ${chainId}: Watch prompt on wallet`, '\x1b[0m')
70+
async function claimAndVerify (chain, initiationTxId, secret, swapParams) {
71+
console.log('\x1b[33m', `Claiming ${chain.id}: Watch prompt on wallet`, '\x1b[0m')
7272
const secretHash = crypto.sha256(secret)
7373
const [ claimTx, claimTxId ] = await Promise.all([
74-
chain.findClaimSwapTransaction(initiationTxId, swapParams.recipientAddress, swapParams.refundAddress, secretHash, swapParams.expiration),
75-
chain.claimSwap(initiationTxId, swapParams.recipientAddress, swapParams.refundAddress, secret, swapParams.expiration)
74+
chain.client.findClaimSwapTransaction(initiationTxId, swapParams.recipientAddress, swapParams.refundAddress, secretHash, swapParams.expiration),
75+
chain.client.claimSwap(initiationTxId, swapParams.recipientAddress, swapParams.refundAddress, secret, swapParams.expiration)
7676
])
7777
expect(claimTx.hash).to.equal(claimTxId)
78-
console.log(`${chainId} Claimed ${claimTxId}`)
78+
console.log(`${chain.id} Claimed ${claimTxId}`)
7979
return claimTx.secret
8080
}
8181

test/integration/swap/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ export default {
55
username: 'bitcoin',
66
password: 'local321'
77
},
8-
network: 'bitcoin_testnet'
8+
network: 'bitcoin_testnet',
9+
value: 10000
910
},
1011
ethereum: {
1112
rpc: {
1213
host: 'http://localhost:8545'
1314
},
15+
value: 100000000000000,
1416
metaMaskConnector: {
1517
port: 3333
1618
}

test/integration/swap/singleChain.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,40 @@ process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0
88

99
chai.use(chaiAsPromised)
1010

11-
async function testSingle (id, chain) {
11+
async function testSingle (chain) {
1212
console.log('\x1b[33m', `Generating secret: Watch for prompt`, '\x1b[0m')
13-
const secret = await chain.generateSecret('test')
13+
const secret = await chain.client.generateSecret('test')
1414
const secretHash = crypto.sha256(secret)
15-
const swapParams = await getSwapParams(id, chain)
15+
const swapParams = await getSwapParams(chain)
1616

17-
const initiationTxId = await initiateAndVerify(chain, id, secretHash, swapParams)
18-
const revealedSecret = await claimAndVerify(chain, id, initiationTxId, secret, swapParams)
17+
const initiationTxId = await initiateAndVerify(chain, secretHash, swapParams)
18+
const revealedSecret = await claimAndVerify(chain, initiationTxId, secret, swapParams)
1919
expect(revealedSecret).to.equal(secret)
2020
}
2121

2222
describe('Swap Single Chain Flow', function () {
2323
this.timeout(120000)
2424

2525
it('Bitcoin - Ledger', async () => {
26-
await testSingle('BitcoinLedger', chains.bitcoinWithLedger)
26+
await testSingle(chains.bitcoinWithLedger)
2727
})
2828

2929
it('Bitcoin - Node', async () => {
3030
const interval = setInterval(() => {
31-
chains.bitcoinWithNode.generateBlock(1)
31+
chains.bitcoinWithNode.client.generateBlock(1)
3232
}, 1000)
33-
await testSingle('BitcoinNode', chains.bitcoinWithNode)
33+
await testSingle(chains.bitcoinWithNode)
3434
clearInterval(interval)
3535
})
3636

37-
it('Ethereum - MetaMask', async () => {
37+
it.only('Ethereum - MetaMask', async () => {
3838
console.log('\x1b[36m', 'Starting MetaMask connector on http://localhost:3333 - Open in browser to continue', '\x1b[0m')
3939
await metaMaskConnector.start()
40-
await testSingle('EthereumMetaMask', chains.ethereumWithMetaMask)
40+
await testSingle(chains.ethereumWithMetaMask)
4141
await metaMaskConnector.stop()
4242
})
4343

4444
it('Ethereum - Node', async () => {
45-
await testSingle('EthereumNode', chains.ethereumWithNode)
45+
await testSingle(chains.ethereumWithNode)
4646
})
4747
})

0 commit comments

Comments
 (0)