@@ -245,62 +245,39 @@ async function getBridgeStatus(params: BridgeStatusParams): Promise<any> {
245
245
return json
246
246
}
247
247
248
- export async function bridgeFunds (
249
- bridgingParams : BridgingParams ,
250
- wallet : EIP155Lib | SmartAccountLib
251
- ) : Promise < void > {
252
- performance . mark ( 'startGetQuote' )
253
- console . log ( 'Bridging funds' , bridgingParams )
248
+
249
+ async function getBridgingTransactions ( bridgingParams : BridgingParams , walletAddress : string ) : Promise < any > {
250
+ const transactions = [ ]
254
251
const originalAmount = bridgingParams . amount
255
252
bridgingParams . amount = Math . round ( originalAmount * AMOUNT_MULTIPLIER )
256
253
const quote = await getQuote ( bridgingParams )
257
- performance . mark ( 'endGetQuote' )
258
- console . log ( 'Fetched quote' , quote )
259
-
260
254
const route = quote . result . routes [ 0 ]
261
255
if ( ! route ) {
262
256
throw new Error ( 'No routes found' )
263
257
}
264
- performance . mark ( 'startGetRouteTransactionData' )
265
258
const apiReturnData = await getRouteTransactionData ( route )
266
- performance . mark ( 'endGetRouteTransactionData' )
267
259
const approvalData = apiReturnData . result . approvalData
268
260
const { allowanceTarget, minimumApprovalAmount } = approvalData
269
- performance . mark ( 'startGetWalletAddress' )
270
261
const sourceChainProvider = new providers . JsonRpcProvider (
271
262
EIP155_CHAINS [ `eip155:${ bridgingParams . fromChainId } ` as TEIP155Chain ] . rpc
272
263
)
273
- const sourceChainConnectedWallet = await wallet . connect ( sourceChainProvider )
274
- const walletAddress = wallet . getAddress ( )
275
- performance . mark ( 'endGetWalletAddress' )
276
- console . log ( { approvalData } )
277
264
let currentNonce = await sourceChainProvider . getTransactionCount ( walletAddress )
278
- // approvalData from apiReturnData is null for native tokens
279
- // Values are returned for ERC20 tokens but token allowance needs to be checked
280
265
if ( approvalData !== null ) {
281
- // Fetches token allowance given to Bungee contracts
282
- performance . mark ( 'startCheckAllowance' )
283
266
const allowanceCheckStatus = await checkAllowance ( {
284
267
chainId : bridgingParams . fromChainId ,
285
268
owner : bridgingParams . userAddress ,
286
269
allowanceTarget,
287
270
tokenAddress : bridgingParams . fromAssetAddress
288
271
} )
289
- performance . mark ( 'endCheckAllowance' )
290
272
const allowanceValue = allowanceCheckStatus . result ?. value
291
- console . log ( 'Allowance value' , allowanceValue )
292
273
if ( minimumApprovalAmount > allowanceValue ) {
293
- console . log ( "Bungee contracts don't have sufficient allowance" )
294
- performance . mark ( 'startGetApprovalTransactionData' )
295
274
const approvalTransactionData = await getApprovalTransactionData ( {
296
275
chainId : bridgingParams . fromChainId ,
297
276
owner : bridgingParams . userAddress ,
298
277
allowanceTarget,
299
278
tokenAddress : bridgingParams . fromAssetAddress ,
300
279
amount : bridgingParams . amount
301
280
} )
302
- performance . mark ( 'endGetApprovalTransactionData' )
303
- performance . mark ( 'startApprovalTransactionGasEstimate' )
304
281
const gasPrice = sourceChainProvider . getGasPrice ( )
305
282
const gasEstimate = await sourceChainProvider . estimateGas ( {
306
283
from : walletAddress ,
@@ -309,10 +286,7 @@ export async function bridgeFunds(
309
286
data : approvalTransactionData . result ?. data ,
310
287
gasPrice : gasPrice
311
288
} )
312
- performance . mark ( 'endApprovalTransactionGasEstimate' )
313
-
314
- performance . mark ( 'startApprovalTransactionSend' )
315
- const hash = await sourceChainConnectedWallet . sendTransaction ( {
289
+ transactions . push ( {
316
290
from : approvalTransactionData . result ?. from ,
317
291
to : approvalTransactionData . result ?. to ,
318
292
value : '0x00' ,
@@ -321,14 +295,9 @@ export async function bridgeFunds(
321
295
gasLimit : gasEstimate ,
322
296
nonce : currentNonce
323
297
} )
324
- const receipt = typeof hash === 'string' ? hash : hash ?. hash
325
- performance . mark ( 'endApprovalTransactionSend' )
326
- console . log ( 'Approval Transaction' , { receipt } )
327
298
currentNonce ++
328
299
}
329
300
}
330
-
331
- performance . mark ( 'startBridgingTransactionGasEstimate' )
332
301
const gasPrice = await sourceChainProvider . getGasPrice ( )
333
302
let gasEstimate = BigInt ( '0x029a6b' ) * BigInt ( 4 )
334
303
try {
@@ -344,10 +313,7 @@ export async function bridgeFunds(
344
313
} catch {
345
314
console . log ( 'Failed gas estimate. Using default with 4x increase' )
346
315
}
347
- performance . mark ( 'endBridgingTransactionGasEstimate' )
348
-
349
- performance . mark ( 'startBridgingTransactionSend' )
350
- const hash = await sourceChainConnectedWallet . sendTransaction ( {
316
+ transactions . push ( {
351
317
from : walletAddress ,
352
318
to : apiReturnData . result . txTarget ,
353
319
data : apiReturnData . result . txData ,
@@ -356,11 +322,29 @@ export async function bridgeFunds(
356
322
gasLimit : gasEstimate ,
357
323
nonce : currentNonce
358
324
} )
359
- const receipt = typeof hash === 'string' ? hash : hash ?. hash
360
- console . log ( 'Bridging Transaction : ' , { receipt } )
361
- performance . mark ( 'endBridgingTransactionSend' )
325
+ return transactions
326
+ }
327
+
362
328
363
- performance . mark ( 'startBridgingTransactionCheck' )
329
+ export async function bridgeFunds (
330
+ bridgingParams : BridgingParams ,
331
+ wallet : EIP155Lib | SmartAccountLib
332
+ ) : Promise < void > {
333
+ const originalAmount = bridgingParams . amount
334
+ const sourceChainProvider = new providers . JsonRpcProvider (
335
+ EIP155_CHAINS [ `eip155:${ bridgingParams . fromChainId } ` as TEIP155Chain ] . rpc
336
+ )
337
+ const sourceChainConnectedWallet = await wallet . connect ( sourceChainProvider )
338
+ const walletAddress = wallet . getAddress ( )
339
+ console . log ( 'Getting bridging transactions' )
340
+ const transactions = await getBridgingTransactions ( bridgingParams , walletAddress )
341
+ console . log ( 'Bridging transactions' , transactions ) ;
342
+ for ( const transaction of transactions ) {
343
+ const hash = await sourceChainConnectedWallet . sendTransaction ( transaction )
344
+ const receipt = typeof hash === 'string' ? hash : hash ?. hash
345
+ console . log ( 'Transaction broadcasted' , { receipt} ) ;
346
+
347
+ }
364
348
let interations = 0
365
349
while ( interations < 20 ) {
366
350
const balance = await getErc20TokenBalance (
@@ -369,100 +353,12 @@ export async function bridgeFunds(
369
353
walletAddress as Hex ,
370
354
false
371
355
)
372
- console . log ( 'Checking destination address' , { balance, originalAmount } )
373
-
374
356
if ( balance >= originalAmount ) {
375
357
console . log ( 'Bridging completed' )
376
- performance . mark ( 'endBridgingTransactionCheck' )
377
- printMeasurements ( )
378
358
return
379
359
}
380
360
await new Promise ( resolve => setTimeout ( resolve , 1500 ) )
381
361
interations ++
382
362
}
383
363
}
384
364
385
- function printMeasurements ( ) {
386
- console . log (
387
- `Total duration: ${
388
- performance . measure ( 'total-duration' , 'startGetQuote' , 'endBridgingTransactionCheck' ) . duration
389
- } ms`
390
- )
391
- console . log (
392
- `Get quote: ${ performance . measure ( 'get-quote' , 'startGetQuote' , 'endGetQuote' ) . duration } ms`
393
- )
394
- console . log (
395
- `Get Route Transaction Data: ${
396
- performance . measure (
397
- 'get-route-transaction' ,
398
- 'startGetRouteTransactionData' ,
399
- 'endGetRouteTransactionData'
400
- ) . duration
401
- } ms`
402
- )
403
- console . log (
404
- `Get Wallet Address: ${
405
- performance . measure ( 'get-wallet-address' , 'startGetWalletAddress' , 'endGetWalletAddress' )
406
- . duration
407
- } ms`
408
- )
409
- console . log (
410
- `Check Allowance: ${
411
- performance . measure ( 'check-allowance' , 'startCheckAllowance' , 'endCheckAllowance' ) . duration
412
- } ms`
413
- )
414
- console . log (
415
- `Get Approval Transaction Data: ${
416
- performance . measure (
417
- 'get-approval-tx-data' ,
418
- 'startGetApprovalTransactionData' ,
419
- 'endGetApprovalTransactionData'
420
- ) . duration
421
- } ms`
422
- )
423
- console . log (
424
- `Get Approval Transaction Gas Estimate: ${
425
- performance . measure (
426
- 'get-approval-tx-gas-estimate' ,
427
- 'startApprovalTransactionGasEstimate' ,
428
- 'endApprovalTransactionGasEstimate'
429
- ) . duration
430
- } ms`
431
- )
432
- console . log (
433
- `Approval transaction send: ${
434
- performance . measure (
435
- 'approval-transaction-send' ,
436
- 'startApprovalTransactionSend' ,
437
- 'endApprovalTransactionSend'
438
- ) . duration
439
- } ms`
440
- )
441
- console . log (
442
- `Bridging transaction gas estimate: ${
443
- performance . measure (
444
- 'bridging-tx-gas-estimate' ,
445
- 'startBridgingTransactionGasEstimate' ,
446
- 'endBridgingTransactionGasEstimate'
447
- ) . duration
448
- } ms`
449
- )
450
- console . log (
451
- `Bridging transaction send: ${
452
- performance . measure (
453
- 'bridging-tx-send' ,
454
- 'startBridgingTransactionSend' ,
455
- 'endBridgingTransactionSend'
456
- ) . duration
457
- } ms`
458
- )
459
- console . log (
460
- `Bridging transaction check: ${
461
- performance . measure (
462
- 'bridging-tx-check' ,
463
- 'startBridgingTransactionCheck' ,
464
- 'endBridgingTransactionCheck'
465
- ) . duration
466
- } ms`
467
- )
468
- }
0 commit comments