|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +import {Script} from "forge-std/Script.sol"; |
| 5 | +import {Exchange} from "../../src/Exchange.sol"; |
| 6 | +import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol"; |
| 7 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 8 | + |
| 9 | +contract WithdrawUSDCFromVault is Script { |
| 10 | + function run(address exchangeAddress, uint256 sharesToWithdraw) external { |
| 11 | + // Ensure the private key is set in the environment |
| 12 | + uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); |
| 13 | + |
| 14 | + // Start broadcasting transactions |
| 15 | + vm.startBroadcast(deployerPrivateKey); |
| 16 | + |
| 17 | + // Get the Exchange contract instance |
| 18 | + Exchange exchange = Exchange(exchangeAddress); |
| 19 | + |
| 20 | + // Get the vault address |
| 21 | + IERC4626 vault = exchange.vault(); |
| 22 | + |
| 23 | + // First approve the Exchange contract to spend your vault shares |
| 24 | + IERC20(address(vault)).approve(exchangeAddress, sharesToWithdraw); |
| 25 | + |
| 26 | + // Then call withdrawToWallet function |
| 27 | + exchange.withdrawToWallet(sharesToWithdraw); |
| 28 | + |
| 29 | + vm.stopBroadcast(); |
| 30 | + } |
| 31 | + |
| 32 | + // Helper function to check how many shares you have |
| 33 | + function checkShares(address exchangeAddress, address walletAddress) external view returns (uint256) { |
| 34 | + Exchange exchange = Exchange(exchangeAddress); |
| 35 | + IERC4626 vault = exchange.vault(); |
| 36 | + return vault.balanceOf(walletAddress); |
| 37 | + } |
| 38 | + |
| 39 | + // Helper function to convert shares to assets (USDC) |
| 40 | + function convertSharesToAssets(address exchangeAddress, uint256 shares) external view returns (uint256) { |
| 41 | + Exchange exchange = Exchange(exchangeAddress); |
| 42 | + IERC4626 vault = exchange.vault(); |
| 43 | + return vault.convertToAssets(shares); |
| 44 | + } |
| 45 | +} |
0 commit comments