Skip to content

Commit

Permalink
feat: add mint/redeem flows
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Trenev committed Dec 8, 2023
1 parent a1428d5 commit caeef4a
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions contracts/SafeVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ contract SafeVault is ISafeVault, ERC4626, Ownable, Pausable {
}

/** @dev See {IERC4626-deposit}. */
function deposit(uint256 assets, address receiver) public override returns (uint256) {
function deposit(uint256 assets, address receiver) public override whenNotPaused returns (uint256) {
uint256 maxAssets = maxDeposit(receiver);
if (assets > maxAssets) {
revert ERC4626ExceededMaxDeposit(receiver, assets, maxAssets);
Expand All @@ -83,8 +83,27 @@ contract SafeVault is ISafeVault, ERC4626, Ownable, Pausable {
return shares;
}

/** @dev See {IERC4626-mint}.
*
* As opposed to {deposit}, minting is allowed even if the vault is in a state where the price of a share is zero.
* In this case, the shares will be minted without requiring any assets to be deposited.
*/
function mint(uint256 shares, address receiver) public override whenNotPaused returns (uint256) {
uint256 maxShares = maxMint(receiver);
if (shares > maxShares) {
revert ERC4626ExceededMaxMint(receiver, shares, maxShares);
}

uint256 assets = previewMint(shares);

uint256 assetsAfterTax = assets + ((assets * buyTaxBps) / 10000);
_deposit(_msgSender(), receiver, assetsAfterTax, shares);

return assets;
}

/** @dev See {IERC4626-withdraw}. */
function withdraw(uint256 assets, address receiver, address owner) public override returns (uint256) {
function withdraw(uint256 assets, address receiver, address owner) public override whenNotPaused returns (uint256) {
uint256 maxAssets = maxWithdraw(owner);
if (assets > maxAssets) {
revert ERC4626ExceededMaxWithdraw(owner, assets, maxAssets);
Expand All @@ -97,4 +116,19 @@ contract SafeVault is ISafeVault, ERC4626, Ownable, Pausable {

return shares;
}

/** @dev See {IERC4626-redeem}. */
function redeem(uint256 shares, address receiver, address owner) public override whenNotPaused returns (uint256) {
uint256 maxShares = maxRedeem(owner);
if (shares > maxShares) {
revert ERC4626ExceededMaxRedeem(owner, shares, maxShares);
}

uint256 assets = previewRedeem(shares);

uint256 assetsAfterTax = assets - ((assets * sellTaxBps) / 10000);
_withdraw(_msgSender(), receiver, owner, assetsAfterTax, shares);

return assets;
}
}

0 comments on commit caeef4a

Please sign in to comment.