@@ -32,8 +32,8 @@ async function getContractInstance(provider, pathToJson){
3232 var contractName = contract ( JSON . parse ( jsonFileContents ) )
3333 contractName . setProvider ( provider )
3434 var instance = await contractName . deployed ( ) . catch ( function ( ) {
35- console . log ( "Failed getting the contractName!" ) ;
36- } )
35+ console . log ( "Failed getting the contractName!" ) ;
36+ } )
3737
3838 return instance
3939}
@@ -42,32 +42,32 @@ async function getContractInstance(provider, pathToJson){
4242// Initialization of the parameters
4343async function init ( provider1 , provider2 , contractOwner1 , contractOwner2 , Alice1 , Bob2 , tokenSupply , senderInitialBalance ) {
4444
45- interopContract1 = await getContractInstance ( provider1 , '../build/contracts/InteroperationBaseClassERC20.json' ) . catch ( function ( ) {
46- console . log ( "Failed getting interopContract1!" ) ;
47- } )
48- interopContract2 = await getContractInstance ( provider2 , '../build/contracts/InteroperationBaseClassERC20.json' ) . catch ( function ( ) {
49- console . log ( "Failed getting interopContract2!" ) ;
50- } )
45+ interopContract1 = await getContractInstance ( provider1 , '../build/contracts/InteroperationBaseClassERC20.json' ) . catch ( function ( ) {
46+ console . log ( "Failed getting interopContract1!" ) ;
47+ } )
48+ interopContract2 = await getContractInstance ( provider2 , '../build/contracts/InteroperationBaseClassERC20.json' ) . catch ( function ( ) {
49+ console . log ( "Failed getting interopContract2!" ) ;
50+ } )
5151
5252
5353 AliceERC20 = await getContractInstance ( provider1 , '../build/contracts/AliceERC20.json' ) . catch ( function ( ) {
54- console . log ( "Failed getting AliceERC20 token contract!" ) ;
55- } )
54+ console . log ( "Failed getting AliceERC20 token contract!" ) ;
55+ } )
5656 BobERC20 = await getContractInstance ( provider2 , '../build/contracts/BobERC20.json' ) . catch ( function ( ) {
57- console . log ( "Failed getting BobERC20 token contract!" ) ;
58- } )
59-
60- // Issue AliceERC20 tokens to Alice in Network 1 and BobERC20 tokens to Bob in Network 2
61- // SUFFICIENT TO RUN THIS ONCE. Comment it out after the first run unless more tokens have to issued to Alice and/or Bob.
62- ///*
63- await AliceERC20 . transfer ( Alice1 , senderInitialBalance , { from : contractOwner1 } ) . catch ( function ( ) {
64- console . log ( "AliceERC20 transfer threw an error; Probably the token supply is used up!" ) ;
65- } )
66- await BobERC20 . transfer ( Bob2 , senderInitialBalance , { from : contractOwner2 } ) . catch ( function ( ) {
67- console . log ( "BobERC20 transfer threw an error; Probably the token supply is used up!" ) ;
68- } )
69- //*/
70-
57+ console . log ( "Failed getting BobERC20 token contract!" ) ;
58+ } )
59+
60+ // Issue AliceERC20 tokens to Alice in Network 1 and BobERC20 tokens
61+ // to Bob in Network 2. A minimal number of tokens equal to the
62+ // number of token being exchanged is issued to Alice and Bob to
63+ // ensure that the exchange in this test application does not fail
64+ // due to insufficient funds.
65+ await AliceERC20 . transfer ( Alice1 , senderInitialBalance , { from : contractOwner1 } ) . catch ( function ( ) {
66+ console . log ( "AliceERC20 transfer threw an error; Probably the token supply is used up!" ) ;
67+ } )
68+ await BobERC20 . transfer ( Bob2 , senderInitialBalance , { from : contractOwner2 } ) . catch ( function ( ) {
69+ console . log ( "BobERC20 transfer threw an error; Probably the token supply is used up!" ) ;
70+ } )
7171}
7272
7373
@@ -77,11 +77,12 @@ async function init(provider1, provider2, contractOwner1, contractOwner2, Alice1
7777// used when locking it
7878async function claimToken ( interopContract , lockContractId , recipient , preimage ) {
7979 console . log ( "\n Claiming %s using preimage: %o" , lockContractId , preimage )
80- var claimStatus = await interopContract . claimFungibleAsset ( lockContractId , preimage , {
81- from : recipient
82- } ) . catch ( function ( ) {
83- console . log ( "claimFungibleAsset threw an error" ) ;
84- } )
80+ var claimStatus = await interopContract . claimFungibleAsset ( lockContractId , preimage , {
81+ from : recipient
82+ } ) . catch ( function ( ) {
83+ console . log ( "claimFungibleAsset threw an error" ) ;
84+ claimStatus = false
85+ } )
8586
8687 return claimStatus
8788}
@@ -90,11 +91,12 @@ async function claimToken(interopContract, lockContractId, recipient, preimage)
9091// Unlock function of a locked fungible asset/token
9192async function unlockToken ( interopContract , lockContractId , sender ) {
9293
93- var unlockStatus = await interopContract . unlockFungibleAsset ( lockContractId , {
94+ var unlockStatus = await interopContract . unlockFungibleAsset ( lockContractId , {
9495 from : sender
95- } ) . catch ( function ( ) {
96+ } ) . catch ( function ( ) {
9697 console . log ( "unlockFungibleAsset threw an error" ) ;
97- } )
98+ unlockStatus = false
99+ } )
98100
99101 return unlockStatus
100102}
@@ -105,13 +107,15 @@ async function unlockToken(interopContract, lockContractId, sender) {
105107// for 'recipient' using the contract constructs of 'interopContract'
106108// with hashLock and timeLock providing the conditions for claiming/unlocking
107109async function lockToken ( sender , recipient , tokenContract , tokenAmount , interopContract , hashLock , timeLock ) {
108- // initiator of the swap has to first designate the swap contract as a spender of his/her money
109- // with allowance matching the swap amount
110- await tokenContract . approve ( interopContract . address , tokenAmount , { from : sender } ) . catch ( function ( ) {
111- console . log ( "Token approval failed!!!" ) ;
112- } )
110+
111+ // initiator of the swap has to first designate the swap contract as a spender of his/her money
112+ // with allowance matching the swap amount
113+ await tokenContract . approve ( interopContract . address , tokenAmount , { from : sender } ) . catch ( function ( ) {
114+ console . log ( "Token approval failed!!!" ) ;
115+ return false
116+ } )
113117
114- return interopContract . lockFungibleAsset (
118+ var lockStatus = interopContract . lockFungibleAsset (
115119 recipient ,
116120 tokenContract . address ,
117121 tokenAmount ,
@@ -120,12 +124,19 @@ async function lockToken(sender, recipient, tokenContract, tokenAmount, interopC
120124 {
121125 from : sender
122126 }
123- )
127+ ) . catch ( function ( ) {
128+ console . log ( "lockFungibleAsset threw an error" ) ;
129+ lockStatus = false
130+ } )
131+
132+ return lockStatus
124133}
125134
126135
127- // A function to obtain and print the account balances of a pair of accounts in the two participating networks.
128- // Designed for printing the account balances of the sender and recipient at various stages of the exchange.
136+ // A function to obtain and print the account balances of a pair
137+ // of accounts in the two participating networks.
138+ // Designed for printing the account balances of the sender and
139+ // recipient at various stages of the exchange.
129140async function getBalances ( Alice1 , Bob1 , Alice2 , Bob2 ) {
130141
131142 var AliceAliceERC20Balance = await AliceERC20 . balanceOf ( Alice1 )
@@ -173,37 +184,46 @@ async function main() {
173184
174185 // Initialization
175186 const tokenSupply = 1000
176- const senderInitialBalance = 100
187+ const tokenAmount = 10 // Number of tokens to be exchanged
188+ const senderInitialBalance = tokenAmount
177189
178190 await init ( provider1 , provider2 , contractOwner1 , contractOwner2 , Alice1 , Bob2 , tokenSupply , senderInitialBalance )
179191
180192 console . log ( "\n Balances after init():" )
181193 await getBalances ( Alice1 , Bob1 , Alice2 , Bob2 )
182194
183- //hashPair = newSecretHashPair()
184195 preimage = crypto . randomBytes ( 32 ) // to sample a preimage for the hash
185196 hash = crypto . createHash ( 'sha256' ) . update ( preimage ) . digest ( )
186197 console . log ( "Hash: " , hash )
187198
188- let tokenAmount = 10
189199 let timeOut = 15
190- let timeLockSeconds = Math . floor ( Date . now ( ) / 1000 ) + 2 * timeOut
200+ let timeLockSeconds = Math . floor ( Date . now ( ) / 1000 ) + 2 * timeOut
191201
192202 // Creating a HTLC contract for Alice locking her AliceERC20 tokens for Bob
193203 let lockTx1 = await lockToken ( Alice1 , Bob1 , AliceERC20 , tokenAmount , interopContract1 , hash , timeLockSeconds )
194- let lockContractId1 = lockTx1 . logs [ 0 ] . args . lockContractId
204+ if ( ! lockTx1 ) {
205+ console . log ( "\n !!! Locking of Alice's tokens failed in Netowrk 1. Aborting here !!!" )
206+ return
207+ }
208+ let lockContractId1 = lockTx1 . logs [ 0 ] . args . lockContractId
195209 console . log ( "lockContractID1: " , lockContractId1 )
196210
197211 console . log ( "\n Balances after Alice locks her tokens in Network 1:" )
198212 await getBalances ( Alice1 , Bob1 , Alice2 , Bob2 )
199213
200- // After he observes this lockContract created in Network 1, Bob creates a similar contract
201- // in Network 2 using the same hash to transfer the agreed upon amount of BobERC20 to Alice.
202- // Bob sets the timeLock such that he will have enough time to claim Alice's tokens in
203- // Network 1 after she claims Bob's tokens in Network 2.
204- timeLockSeconds = Math . floor ( Date . now ( ) / 1000 ) + timeOut
205- let lockTx2 = await lockToken ( Bob2 , Alice2 , BobERC20 , tokenAmount , interopContract2 , hash , timeLockSeconds )
206- let lockContractId2 = lockTx2 . logs [ 0 ] . args . lockContractId
214+ // After he observes this lockContract created in Network 1,
215+ // Bob creates a similar contract in Network 2 using the same
216+ // hash to transfer the agreed upon amount of BobERC20 to Alice.
217+ // Bob sets the timeLock such that he will have enough time to
218+ // claim Alice's tokens in Network 1 after she claims Bob's
219+ // tokens in Network 2.
220+ timeLockSeconds = Math . floor ( Date . now ( ) / 1000 ) + timeOut
221+ let lockTx2 = await lockToken ( Bob2 , Alice2 , BobERC20 , tokenAmount , interopContract2 , hash , timeLockSeconds )
222+ if ( ! lockTx2 ) {
223+ console . log ( "\n !!! Locking of Bob's tokens failed in Netowrk 2. Aborting here !!!" )
224+ return
225+ }
226+ let lockContractId2 = lockTx2 . logs [ 0 ] . args . lockContractId
207227 console . log ( "lockContractID2: " , lockContractId2 )
208228
209229 console . log ( "\n Balances after creating Bob locks his tokens in Network 2:" )
0 commit comments