@@ -156,10 +156,10 @@ contract ElementBridge is IDefiBridge {
156
156
uint256 internal constant MIN_GAS_FOR_EXPIRY_REMOVAL = 25000 ;
157
157
158
158
// event emitted on every successful convert call
159
- event LogConvert (uint256 indexed nonce , uint256 totalInputValue );
159
+ event LogConvert (uint256 indexed nonce , uint256 totalInputValue , int64 gasUsed );
160
160
161
161
// event emitted on every attempt to finalise, successful or otherwise
162
- event LogFinalise (uint256 indexed nonce , bool success , string message );
162
+ event LogFinalise (uint256 indexed nonce , bool success , string message , int64 gasUsed );
163
163
164
164
// event emitted on wvery newly configured pool
165
165
event LogPoolAdded (address poolAddress , address wrappedPositionAddress , uint64 expiry );
@@ -400,6 +400,13 @@ contract ElementBridge is IDefiBridge {
400
400
hashValue = uint256 (keccak256 (abi.encodePacked (asset, uint256 (expiry))));
401
401
}
402
402
403
+ struct ConvertArgs {
404
+ address inputAssetAddress;
405
+ uint256 totalInputValue;
406
+ uint256 interactionNonce;
407
+ uint64 auxData;
408
+ }
409
+
403
410
/**
404
411
* @dev Function to add a new interaction to the bridge
405
412
* Converts the amount of input asset given to the market determined amount of tranche asset
@@ -431,6 +438,8 @@ contract ElementBridge is IDefiBridge {
431
438
bool isAsync
432
439
)
433
440
{
441
+ int64 gasAtStart = int64 (int256 (gasleft ()));
442
+ int64 gasUsed = 0 ;
434
443
// ### INITIALIZATION AND SANITY CHECKS
435
444
if (msg .sender != rollupProcessor) {
436
445
revert INVALID_CALLER ();
@@ -449,36 +458,47 @@ contract ElementBridge is IDefiBridge {
449
458
isAsync = true ;
450
459
outputValueA = 0 ;
451
460
outputValueB = 0 ;
461
+
462
+ // capture the provided arguments in a struct to prevent 'stack too deep' errors
463
+ ConvertArgs memory convertArgs = ConvertArgs ({
464
+ inputAssetAddress: inputAssetA.erc20Address,
465
+ totalInputValue: totalInputValue,
466
+ interactionNonce: interactionNonce,
467
+ auxData: auxData
468
+ });
469
+
452
470
// retrieve the appropriate pool for this interaction and verify that it exists
453
- Pool storage pool = pools[hashAssetAndExpiry (inputAssetA.erc20Address, auxData)];
471
+ Pool storage pool = pools[hashAssetAndExpiry (convertArgs.inputAssetAddress, convertArgs. auxData)];
454
472
if (pool.trancheAddress == address (0 )) {
455
473
revert POOL_NOT_FOUND ();
456
474
}
457
475
ITranche tranche = ITranche (pool.trancheAddress);
458
- if (block .timestamp >= tranche.unlockTimestamp ()) {
476
+ uint64 trancheExpiry = uint64 (tranche.unlockTimestamp ());
477
+ if (block .timestamp >= trancheExpiry) {
459
478
revert TRANCHE_ALREADY_EXPIRED ();
460
479
}
461
- uint64 trancheExpiry = uint64 (tranche. unlockTimestamp ());
480
+
462
481
// approve the transfer of tokens to the balancer address
463
- ERC20 (inputAssetA.erc20Address ).approve (balancerAddress, totalInputValue);
482
+ ERC20 (convertArgs.inputAssetAddress ).approve (balancerAddress, convertArgs. totalInputValue);
464
483
// execute the swap on balancer
465
- uint256 principalTokensAmount = exchangeAssetForTrancheTokens (inputAssetA.erc20Address , pool, totalInputValue);
484
+ uint256 principalTokensAmount = exchangeAssetForTrancheTokens (convertArgs.inputAssetAddress , pool, convertArgs. totalInputValue);
466
485
// store the tranche that underpins our interaction, the expiry and the number of received tokens against the nonce
467
- Interaction storage newInteraction = interactions[interactionNonce];
486
+ Interaction storage newInteraction = interactions[convertArgs. interactionNonce];
468
487
newInteraction.expiry = trancheExpiry;
469
488
newInteraction.failed = false ;
470
489
newInteraction.finalised = false ;
471
490
newInteraction.quantityPT = principalTokensAmount;
472
491
newInteraction.trancheAddress = pool.trancheAddress;
473
492
// add the nonce and expiry to our expiry heap
474
- addNonceAndExpiryToNonceMapping (interactionNonce, trancheExpiry);
493
+ addNonceAndExpiryToNonceMapping (convertArgs. interactionNonce, trancheExpiry);
475
494
// increase our tranche account deposits and holdings
476
495
// other members are left as their initial values (all zeros)
477
496
TrancheAccount storage trancheAccount = trancheAccounts[newInteraction.trancheAddress];
478
497
trancheAccount.numDeposits++ ;
479
498
trancheAccount.quantityTokensHeld += newInteraction.quantityPT;
480
- emit LogConvert (interactionNonce, totalInputValue);
481
- finaliseExpiredInteractions (MIN_GAS_FOR_FUNCTION_COMPLETION);
499
+ unchecked { gasUsed = gasAtStart - int64 (int256 (gasleft ())); }
500
+ emit LogConvert (convertArgs.interactionNonce, convertArgs.totalInputValue, gasUsed);
501
+ finaliseExpiredInteractions (MIN_GAS_FOR_FUNCTION_COMPLETION);
482
502
// we need to get here with MIN_GAS_FOR_FUNCTION_COMPLETION gas to exit.
483
503
}
484
504
@@ -584,6 +604,8 @@ contract ElementBridge is IDefiBridge {
584
604
bool interactionCompleted
585
605
)
586
606
{
607
+ int64 gasAtStart = int64 (int256 (gasleft ()));
608
+ int64 gasUsed = 0 ;
587
609
if (msg .sender != rollupProcessor) {
588
610
revert INVALID_CALLER ();
589
611
}
@@ -602,7 +624,7 @@ contract ElementBridge is IDefiBridge {
602
624
TrancheAccount storage trancheAccount = trancheAccounts[interaction.trancheAddress];
603
625
if (trancheAccount.numDeposits == 0 ) {
604
626
// shouldn't be possible, this means we have had no deposits against this tranche
605
- setInteractionAsFailure (interaction, interactionNonce, 'NO_DEPOSITS_FOR_TRANCHE ' );
627
+ setInteractionAsFailure (interaction, interactionNonce, 'NO_DEPOSITS_FOR_TRANCHE ' , 0 );
606
628
popInteractionFromNonceMapping (interaction, interactionNonce);
607
629
return (0 , 0 , false );
608
630
}
@@ -617,12 +639,14 @@ contract ElementBridge is IDefiBridge {
617
639
trancheAccount.quantityAssetRemaining = valueRedeemed;
618
640
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_SUCCEEDED;
619
641
} catch Error (string memory errorMessage ) {
620
- setInteractionAsFailure (interaction, interactionNonce, errorMessage);
642
+ unchecked { gasUsed = gasAtStart - int64 (int256 (gasleft ())); }
643
+ setInteractionAsFailure (interaction, interactionNonce, errorMessage, gasUsed);
621
644
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_FAILED;
622
645
popInteractionFromNonceMapping (interaction, interactionNonce);
623
646
return (0 , 0 , false );
624
647
} catch {
625
- setInteractionAsFailure (interaction, interactionNonce, 'UNKNOWN_ERROR_FROM_TRANCHE_WITHDRAW ' );
648
+ unchecked { gasUsed = gasAtStart - int64 (int256 (gasleft ())); }
649
+ setInteractionAsFailure (interaction, interactionNonce, 'UNKNOWN_ERROR_FROM_TRANCHE_WITHDRAW ' , gasUsed);
626
650
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_FAILED;
627
651
popInteractionFromNonceMapping (interaction, interactionNonce);
628
652
return (0 , 0 , false );
@@ -661,7 +685,8 @@ contract ElementBridge is IDefiBridge {
661
685
outputValueA = amountToAllocate;
662
686
outputValueB = 0 ;
663
687
interactionCompleted = true ;
664
- emit LogFinalise (interactionNonce, interactionCompleted, '' );
688
+ unchecked { gasUsed = gasAtStart - int64 (int256 (gasleft ())); }
689
+ emit LogFinalise (interactionNonce, interactionCompleted, '' , gasUsed);
665
690
}
666
691
667
692
/**
@@ -673,10 +698,11 @@ contract ElementBridge is IDefiBridge {
673
698
function setInteractionAsFailure (
674
699
Interaction storage interaction ,
675
700
uint256 interactionNonce ,
676
- string memory message
701
+ string memory message ,
702
+ int64 gasUsed
677
703
) internal {
678
704
interaction.failed = true ;
679
- emit LogFinalise (interactionNonce, false , message);
705
+ emit LogFinalise (interactionNonce, false , message, gasUsed );
680
706
}
681
707
682
708
/**
@@ -780,7 +806,7 @@ contract ElementBridge is IDefiBridge {
780
806
(bool canBeFinalised , string memory message ) = interactionCanBeFinalised (interaction);
781
807
if (! canBeFinalised) {
782
808
// can't be finalised, add to failures and pop from nonces
783
- setInteractionAsFailure (interaction, nextNonce, message);
809
+ setInteractionAsFailure (interaction, nextNonce, message, 0 );
784
810
nonces.pop ();
785
811
continue ;
786
812
}
0 commit comments