Skip to content

Commit f5bad23

Browse files
committed
feat: integrate xSGD token into exchange contract and update related tests
1 parent 79fbcc4 commit f5bad23

File tree

4 files changed

+257
-45
lines changed

4 files changed

+257
-45
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
## Contracts
77

88
1. Registry.sol: [0x2b4836d81370e37030727e4dcbd9cc5a772cf43a](https://sepolia.basescan.org/address/0x2b4836d81370e37030727e4dcbd9cc5a772cf43a)
9-
2. Exchange.sol: [0xd9004Edc4bdEB308C4A40fdCbE320bbE5DF4AF77](https://sepolia.basescan.org/address/0xd9004edc4bdeb308c4a40fdcbe320bbe5df4af77)
10-
3. Vault.sol: [0xd580248163CDD5AE3225A700E9f4e7CD525b27b0](https://sepolia.basescan.org/address/0xd580248163cdd5ae3225a700e9f4e7cd525b27b0)
11-
4. XSGD.sol [0xd7260d7063fE5A62A90E6A8DD5A39Ab27A05986B](https://sepolia.basescan.org/token/0xd7260d7063fe5a62a90e6a8dd5a39ab27a05986b)
9+
2. Exchange.sol (V1): [0xd9004Edc4bdEB308C4A40fdCbE320bbE5DF4AF77](https://sepolia.basescan.org/address/0xd9004edc4bdeb308c4a40fdcbe320bbe5df4af77)
10+
3. Exchange.sol (V2): [0x92F5D70ffBE0988DEcD5c1E7A6cb8A048a3Fe75D](https://sepolia.basescan.org/address/0x92F5D70ffBE0988DEcD5c1E7A6cb8A048a3Fe75D)
11+
4. Vault.sol: [0xd580248163CDD5AE3225A700E9f4e7CD525b27b0](https://sepolia.basescan.org/address/0xd580248163cdd5ae3225a700e9f4e7cd525b27b0)
12+
5. XSGD.sol: [0xd7260d7063fE5A62A90E6A8DD5A39Ab27A05986B](https://sepolia.basescan.org/token/0xd7260d7063fe5a62a90e6a8dd5a39ab27a05986b)
1213

1314
## Deployment
1415

script/deploy/Exchange.s.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ contract DeployExchange is Script {
1111

1212
address registryAddress = 0x2b4836d81370e37030727E4DCbd9cC5a772cf43A;
1313
address usdcAddress = 0x036CbD53842c5426634e7929541eC2318f3dCF7e;
14+
address xsgdAddress = 0xd7260d7063fE5A62A90E6A8DD5A39Ab27A05986B;
1415
address vaultAddress = 0xd580248163CDD5AE3225A700E9f4e7CD525b27b0;
1516

16-
Exchange exchange = new Exchange(registryAddress, usdcAddress, vaultAddress);
17+
Exchange exchange = new Exchange(registryAddress, usdcAddress, xsgdAddress, vaultAddress);
1718

1819
console.log("Exchange deployed at:", address(exchange));
1920

src/Exchange.sol

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ contract Exchange is ReentrancyGuard, Ownable {
1919

2020
Registry public immutable registry;
2121
IERC20 public immutable usdcToken;
22+
IERC20 public immutable xsgdToken;
2223
IERC4626 public immutable vault;
2324

2425
uint256 public fee = 100; // 100 basis points fee (1%)
@@ -34,12 +35,16 @@ contract Exchange is ReentrancyGuard, Ownable {
3435
event VaultDeposit(address indexed merchant, uint256 assets, uint256 shares);
3536
event VaultWithdraw(address indexed merchant, uint256 assets, uint256 shares);
3637

37-
constructor(address _registryAddress, address _usdcAddress, address _vaultAddress) Ownable(msg.sender) {
38+
constructor(address _registryAddress, address _usdcAddress, address _xsgdAddress, address _vaultAddress)
39+
Ownable(msg.sender)
40+
{
3841
require(_registryAddress != address(0), "Invalid registry address");
3942
require(_usdcAddress != address(0), "Invalid USDC address");
43+
require(_xsgdAddress != address(0), "Invalid xSGD address");
4044
require(_vaultAddress != address(0), "Invalid vault address");
4145
registry = Registry(_registryAddress);
4246
usdcToken = IERC20(_usdcAddress);
47+
xsgdToken = IERC20(_xsgdAddress);
4348
feeCollector = address(this);
4449
vault = IERC4626(_vaultAddress);
4550
lastUpdateTime = block.timestamp;
@@ -55,7 +60,7 @@ contract Exchange is ReentrancyGuard, Ownable {
5560
* @param _uen Merchant's UEN.
5661
* @param _amount Amount of USDC to transfer to merchant.
5762
*/
58-
function transferToMerchant(string memory _uen, uint256 _amount) external nonReentrant {
63+
function transferUsdcToMerchant(string memory _uen, uint256 _amount) external nonReentrant {
5964
require(_amount > 0, "Amount must be greater than zero");
6065

6166
address merchantWalletAddress = registry.getMerchantByUEN(_uen).wallet_address;
@@ -72,6 +77,23 @@ contract Exchange is ReentrancyGuard, Ownable {
7277
emit Transfer(msg.sender, merchantWalletAddress, merchantAmount, _uen);
7378
}
7479

80+
function transferXsgdToMerchant(string memory _uen, uint256 _amount) external nonReentrant {
81+
require(_amount > 0, "Amount must be greater than zero");
82+
83+
address merchantWalletAddress = registry.getMerchantByUEN(_uen).wallet_address;
84+
require(merchantWalletAddress != address(0), "Invalid merchant wallet address");
85+
86+
uint256 feeAmount = (_amount * fee) / 10000;
87+
uint256 merchantAmount = _amount - feeAmount;
88+
89+
xsgdToken.safeTransferFrom(msg.sender, merchantWalletAddress, merchantAmount);
90+
if (feeAmount > 0) {
91+
xsgdToken.safeTransferFrom(msg.sender, feeCollector, feeAmount);
92+
}
93+
94+
emit Transfer(msg.sender, merchantWalletAddress, merchantAmount, _uen);
95+
}
96+
7597
/**
7698
* @notice Transfer USDC to vault.
7799
* @param _uen Merchant's UEN.
@@ -154,11 +176,11 @@ contract Exchange is ReentrancyGuard, Ownable {
154176
}
155177

156178
/**
157-
* @notice Withdraw fees.
179+
* @notice Withdraw USDC fees.
158180
* @param _to Withdrawal address.
159181
* @param _amount Amount of USDC to withdraw.
160182
*/
161-
function withdrawFees(address _to, uint256 _amount) external onlyOwner {
183+
function withdrawUsdcFees(address _to, uint256 _amount) external onlyOwner {
162184
require(_to != address(0), "Invalid withdrawal address");
163185
require(_amount > 0, "Withdrawal amount must be greater than zero");
164186
require(_amount <= usdcToken.balanceOf(address(this)), "Insufficient balance");
@@ -167,6 +189,24 @@ contract Exchange is ReentrancyGuard, Ownable {
167189
emit FeesWithdrawn(_to, _amount);
168190
}
169191

192+
/**
193+
* @notice Withdraw XSGD fees.
194+
* @param _to Withdrawal address.
195+
* @param _amount Amount of XSGD to withdraw.
196+
*/
197+
function withdrawXsgdFees(address _to, uint256 _amount) external onlyOwner {
198+
require(_to != address(0), "Invalid withdrawal address");
199+
require(_amount > 0, "Withdrawal amount must be greater than zero");
200+
require(_amount <= xsgdToken.balanceOf(address(this)), "Insufficient balance");
201+
202+
xsgdToken.safeTransfer(_to, _amount);
203+
emit FeesWithdrawn(_to, _amount);
204+
}
205+
206+
///////////////////
207+
// VAULT HELPERS //
208+
///////////////////
209+
170210
/**
171211
* @notice Get current price per share
172212
* @return Current price of 1 share in terms of assets (USDC)

0 commit comments

Comments
 (0)