-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.19; | ||
|
||
contract MockSwap { | ||
event Swap(address indexed user, uint256 amount); | ||
event Mint(address indexed user, uint256 amount); | ||
event ReverseSwap(address indexed user, uint256 amount); | ||
|
||
mapping(address => uint256) public balances; | ||
mapping(address => uint256) public tokenBalances; | ||
address[] public users; | ||
|
||
function deposit() external payable { | ||
if (balances[msg.sender] == 0 && tokenBalances[msg.sender] == 0) { | ||
users.push(msg.sender); | ||
} | ||
balances[msg.sender] += msg.value; | ||
tokenBalances[msg.sender] += msg.value; // 1:1 ratio for simplicity | ||
emit Mint(msg.sender, msg.value); | ||
} | ||
|
||
function swap(uint256 amount) external { | ||
require(tokenBalances[msg.sender] >= amount, "Insufficient token balance"); | ||
tokenBalances[msg.sender] -= amount; | ||
emit Swap(msg.sender, amount); | ||
} | ||
|
||
function reverseSwap(uint256 amount) external { | ||
require(balances[msg.sender] >= amount, "Insufficient ether balance"); | ||
balances[msg.sender] -= amount; | ||
tokenBalances[msg.sender] += amount; // Mint back the tokens | ||
payable(msg.sender).transfer(amount); | ||
emit ReverseSwap(msg.sender, amount); | ||
} | ||
|
||
function getBalance(address user) external view returns (uint256) { | ||
return balances[user]; | ||
} | ||
|
||
function getTokenBalance(address user) external view returns (uint256) { | ||
return tokenBalances[user]; | ||
} | ||
|
||
function resetBalances() external { | ||
// Only for demonstration purposes; in a real scenario, proper access control is needed. | ||
for (uint256 i = 0; i < users.length; i++) { | ||
balances[users[i]] = 0; | ||
tokenBalances[users[i]] = 0; | ||
} | ||
delete users; | ||
} | ||
} |