Skip to content

Commit

Permalink
[NES-263] [N3] [Medium] Wrong asset check when async minting (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
ungaro authored Dec 10, 2024
1 parent a78004d commit d6b1e50
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions nest/src/ComponentToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -334,23 +334,22 @@ abstract contract ComponentToken is
}

ComponentTokenStorage storage $ = _getComponentTokenStorage();
assets = convertToAssets(shares);

if ($.asyncDeposit) {
if ($.claimableDepositRequest[controller] < assets) {
revert InsufficientRequestBalance(controller, assets, 1);
// Check shares directly instead of converting to assets
if ($.sharesDepositRequest[controller] < shares) {
revert InsufficientRequestBalance(controller, shares, 1);
}
$.claimableDepositRequest[controller] -= assets;
$.sharesDepositRequest[controller] -= shares;
// Use the pre-calculated assets amount from when deposit was notified
assets = $.claimableDepositRequest[controller];
$.claimableDepositRequest[controller] = 0;
$.sharesDepositRequest[controller] = 0;
} else {
if (!IERC20(asset()).transferFrom(controller, address(this), assets)) {
revert InsufficientBalance(IERC20(asset()), controller, assets);
}
assets = previewMint(shares);
_deposit(msg.sender, receiver, assets, shares);
}

_mint(receiver, shares);

emit Deposit(controller, receiver, assets, shares);
emit Deposit(msg.sender, receiver, assets, shares);
return assets;
}

/// @inheritdoc IComponentToken
Expand Down Expand Up @@ -443,31 +442,29 @@ abstract contract ComponentToken is
address receiver,
address controller
) public virtual override(ERC4626Upgradeable, IERC7540) nonReentrant returns (uint256 shares) {
if (assets == 0) {
if (shares == 0) {
revert ZeroAmount();
}
if (msg.sender != controller) {
revert Unauthorized(msg.sender, controller);
}

ComponentTokenStorage storage $ = _getComponentTokenStorage();
shares = convertToShares(assets);

if ($.asyncRedeem) {
if ($.claimableRedeemRequest[controller] < shares) {
revert InsufficientRequestBalance(controller, shares, 3);
// Use the pre-calculated assets amount from when redeem was notified
if ($.assetsRedeemRequest[controller] < assets) {
revert InsufficientRequestBalance(controller, assets, 3);
}
$.claimableRedeemRequest[controller] -= shares;
$.assetsRedeemRequest[controller] -= assets;
shares = $.claimableRedeemRequest[controller];
$.claimableRedeemRequest[controller] = 0;
$.assetsRedeemRequest[controller] = 0;
} else {
_burn(controller, shares);
shares = previewWithdraw(assets);
_withdraw(msg.sender, receiver, msg.sender, assets, shares);
}

if (!IERC20(asset()).transfer(receiver, assets)) {
revert InsufficientBalance(IERC20(asset()), address(this), assets);
}

emit Withdraw(controller, receiver, controller, assets, shares);
_burn(msg.sender, shares);
emit Withdraw(msg.sender, receiver, msg.sender, assets, shares);
return shares;
}

// Getter View Functions
Expand Down

0 comments on commit d6b1e50

Please sign in to comment.