Skip to content

Commit

Permalink
safetransfer changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ungaro committed Dec 10, 2024
1 parent ed4aeea commit 452500b
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions nest/src/ComponentToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ abstract contract ComponentToken is

SafeERC20.safeTransferFrom(IERC20(asset()), owner, address(this), assets);
$.pendingDepositRequest[controller] += assets;

emit DepositRequest(controller, owner, REQUEST_ID, owner, assets);
return REQUEST_ID;
}
Expand Down Expand Up @@ -332,21 +333,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 {
SafeERC20.safeTransferFrom(IERC20(asset()), controller, address(this), 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 @@ -437,29 +439,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);
}

SafeERC20.safeTransfer(IERC20(asset()), receiver, 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 Expand Up @@ -518,7 +520,7 @@ abstract contract ComponentToken is
if (_getComponentTokenStorage().asyncDeposit) {
revert Unimplemented();
}
assets = super.previewDeposit(shares);
assets = convertToAssets(shares);
}

/**
Expand All @@ -545,12 +547,12 @@ abstract contract ComponentToken is
if (_getComponentTokenStorage().asyncRedeem) {
revert Unimplemented();
}
shares = super.previewWithdraw(assets);
shares = convertToShares(assets);
}

/// @inheritdoc IERC7540
function setOperator(address, bool) public pure returns (bool) {
revert Unimplemented();
}

}
}

0 comments on commit 452500b

Please sign in to comment.