Skip to content

Commit

Permalink
Read vars to memory from storage
Browse files Browse the repository at this point in the history
Read variables to memory from storage once to optimize gas cost usage.

For queueStake gas usage was reduced from `142 767` to `113 595`
For cancelQueuedStake gas usage was reduced from `66 174` to `66 013`
  • Loading branch information
nkuba committed Feb 27, 2024
1 parent cafc842 commit 9fee52e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions core/contracts/AcreBitcoinDepositor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,9 @@ contract AcreBitcoinDepositor is AbstractTBTCDepositor, Ownable2Step {
request.queuedAmount = SafeCast.toUint88(amountToStake);

// Increase pending stakes balance.
queuedStakesBalance += request.queuedAmount;
queuedStakesBalance += amountToStake;

emit StakeRequestQueued(depositKey, msg.sender, request.queuedAmount);
emit StakeRequestQueued(depositKey, msg.sender, amountToStake);
}

/// @notice This function should be called for previously queued stake
Expand Down Expand Up @@ -449,20 +449,21 @@ contract AcreBitcoinDepositor is AbstractTBTCDepositor, Ownable2Step {

StakeRequest storage request = stakeRequests[depositKey];

if (request.queuedAmount == 0) revert StakeRequestNotQueued();
uint256 amount = request.queuedAmount;
if (amount == 0) revert StakeRequestNotQueued();

address staker = request.staker;
// Check if caller is the staker.
if (msg.sender != request.staker) revert CallerNotStaker();
if (msg.sender != staker) revert CallerNotStaker();

uint256 amount = request.queuedAmount;
delete (request.queuedAmount);

emit StakeRequestCancelledFromQueue(depositKey, request.staker, amount);
emit StakeRequestCancelledFromQueue(depositKey, staker, amount);

// Decrease pending stakes balance.
queuedStakesBalance -= amount;

tbtcToken.safeTransfer(request.staker, amount);
tbtcToken.safeTransfer(staker, amount);
}

/// @notice Minimum stake amount in satoshi precision.
Expand Down

0 comments on commit 9fee52e

Please sign in to comment.