Skip to content

Commit ffab86d

Browse files
authored
Merge pull request #70 from AztecProtocol/pw/element-assetb-checks
Pw/element assetb checks
2 parents b3444d5 + 8bed027 commit ffab86d

File tree

3 files changed

+344
-31
lines changed

3 files changed

+344
-31
lines changed

src/aztec/RollupProcessor.sol

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ contract RollupProcessor is DSTest {
2424
uint256 totalInputValue;
2525
uint256 interactionNonce;
2626
uint256 auxInputData; // (auxData)
27+
uint256 outputValueA;
28+
uint256 outputValueB;
29+
bool finalised;
2730
}
2831

2932
uint256 private constant NUMBER_OF_BRIDGE_CALLS = 32;
@@ -73,7 +76,12 @@ contract RollupProcessor is DSTest {
7376
limit = limit == 0 ? 200000 : limit;
7477
}
7578

76-
mapping(uint256 => DefiInteraction) private defiInteractions;
79+
function getDefiResult(uint256 nonce) public returns (bool finalised, uint256 outputValueA) {
80+
finalised = defiInteractions[nonce].finalised;
81+
outputValueA = defiInteractions[nonce].outputValueA;
82+
}
83+
84+
mapping(uint256 => DefiInteraction) public defiInteractions;
7785

7886
constructor(address _bridgeProxyAddress) {
7987
bridgeProxy = DefiBridgeProxy(_bridgeProxyAddress);
@@ -178,6 +186,9 @@ contract RollupProcessor is DSTest {
178186
outputValueB,
179187
true
180188
);
189+
interaction.finalised = true;
190+
interaction.outputValueA = outputValueA;
191+
interaction.outputValueB = outputValueB;
181192
}
182193

183194
struct ConvertArgs {
@@ -209,7 +220,10 @@ contract RollupProcessor is DSTest {
209220
convertArgs.outputAssetB,
210221
convertArgs.totalInputValue,
211222
convertArgs.interactionNonce,
212-
convertArgs.auxInputData
223+
convertArgs.auxInputData,
224+
0,
225+
0,
226+
false
213227
);
214228
uint256 gas = bridgeGasLimits[convertArgs.bridgeAddress] > 0 ? bridgeGasLimits[convertArgs.bridgeAddress]: uint256(150000000);
215229

src/bridges/element/ElementBridge.sol

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ contract ElementBridge is IDefiBridge {
3535
error INVALID_CALLER();
3636
error ASSET_IDS_NOT_EQUAL();
3737
error ASSET_NOT_ERC20();
38+
error INPUT_ASSETB_NOT_UNUSED();
39+
error OUTPUT_ASSETB_NOT_UNUSED();
3840
error INTERACTION_ALREADY_EXISTS();
3941
error POOL_NOT_FOUND();
4042
error UNKNOWN_NONCE();
@@ -146,12 +148,12 @@ contract ElementBridge is IDefiBridge {
146148
mapping(uint64 => uint256[]) private expiryToNonce;
147149

148150
// 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;
150152

151153
uint256 internal constant MAX_UINT = type(uint256).max;
152154

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;
155157
uint256 internal constant MIN_GAS_FOR_FAILED_INTERACTION = 20000;
156158
uint256 internal constant MIN_GAS_FOR_EXPIRY_REMOVAL = 25000;
157159

@@ -421,9 +423,9 @@ contract ElementBridge is IDefiBridge {
421423
*/
422424
function convert(
423425
AztecTypes.AztecAsset calldata inputAssetA,
424-
AztecTypes.AztecAsset calldata,
426+
AztecTypes.AztecAsset calldata inputAssetB,
425427
AztecTypes.AztecAsset calldata outputAssetA,
426-
AztecTypes.AztecAsset calldata,
428+
AztecTypes.AztecAsset calldata outputAssetB,
427429
uint256 totalInputValue,
428430
uint256 interactionNonce,
429431
uint64 auxData,
@@ -450,6 +452,12 @@ contract ElementBridge is IDefiBridge {
450452
if (inputAssetA.assetType != AztecTypes.AztecAssetType.ERC20) {
451453
revert ASSET_NOT_ERC20();
452454
}
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+
}
453461
if (interactions[interactionNonce].expiry != 0) {
454462
revert INTERACTION_ALREADY_EXISTS();
455463
}
@@ -614,15 +622,18 @@ contract ElementBridge is IDefiBridge {
614622
if (interaction.expiry == 0) {
615623
revert UNKNOWN_NONCE();
616624
}
617-
if (interaction.expiry > block.timestamp) {
625+
if (interaction.expiry >= block.timestamp) {
618626
revert BRIDGE_NOT_READY();
619627
}
620628
if (interaction.finalised) {
621629
revert ALREADY_FINALISED();
622630
}
623631

624632
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) {
626637
// shouldn't be possible, this means we have had no deposits against this tranche
627638
setInteractionAsFailure(interaction, interactionNonce, 'NO_DEPOSITS_2', 0);
628639
popInteractionFromNonceMapping(interaction, interactionNonce);
@@ -634,7 +645,7 @@ contract ElementBridge is IDefiBridge {
634645
// tranche not redeemed, we need to withdraw the principal
635646
// convert the tokens back to underlying using the tranche
636647
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) {
638649
trancheAccount.quantityAssetRedeemed = valueRedeemed;
639650
trancheAccount.quantityAssetRemaining = valueRedeemed;
640651
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_SUCCEEDED;
@@ -655,20 +666,20 @@ contract ElementBridge is IDefiBridge {
655666

656667
// at this point, the tranche must have been redeemed and we can allocate proportionately to this interaction
657668
uint256 amountToAllocate = 0;
658-
if (trancheAccount.quantityTokensHeld == 0) {
669+
if (trancheTokensHeld == 0) {
659670
// what can we do here?
660671
// 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
661672
// we know that the number of deposits against this tranche is > 0 as we check further up this function
662673
// 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;
664675
} else {
665676
// apportion the output asset based on the interaction's holding of the principle token
666677
// 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);
669680
}
670681
// 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));
672683
// the number of remaining interactions should never be less than 1 here, but test for <= 1 to ensure we catch all possibilities
673684
if (numRemainingInteractionsForTranche <= 1 || amountToAllocate > trancheAccount.quantityAssetRemaining) {
674685
// if there are no more interactions to finalise after this then allocate all the remaining
@@ -780,7 +791,7 @@ contract ElementBridge is IDefiBridge {
780791
}
781792
// retrieve the minimum (oldest) expiry and determine if it is in the past
782793
uint64 nextExpiry = heap.min();
783-
if (nextExpiry > block.timestamp) {
794+
if (nextExpiry >= block.timestamp) {
784795
// oldest expiry is still not expired
785796
return (false, 0);
786797
}
@@ -854,7 +865,7 @@ contract ElementBridge is IDefiBridge {
854865
ITranche tranche = ITranche(interaction.trancheAddress);
855866
uint256 speedbump = tranche.speedbump();
856867
if (speedbump != 0) {
857-
uint256 newExpiry = speedbump + _FORTY_EIGHT_HOURS;
868+
uint256 newExpiry = speedbump + FORTY_EIGHT_HOURS;
858869
if (newExpiry > block.timestamp) {
859870
// a speedbump is in force for this tranche and it is beyond the current time
860871
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_FAILED;

0 commit comments

Comments
 (0)