Skip to content

Commit

Permalink
👷 disable mint/withdraw
Browse files Browse the repository at this point in the history
  • Loading branch information
Flocqst committed Oct 29, 2024
1 parent 8668cfe commit 7b9b12f
Showing 1 changed file with 40 additions and 48 deletions.
88 changes: 40 additions & 48 deletions src/KSXVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ pragma solidity 0.8.25;
import {ERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC4626} from
"@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
import {SafeERC20} from
"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IStakingRewardsV2} from "@token/interfaces/IStakingRewardsV2.sol";

/// @title KSXVault Contract
Expand Down Expand Up @@ -92,38 +94,42 @@ contract KSXVault is ERC4626 {
return shares;
}

/// @notice Mint shares of the vault
/// @dev Overrides the ERC4626 mint function to include reward collection
/// and staking
/// @param shares The amount of shares to mint
/// @param receiver The address to receive the minted shares
/// @return assets The amount of assets deposited
function mint(
uint256 shares,
address receiver
/// @dev Disabled to enforce deposit/redeem pattern. Use deposit() instead.
function mint(uint256, address) public virtual override returns (uint256) {
revert("Disabled");
}

/*//////////////////////////////////////////////////////////////
WITHDRAW/REDEEM FUNCTIONS
//////////////////////////////////////////////////////////////*/

/// @dev Disabled to enforce deposit/redeem pattern. Use redeem() instead.
function withdraw(
uint256,
address,
address
)
public
virtual
override
returns (uint256)
{
uint256 assets = super.mint(shares, receiver);
_collectAndStakeRewards();
return assets;
revert("Disabled");
}

/*//////////////////////////////////////////////////////////////
WITHDRAW/REDEEM FUNCTIONS
//////////////////////////////////////////////////////////////*/

/// @notice Internal withdraw function that handles the actual token
/// movement
/// @dev Overridden to handle unstaking before the ERC4626 withdraw logic
/// @param caller The address that initiated the withdrawal
/// @param receiver The address that will receive the assets
/// @param owner The address that owns the shares
/// @param assets The amount of assets to withdraw
/// @param shares The amount of shares to burn
/// @notice Internal withdrawal mechanism for both withdraw and redeem
/// operations
/// @dev Overrides ERC4626 _withdraw to handle unstaking before token
/// transfers.
/// Order of operations is important:
/// 1. Burn shares (maintaining ratio)
/// 2. Unstake tokens
/// 3. Transfer tokens to receiver
/// @param caller Address that initiated the withdrawal
/// @param receiver Address that will receive the tokens
/// @param owner Address that owns the shares
/// @param assets Amount of underlying tokens to withdraw
/// @param shares Amount of shares to burn
function _withdraw(
address caller,
address receiver,
Expand All @@ -135,33 +141,19 @@ contract KSXVault is ERC4626 {
virtual
override
{
// Unstake the KWENTA tokens first
// First check and burn shares
if (caller != owner) {
_spendAllowance(owner, caller, shares);
}
_burn(owner, shares);

// Then unstake corresponding assets
_unstakeKWENTA(assets);

// Let ERC4626 handle the rest
super._withdraw(caller, receiver, owner, assets, shares);
}
// Finally transfer assets
SafeERC20.safeTransfer(KWENTA, receiver, assets);

/// @notice Redeem shares of the vault
/// @dev Overrides the ERC4626 redeem function to include unstaking of
/// KWENTA
/// @param shares The amount of shares to redeem
/// @param receiver The address to receive the assets
/// @param owner The owner of the shares
/// @return assets The amount of assets withdrawn
function redeem(
uint256 shares,
address receiver,
address owner
)
public
virtual
override
returns (uint256)
{
uint256 assets = previewRedeem(shares);
_unstakeKWENTA(assets);
return super.redeem(shares, receiver, owner);
emit Withdraw(caller, receiver, owner, assets, shares);
}

/*//////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 7b9b12f

Please sign in to comment.