// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/utils/math/SafeMath.sol"; contract UsernameReservation { using SafeMath for uint256; address public admin; address public e3xTokenAddress; uint256 public tokenPrice = 1e18; // 1 E3X token mapping(address => string) public usernames; event UsernameReserved(address indexed user, string username); event TokensDeposited(address indexed user, uint256 amount); event TokensWithdrawn(address admin, uint256 amount); constructor(address _e3xTokenAddress) { admin = msg.sender; e3xTokenAddress = _e3xTokenAddress; } modifier onlyAdmin() { require(msg.sender == admin, "Only admin can call this function"); _; } function reserveUsername(string memory _username) external { require(bytes(usernames[msg.sender]).length == 0, "Username already reserved"); require(bytes(_username).length <= 9, "Username must be up to 9 characters long"); require(validateAlphanumeric(_username), "Username must be alphanumeric"); // Convert username to uppercase string memory uppercaseUsername = uppercase(_username); // Reserve username usernames[msg.sender] = uppercaseUsername; emit UsernameReserved(msg.sender, uppercaseUsername); } function depositTokens(uint256 _amount) external { IERC20(e3xTokenAddress).transferFrom(msg.sender, address(this), _amount); emit TokensDeposited(msg.sender, _amount); } function withdrawTokens(uint256 _amount) external onlyAdmin { IERC20(e3xTokenAddress).transfer(admin, _amount); emit TokensWithdrawn(admin, _amount); } function validateAlphanumeric(string memory _str) private pure returns (bool) { bytes memory b = bytes(_str); for (uint256 i = 0; i < b.length; i++) { if (!((uint8(b[i]) >= 48 && uint8(b[i]) <= 57) || // 0-9 (uint8(b[i]) >= 65 && uint8(b[i]) <= 90) || // A-Z (uint8(b[i]) >= 97 && uint8(b[i]) <= 122))) // a-z return false; } return true; } function uppercase(string memory _str) private pure returns (string memory) { bytes memory bStr = bytes(_str); for (uint256 i = 0; i < bStr.length; i++) { // Uppercase lowercase letters if (uint8(bStr[i]) >= 97 && uint8(bStr[i]) <= 122) { bStr[i] = bytes1(uint8(bStr[i]) - 32); } } return string(bStr); } // Function to transfer user data to a new contract (unused in this example) function transferToNewContract(address _newContract) public onlyAdmin { // Implement logic to transfer data to the new contract } }