Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MezoAllocator Withdrawals Improvements #717

Merged
merged 11 commits into from
Sep 3, 2024
59 changes: 45 additions & 14 deletions solidity/contracts/MezoAllocator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,15 @@ contract MezoAllocator is IDispatcher, Ownable2StepUpgradeable {
uint256 newDepositAmount
);
/// @notice Emitted when tBTC is withdrawn from MezoPortal.
event DepositWithdrawn(uint256 indexed depositId, uint256 amount);
/// If MezoAllocator has a positive balance part of the requested amount
/// is withdrawn from MezoAllocator and the rest from MezoPortal.
event WithdrawFromMezoPortal(
uint256 indexed depositId,
uint256 requestedAmount,
uint256 amountWithdrawnFromPortal
);
/// @notice Emitted when tBTC is withdrawn from MezoAllocator.
event WithdrawFromMezoAllocator(uint256 amount);
/// @notice Emitted when the maintainer address is updated.
event MaintainerAdded(address indexed maintainer);
/// @notice Emitted when the maintainer address is updated.
Expand All @@ -142,6 +150,12 @@ contract MezoAllocator is IDispatcher, Ownable2StepUpgradeable {
error MaintainerNotRegistered();
/// @notice Reverts if the maintainer has been already registered.
error MaintainerAlreadyRegistered();
/// @notice Reverts if the requested amount to withdraw exceeds the amount
/// deposited in the Mezo Portal.
error WithdrawalAmountExceedsDepositBalance(
uint256 requestedAmount,
uint256 depositAmount
);

modifier onlyMaintainer() {
if (!isMaintainer[msg.sender]) {
Expand Down Expand Up @@ -225,20 +239,34 @@ contract MezoAllocator is IDispatcher, Ownable2StepUpgradeable {
function withdraw(uint256 amount) external {
if (msg.sender != address(stbtc)) revert CallerNotStbtc();

emit DepositWithdrawn(depositId, amount);
uint256 unallocatedBalance = tbtc.balanceOf(address(this));

if (amount > unallocatedBalance) {
uint256 amountToWithdraw = amount - unallocatedBalance;

emit WithdrawFromMezoPortal(depositId, amount, amountToWithdraw);

if (amountToWithdraw < depositBalance) {
dimpar marked this conversation as resolved.
Show resolved Hide resolved
mezoPortal.withdrawPartially(
address(tbtc),
depositId,
uint96(amountToWithdraw)
);
} else if (amountToWithdraw == depositBalance) {
mezoPortal.withdraw(address(tbtc), depositId);
} else {
revert WithdrawalAmountExceedsDepositBalance(
amountToWithdraw,
depositBalance
);
}

if (amount < depositBalance) {
mezoPortal.withdrawPartially(
address(tbtc),
depositId,
uint96(amount)
);
// slither-disable-next-line reentrancy-no-eth
depositBalance -= uint96(amountToWithdraw);
} else {
mezoPortal.withdraw(address(tbtc), depositId);
emit WithdrawFromMezoAllocator(amount);
}

// slither-disable-next-line reentrancy-no-eth
depositBalance -= uint96(amount);
tbtc.safeTransfer(address(stbtc), amount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we emit some event informing about withdrawing from the allocator? DepositWithdrawn is only about withdrawing from the Portal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we maybe change DepositWithdrawn(..) to DepositWithdrawn(depositId, requestedAmount, mezoWithdrawnAmount)? The diff between requestedAmount-mezoWithdrawnAmount can always be calculated off-chain and this is the amount that was withdrawn from Allocator. Might be cheaper than emitting an additional event.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Expand All @@ -250,9 +278,12 @@ contract MezoAllocator is IDispatcher, Ownable2StepUpgradeable {
.getDeposit(address(this), address(tbtc), depositId)
.balance;

emit DepositReleased(depositId, amount);
depositBalance = 0;
mezoPortal.withdraw(address(tbtc), depositId);
if (amount > 0) {
emit DepositReleased(depositId, amount);
depositBalance = 0;
mezoPortal.withdraw(address(tbtc), depositId);
}

tbtc.safeTransfer(address(stbtc), tbtc.balanceOf(address(this)));
}

Expand Down
Loading
Loading