Skip to content

Commit a131680

Browse files
authored
Merge pull request #60 from AztecProtocol/pw/gas-logs
Emit gas consumption in logs for Element bridge
2 parents 745c19d + 7dbe55f commit a131680

File tree

3 files changed

+61
-33
lines changed

3 files changed

+61
-33
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"rootDir": "./src"
2828
},
2929
"name": "@aztec/bridge-clients",
30-
"version": "0.1.33",
30+
"version": "0.1.34",
3131
"description": "This repo contains the solidity files and typescript helper class for all of the Aztec Connect Bridge Contracts",
3232
"repository": "[email protected]:AztecProtocol/aztec-connect-bridges.git",
3333
"license": "MIT",

src/bridges/element/ElementBridge.sol

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,10 @@ contract ElementBridge is IDefiBridge {
156156
uint256 internal constant MIN_GAS_FOR_EXPIRY_REMOVAL = 25000;
157157

158158
// 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);
160160

161161
// 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);
163163

164164
// event emitted on wvery newly configured pool
165165
event LogPoolAdded(address poolAddress, address wrappedPositionAddress, uint64 expiry);
@@ -400,6 +400,13 @@ contract ElementBridge is IDefiBridge {
400400
hashValue = uint256(keccak256(abi.encodePacked(asset, uint256(expiry))));
401401
}
402402

403+
struct ConvertArgs {
404+
address inputAssetAddress;
405+
uint256 totalInputValue;
406+
uint256 interactionNonce;
407+
uint64 auxData;
408+
}
409+
403410
/**
404411
* @dev Function to add a new interaction to the bridge
405412
* Converts the amount of input asset given to the market determined amount of tranche asset
@@ -431,6 +438,8 @@ contract ElementBridge is IDefiBridge {
431438
bool isAsync
432439
)
433440
{
441+
int64 gasAtStart = int64(int256(gasleft()));
442+
int64 gasUsed = 0;
434443
// ### INITIALIZATION AND SANITY CHECKS
435444
if (msg.sender != rollupProcessor) {
436445
revert INVALID_CALLER();
@@ -449,36 +458,47 @@ contract ElementBridge is IDefiBridge {
449458
isAsync = true;
450459
outputValueA = 0;
451460
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+
452470
// 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)];
454472
if (pool.trancheAddress == address(0)) {
455473
revert POOL_NOT_FOUND();
456474
}
457475
ITranche tranche = ITranche(pool.trancheAddress);
458-
if (block.timestamp >= tranche.unlockTimestamp()) {
476+
uint64 trancheExpiry = uint64(tranche.unlockTimestamp());
477+
if (block.timestamp >= trancheExpiry) {
459478
revert TRANCHE_ALREADY_EXPIRED();
460479
}
461-
uint64 trancheExpiry = uint64(tranche.unlockTimestamp());
480+
462481
// approve the transfer of tokens to the balancer address
463-
ERC20(inputAssetA.erc20Address).approve(balancerAddress, totalInputValue);
482+
ERC20(convertArgs.inputAssetAddress).approve(balancerAddress, convertArgs.totalInputValue);
464483
// execute the swap on balancer
465-
uint256 principalTokensAmount = exchangeAssetForTrancheTokens(inputAssetA.erc20Address, pool, totalInputValue);
484+
uint256 principalTokensAmount = exchangeAssetForTrancheTokens(convertArgs.inputAssetAddress, pool, convertArgs.totalInputValue);
466485
// 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];
468487
newInteraction.expiry = trancheExpiry;
469488
newInteraction.failed = false;
470489
newInteraction.finalised = false;
471490
newInteraction.quantityPT = principalTokensAmount;
472491
newInteraction.trancheAddress = pool.trancheAddress;
473492
// add the nonce and expiry to our expiry heap
474-
addNonceAndExpiryToNonceMapping(interactionNonce, trancheExpiry);
493+
addNonceAndExpiryToNonceMapping(convertArgs.interactionNonce, trancheExpiry);
475494
// increase our tranche account deposits and holdings
476495
// other members are left as their initial values (all zeros)
477496
TrancheAccount storage trancheAccount = trancheAccounts[newInteraction.trancheAddress];
478497
trancheAccount.numDeposits++;
479498
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);
482502
// we need to get here with MIN_GAS_FOR_FUNCTION_COMPLETION gas to exit.
483503
}
484504

@@ -584,6 +604,8 @@ contract ElementBridge is IDefiBridge {
584604
bool interactionCompleted
585605
)
586606
{
607+
int64 gasAtStart = int64(int256(gasleft()));
608+
int64 gasUsed = 0;
587609
if (msg.sender != rollupProcessor) {
588610
revert INVALID_CALLER();
589611
}
@@ -602,7 +624,7 @@ contract ElementBridge is IDefiBridge {
602624
TrancheAccount storage trancheAccount = trancheAccounts[interaction.trancheAddress];
603625
if (trancheAccount.numDeposits == 0) {
604626
// 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);
606628
popInteractionFromNonceMapping(interaction, interactionNonce);
607629
return (0, 0, false);
608630
}
@@ -617,12 +639,14 @@ contract ElementBridge is IDefiBridge {
617639
trancheAccount.quantityAssetRemaining = valueRedeemed;
618640
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_SUCCEEDED;
619641
} catch Error(string memory errorMessage) {
620-
setInteractionAsFailure(interaction, interactionNonce, errorMessage);
642+
unchecked { gasUsed = gasAtStart - int64(int256(gasleft())); }
643+
setInteractionAsFailure(interaction, interactionNonce, errorMessage, gasUsed);
621644
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_FAILED;
622645
popInteractionFromNonceMapping(interaction, interactionNonce);
623646
return (0, 0, false);
624647
} 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);
626650
trancheAccount.redemptionStatus = TrancheRedemptionStatus.REDEMPTION_FAILED;
627651
popInteractionFromNonceMapping(interaction, interactionNonce);
628652
return (0, 0, false);
@@ -661,7 +685,8 @@ contract ElementBridge is IDefiBridge {
661685
outputValueA = amountToAllocate;
662686
outputValueB = 0;
663687
interactionCompleted = true;
664-
emit LogFinalise(interactionNonce, interactionCompleted, '');
688+
unchecked { gasUsed = gasAtStart - int64(int256(gasleft())); }
689+
emit LogFinalise(interactionNonce, interactionCompleted, '', gasUsed);
665690
}
666691

667692
/**
@@ -673,10 +698,11 @@ contract ElementBridge is IDefiBridge {
673698
function setInteractionAsFailure(
674699
Interaction storage interaction,
675700
uint256 interactionNonce,
676-
string memory message
701+
string memory message,
702+
int64 gasUsed
677703
) internal {
678704
interaction.failed = true;
679-
emit LogFinalise(interactionNonce, false, message);
705+
emit LogFinalise(interactionNonce, false, message, gasUsed);
680706
}
681707

682708
/**
@@ -780,7 +806,7 @@ contract ElementBridge is IDefiBridge {
780806
(bool canBeFinalised, string memory message) = interactionCanBeFinalised(interaction);
781807
if (!canBeFinalised) {
782808
// can't be finalised, add to failures and pop from nonces
783-
setInteractionAsFailure(interaction, nextNonce, message);
809+
setInteractionAsFailure(interaction, nextNonce, message, 0);
784810
nonces.pop();
785811
continue;
786812
}

src/test/element/Element.t.sol

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,11 @@ contract ElementTest is DSTest {
9999
AztecTypes.AztecAsset emptyAsset;
100100
uint256 private numTranches = 0;
101101

102-
event LogConvert(uint256 indexed nonce, uint256 totalInputValue);
102+
int64 constant daiConvertGas = 238860;
103103

104-
event LogFinalise(uint256 indexed nonce, bool success, string message);
104+
event LogConvert(uint256 indexed nonce, uint256 totalInputValue, int64 gasUsed);
105+
106+
event LogFinalise(uint256 indexed nonce, bool success, string message, int64 gasUsed);
105107

106108
event LogPoolAdded(address poolAddress, address wrappedPositionAddress, uint64 expiry);
107109

@@ -688,7 +690,7 @@ contract ElementTest is DSTest {
688690
_setTokenBalance('DAI', address(elementBridge), interactionConfig.depositAmount);
689691
uint256 balancerBefore = tokens['DAI'].balanceOf(address(balancer));
690692
vm.expectEmit(false, false, false, true);
691-
emit LogConvert(interactionConfig.nonce, interactionConfig.depositAmount);
693+
emit LogConvert(interactionConfig.nonce, interactionConfig.depositAmount, daiConvertGas);
692694
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callElementConvert('DAI', interactionConfig);
693695
assertEq(isAsync, true);
694696
assertEq(outputValueA, 0);
@@ -717,7 +719,7 @@ contract ElementTest is DSTest {
717719
_setTokenBalance('DAI', address(elementBridge), interactionConfig.depositAmount);
718720
uint256 balancerBefore = tokens['DAI'].balanceOf(address(balancer));
719721
vm.expectEmit(false, false, false, true);
720-
emit LogConvert(interactionConfig.nonce, interactionConfig.depositAmount);
722+
emit LogConvert(interactionConfig.nonce, interactionConfig.depositAmount, daiConvertGas);
721723
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callElementConvert('DAI', interactionConfig);
722724
assertEq(isAsync, true);
723725
assertEq(outputValueA, 0);
@@ -751,7 +753,7 @@ contract ElementTest is DSTest {
751753
_setTokenBalance('DAI', address(elementBridge), interactionConfig.depositAmount * 2);
752754
uint256 balancerBefore = tokens['DAI'].balanceOf(address(balancer));
753755
vm.expectEmit(false, false, false, true);
754-
emit LogConvert(interactionConfig.nonce, interactionConfig.depositAmount);
756+
emit LogConvert(interactionConfig.nonce, interactionConfig.depositAmount, daiConvertGas);
755757
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callElementConvert('DAI', interactionConfig);
756758
assertEq(isAsync, true);
757759
assertEq(outputValueA, 0);
@@ -1139,7 +1141,7 @@ contract ElementTest is DSTest {
11391141
// we expect 5 Finalise events to be emitted but we can't test the data values
11401142
for (uint256 i = 0; i < numUsdcInteractions; i++) {
11411143
vm.expectEmit(false, false, false, false);
1142-
emit LogFinalise(1 + i, true, '');
1144+
emit LogFinalise(1 + i, true, '', 0);
11431145
}
11441146
{
11451147
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callRollupConvert(asset, daiInteraction);
@@ -1203,7 +1205,7 @@ contract ElementTest is DSTest {
12031205
// we expect 5 Finalise events to be emitted but we can't test the data values
12041206
for (uint256 i = 0; i < numEurInteractions; i++) {
12051207
vm.expectEmit(false, false, false, false);
1206-
emit LogFinalise(1 + i, true, '');
1208+
emit LogFinalise(1 + i, true, '', 0);
12071209
}
12081210
{
12091211
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callRollupConvert(asset, daiInteraction);
@@ -1358,7 +1360,7 @@ contract ElementTest is DSTest {
13581360
// we expect 5 Finalise events to be emitted but we can't test the data values
13591361
for (uint256 i = 0; i < numEurInteractions; i++) {
13601362
vm.expectEmit(false, false, false, false);
1361-
emit LogFinalise(1 + i, true, '');
1363+
emit LogFinalise(1 + i, true, '', 0);
13621364
}
13631365
{
13641366
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callRollupConvert(asset, daiInteraction);
@@ -1439,7 +1441,7 @@ contract ElementTest is DSTest {
14391441
_increaseTokenBalance(asset, address(rollupProcessor), daiDepositAmount);
14401442
// we expect 1 Finalise events to be emitted but we can't test the data values
14411443
vm.expectEmit(false, false, false, true);
1442-
emit LogFinalise(5, true, '');
1444+
emit LogFinalise(5, true, '', 225337);
14431445
{
14441446
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callRollupConvert(asset, daiInteraction);
14451447
assertEq(isAsync, true);
@@ -1503,8 +1505,8 @@ contract ElementTest is DSTest {
15031505
// we expect 2 Finalise events to be emitted but we can't test the data values
15041506
uint numExpectedFinalisedInteractions = 2;
15051507
vm.expectEmit(false, false, false, false);
1506-
emit LogFinalise(5, true, '');
1507-
emit LogFinalise(4, true, '');
1508+
emit LogFinalise(5, true, '', 0);
1509+
emit LogFinalise(4, true, '', 0);
15081510
{
15091511
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callRollupConvert(asset, daiInteraction);
15101512
assertEq(isAsync, true);
@@ -1568,9 +1570,9 @@ contract ElementTest is DSTest {
15681570
// we expect 3 Finalise events to be emitted but we can't test the data values
15691571
uint numExpectedFinalisedInteractions = 3;
15701572
vm.expectEmit(false, false, false, false);
1571-
emit LogFinalise(5, true, '');
1572-
emit LogFinalise(4, true, '');
1573-
emit LogFinalise(3, true, '');
1573+
emit LogFinalise(5, true, '', 0);
1574+
emit LogFinalise(4, true, '', 0);
1575+
emit LogFinalise(3, true, '', 0);
15741576
{
15751577
(uint256 outputValueA, uint256 outputValueB, bool isAsync) = _callRollupConvert(asset, daiInteraction);
15761578
assertEq(isAsync, true);

0 commit comments

Comments
 (0)