@@ -35,6 +35,8 @@ contract ElementBridge is IDefiBridge {
35
35
error INVALID_CALLER ();
36
36
error ASSET_IDS_NOT_EQUAL ();
37
37
error ASSET_NOT_ERC20 ();
38
+ error INPUT_ASSETB_NOT_UNUSED ();
39
+ error OUTPUT_ASSETB_NOT_UNUSED ();
38
40
error INTERACTION_ALREADY_EXISTS ();
39
41
error POOL_NOT_FOUND ();
40
42
error UNKNOWN_NONCE ();
@@ -146,12 +148,12 @@ contract ElementBridge is IDefiBridge {
146
148
mapping (uint64 => uint256 []) private expiryToNonce;
147
149
148
150
// 48 hours in seconds, usd for calculating speeedbump expiries
149
- uint256 internal constant _FORTY_EIGHT_HOURS = 172800 ;
151
+ uint256 internal constant FORTY_EIGHT_HOURS = 172800 ;
150
152
151
153
uint256 internal constant MAX_UINT = type (uint256 ).max;
152
154
153
- uint256 internal constant MIN_GAS_FOR_CHECK_AND_FINALISE = 50000 ;
154
- uint256 internal constant MIN_GAS_FOR_FUNCTION_COMPLETION = 5000 ;
155
+ uint256 internal constant MIN_GAS_FOR_CHECK_AND_FINALISE = 40000 ;
156
+ uint256 internal constant MIN_GAS_FOR_FUNCTION_COMPLETION = 2000 ;
155
157
uint256 internal constant MIN_GAS_FOR_FAILED_INTERACTION = 20000 ;
156
158
uint256 internal constant MIN_GAS_FOR_EXPIRY_REMOVAL = 25000 ;
157
159
@@ -421,9 +423,9 @@ contract ElementBridge is IDefiBridge {
421
423
*/
422
424
function convert (
423
425
AztecTypes.AztecAsset calldata inputAssetA ,
424
- AztecTypes.AztecAsset calldata ,
426
+ AztecTypes.AztecAsset calldata inputAssetB ,
425
427
AztecTypes.AztecAsset calldata outputAssetA ,
426
- AztecTypes.AztecAsset calldata ,
428
+ AztecTypes.AztecAsset calldata outputAssetB ,
427
429
uint256 totalInputValue ,
428
430
uint256 interactionNonce ,
429
431
uint64 auxData ,
@@ -450,6 +452,12 @@ contract ElementBridge is IDefiBridge {
450
452
if (inputAssetA.assetType != AztecTypes.AztecAssetType.ERC20 ) {
451
453
revert ASSET_NOT_ERC20 ();
452
454
}
455
+ if (inputAssetB.assetType != AztecTypes.AztecAssetType.NOT_USED) {
456
+ revert INPUT_ASSETB_NOT_UNUSED ();
457
+ }
458
+ if (outputAssetB.assetType != AztecTypes.AztecAssetType.NOT_USED) {
459
+ revert OUTPUT_ASSETB_NOT_UNUSED ();
460
+ }
453
461
if (interactions[interactionNonce].expiry != 0 ) {
454
462
revert INTERACTION_ALREADY_EXISTS ();
455
463
}
@@ -614,15 +622,18 @@ contract ElementBridge is IDefiBridge {
614
622
if (interaction.expiry == 0 ) {
615
623
revert UNKNOWN_NONCE ();
616
624
}
617
- if (interaction.expiry > block .timestamp ) {
625
+ if (interaction.expiry >= block .timestamp ) {
618
626
revert BRIDGE_NOT_READY ();
619
627
}
620
628
if (interaction.finalised) {
621
629
revert ALREADY_FINALISED ();
622
630
}
623
631
624
632
TrancheAccount storage trancheAccount = trancheAccounts[interaction.trancheAddress];
625
- if (trancheAccount.numDeposits == 0 ) {
633
+ // cache a couple of frequently used values from the tranche account here
634
+ uint32 numDepositsIntoTranche = trancheAccount.numDeposits;
635
+ uint256 trancheTokensHeld = trancheAccount.quantityTokensHeld;
636
+ if (numDepositsIntoTranche == 0 ) {
626
637
// shouldn't be possible, this means we have had no deposits against this tranche
627
638
setInteractionAsFailure (interaction, interactionNonce, 'NO_DEPOSITS_2 ' , 0 );
628
639
popInteractionFromNonceMapping (interaction, interactionNonce);
@@ -634,7 +645,7 @@ contract ElementBridge is IDefiBridge {
634
645
// tranche not redeemed, we need to withdraw the principal
635
646
// convert the tokens back to underlying using the tranche
636
647
ITranche tranche = ITranche (interaction.trancheAddress);
637
- try tranche.withdrawPrincipal (trancheAccount.quantityTokensHeld , address (this )) returns (uint256 valueRedeemed ) {
648
+ try tranche.withdrawPrincipal (trancheTokensHeld , address (this )) returns (uint256 valueRedeemed ) {
638
649
trancheAccount.quantityAssetRedeemed = valueRedeemed;
639
650
trancheAccount.quantityAssetRemaining = valueRedeemed;
640
651
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_SUCCEEDED;
@@ -655,20 +666,20 @@ contract ElementBridge is IDefiBridge {
655
666
656
667
// at this point, the tranche must have been redeemed and we can allocate proportionately to this interaction
657
668
uint256 amountToAllocate = 0 ;
658
- if (trancheAccount.quantityTokensHeld == 0 ) {
669
+ if (trancheTokensHeld == 0 ) {
659
670
// what can we do here?
660
671
// we seem to have 0 total principle tokens so we can't apportion the output asset as it must be the case that each interaction purchased 0
661
672
// we know that the number of deposits against this tranche is > 0 as we check further up this function
662
673
// so we will have to divide the output asset, if there is any, equally
663
- amountToAllocate = trancheAccount.quantityAssetRedeemed / trancheAccount.numDeposits ;
674
+ amountToAllocate = trancheAccount.quantityAssetRedeemed / numDepositsIntoTranche ;
664
675
} else {
665
676
// apportion the output asset based on the interaction's holding of the principle token
666
677
// protects against phantom overflow in the operation of
667
- // amountToAllocate = (trancheAccount.quantityAssetRedeemed * interaction.quantityPT) / trancheAccount.quantityTokensHeld ;
668
- amountToAllocate = FullMath.mulDiv (trancheAccount.quantityAssetRedeemed, interaction.quantityPT, trancheAccount.quantityTokensHeld );
678
+ // amountToAllocate = (trancheAccount.quantityAssetRedeemed * interaction.quantityPT) / trancheTokensHeld ;
679
+ amountToAllocate = FullMath.mulDiv (trancheAccount.quantityAssetRedeemed, interaction.quantityPT, trancheTokensHeld );
669
680
}
670
681
// numDeposits and numFinalised are uint32 types, so easily within range for an int256
671
- int256 numRemainingInteractionsForTranche = int256 (uint256 (trancheAccount.numDeposits )) - int256 (uint256 (trancheAccount.numFinalised));
682
+ int256 numRemainingInteractionsForTranche = int256 (uint256 (numDepositsIntoTranche )) - int256 (uint256 (trancheAccount.numFinalised));
672
683
// the number of remaining interactions should never be less than 1 here, but test for <= 1 to ensure we catch all possibilities
673
684
if (numRemainingInteractionsForTranche <= 1 || amountToAllocate > trancheAccount.quantityAssetRemaining) {
674
685
// if there are no more interactions to finalise after this then allocate all the remaining
@@ -780,7 +791,7 @@ contract ElementBridge is IDefiBridge {
780
791
}
781
792
// retrieve the minimum (oldest) expiry and determine if it is in the past
782
793
uint64 nextExpiry = heap.min ();
783
- if (nextExpiry > block .timestamp ) {
794
+ if (nextExpiry >= block .timestamp ) {
784
795
// oldest expiry is still not expired
785
796
return (false , 0 );
786
797
}
@@ -854,7 +865,7 @@ contract ElementBridge is IDefiBridge {
854
865
ITranche tranche = ITranche (interaction.trancheAddress);
855
866
uint256 speedbump = tranche.speedbump ();
856
867
if (speedbump != 0 ) {
857
- uint256 newExpiry = speedbump + _FORTY_EIGHT_HOURS ;
868
+ uint256 newExpiry = speedbump + FORTY_EIGHT_HOURS ;
858
869
if (newExpiry > block .timestamp ) {
859
870
// a speedbump is in force for this tranche and it is beyond the current time
860
871
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_FAILED;
0 commit comments