Skip to content

Commit fa21bc6

Browse files
committed
feat: added withdraw USDC from vault scripts
1 parent 589b7d7 commit fa21bc6

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

README.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@
1515
1. Registry.sol
1616

1717
```
18-
forge script script/deploy/Registry.s.sol:DeployRegistry --rpc-url <PRC_URL> --broadcast
18+
forge script script/deploy/Registry.s.sol:DeployRegistry --rpc-url <PRC_URL> --broadcast
1919
```
2020

2121
2. Exchange.sol
2222

2323
```
24-
forge script script/deploy/Exchange.s.sol:DeployExchange --rpc-url <PRC_URL> --broadcast
24+
forge script script/deploy/Exchange.s.sol:DeployExchange --rpc-url <PRC_URL> --broadcast
2525
```
2626

2727
3. Vault.sol
2828

2929
```
30-
forge script script/deploy/Vault.s.sol:DeployVault --rpc-url <PRC_URL> --broadcast
30+
forge script script/deploy/Vault.s.sol:DeployVault --rpc-url <PRC_URL> --broadcast
3131
```
3232

3333
4. XSGD.sol
@@ -61,3 +61,23 @@ forge script script/actions/removeMerchant.s.sol:RemoveMerchant --rpc-url <RPC_U
6161
```
6262
forge script script/actions/addMerchant.s.sol:AddMerchant --rpc-url <RPC_URL> --broadcast
6363
```
64+
65+
### Withdraw USDC From Vault
66+
67+
#### Check Shares
68+
69+
```
70+
forge script script/actions/withdrawUSDCFromVault.s.sol:WithdrawUSDCFromVault --sig "checkShares(address,address)" <EXCHANGE_ADDRESS> <MERCHANT_ADDRESS> --rpc-url <RPC_URL>
71+
```
72+
73+
#### Convert Shares to Assets
74+
75+
```
76+
forge script script/actions/withdrawUSDCFromVault.s.sol:WithdrawUSDCFromVault --sig "convertSharesToAssets(address,uint256)" <EXCHANGE_ADDRESS> <SHARES> --rpc-url <RPC_URL>
77+
```
78+
79+
#### Withdraw USDC From Vault
80+
81+
```
82+
forge script script/actions/withdrawUSDCFromVault.s.sol:WithdrawUSDCFromVault --sig "run(address,uint256)" <EXCHANGE_ADDRESS> <SHARES> --rpc-url <RPC_URL> --broadcast
83+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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

Comments
 (0)