// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import "/e2XUsers1.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; contract TCPointContract is Ownable { e2XUsernames public userDataContract; IERC20 public e2xToken; uint256 public pointsPerToken; address public tcPointBurntAccount; struct TCPointBalance { uint128 balance; // Represents TCPoints balance in multiples of 100 } struct uq112x112 { uint224 _x; } // Decimal scaling factor uint8 private constant DECIMALS = 18; mapping(address => TCPointBalance) public tcBalance; mapping(address => uint256) public totalTCBalance; mapping(address => bool) public tcPointsAdmins; mapping(address => bool) public authorizedContracts; event PointsRatioUpdated(uint256 newPointsPerToken); event TCPUpdated(address user, uint256 amount, bool added); event AdminAdded(address indexed admin); event AdminRemoved(address indexed admin); event ContractAuthorized(address indexed contractAddress); event ContractUnauthorized(address indexed contractAddress); event TCPointDeducted(address indexed from, address indexed to, uint256 amount); modifier onlyTCPointsAdminOrContract() { require(tcPointsAdmins[msg.sender] || authorizedContracts[msg.sender], "Sender is not authorized"); _; } constructor(address _userDataContract, address _e2xToken) { userDataContract = e2XUsernames(_userDataContract); e2xToken = IERC20(_e2xToken); pointsPerToken = 100; // 100 TCP per E2X token initially tcPointsAdmins[msg.sender] = true; // Set contract deployer as admin by default //tcPointBurntAccount = address(0x5e6A3a01e7d7280E873CBfAA644c97E724fd7F82); // Initialize the burnt account } function setPointsPerToken(uint256 _newPointsPerToken) external onlyOwner { pointsPerToken = _newPointsPerToken; emit PointsRatioUpdated(_newPointsPerToken); } function addAdmin(address _admin) external onlyOwner { require(_admin != address(0), "Invalid admin address"); tcPointsAdmins[_admin] = true; emit AdminAdded(_admin); } function removeAdmin(address _admin) external onlyOwner { require(tcPointsAdmins[_admin], "Address is not an admin"); tcPointsAdmins[_admin] = false; emit AdminRemoved(_admin); } function authorizeContract(address _contractAddress) external onlyOwner { require(_contractAddress != address(0), "Invalid contract address"); authorizedContracts[_contractAddress] = true; emit ContractAuthorized(_contractAddress); } function unauthorizeContract(address _contractAddress) external onlyOwner { require(authorizedContracts[_contractAddress], "Contract is not authorized"); authorizedContracts[_contractAddress] = false; emit ContractUnauthorized(_contractAddress); } function transferE2XToken(uint256 _amount) external { require(_amount > 0, "Amount must be greater than 0"); require(e2xToken.transferFrom(msg.sender, address(this), _amount), "E2X token transfer failed"); address userAddress = msg.sender; // Calculate the number of TCPoints to mint based on the transferred E2X tokens uint256 tcPointsToMint = fraction(_amount, 1 ether); // 1 ether represents 1 E2X token // Mint TCPoints to the user tcBalance[userAddress].balance += uint128(tcPointsToMint); totalTCBalance[userAddress] += tcPointsToMint; emit TCPUpdated(userAddress, tcPointsToMint, true); } function transferTCP(address _fromUser, address _toUser, uint256 _amount) external onlyTCPointsAdminOrContract { // Ensure that the sender has sufficient TCP balance require(tcBalance[_fromUser].balance >= _amount, "Insufficient TCP balance"); // Deduct 1 TCPoint from the sender and transfer it to the admin tcBalance[_fromUser].balance -= 1; tcBalance[owner()].balance += 1; // Emit an event to log the deduction of 1 TCPoint emit TCPointDeducted(_fromUser, owner(), 1); // Transfer the remaining TCPoints from _fromUser to _toUser tcBalance[_fromUser].balance -= uint128(_amount); tcBalance[_toUser].balance += uint128(_amount); // Emit an event to log the TCP transfer emit TCPointDeducted(_fromUser, _toUser, _amount); } function withdrawE2XToken(uint256 _amount) external onlyOwner { require(_amount > 0, "Amount must be greater than 0"); require(e2xToken.balanceOf(address(this)) >= _amount, "Insufficient E2X token balance"); require(e2xToken.transfer(owner(), _amount), "E2X token transfer failed"); } function transferToNewContract(address _newContract) external view onlyOwner { require(_newContract != address(0), "Invalid new contract address"); // Implement logic to transfer data to the new contract } function changeAdmin(address _newAdmin) external onlyOwner { require(_newAdmin != address(0), "Invalid admin address"); require(tcPointsAdmins[_newAdmin], "Address is not an admin"); transferOwnership(_newAdmin); } function getUserTCPBalance(address _user) external view returns (uint256) { return tcBalance[_user].balance; } function getTotalTCPBalanceInSystem() external view returns (uint256) { return totalTCBalance[address(this)]; } modifier onlyAdminOrAuthorizedContract() { require(tcPointsAdmins[msg.sender] || authorizedContracts[msg.sender] || msg.sender == owner(), "Not authorized"); _; } function deductTCP(address _fromUser, address _toUser, uint256 _amount) public onlyAdminOrAuthorizedContract { require(tcBalance[_fromUser].balance >= _amount, "Insufficient TCP balance"); tcBalance[_fromUser].balance -= uint128(_amount); tcBalance[_toUser].balance += uint128(_amount); emit TCPointDeducted(_fromUser, _toUser, _amount); } function transferTCPBetweenMembers(string calldata _fromUser, string calldata _toUser, uint256 _amount) external { // Get the sender's address address senderAddress = msg.sender; // Get the addresses associated with the provided e2xdata usernames address fromUserAddress = userDataContract.getAddressByUser(_fromUser); address toUserAddress = userDataContract.getAddressByUser(_toUser); // Ensure that the sender is the owner of the e2xdata address (_fromUser) require(fromUserAddress == senderAddress, "Sender is not the owner of the e2xdata address"); // Check if the e2xdata addresses exist require(fromUserAddress != address(0) && toUserAddress != address(0), "Invalid e2xdata addresses"); // Check if the sender has sufficient TCP balance require(tcBalance[fromUserAddress].balance >= _amount, "Insufficient TCP balance"); // Deduct 1 TCPoint from the sender and transfer it to the admin tcBalance[fromUserAddress].balance -= 1; tcBalance[owner()].balance += 1; // Emit an event to log the deduction of 1 TCPoint emit TCPointDeducted(fromUserAddress, owner(), 1); // Perform the TCP transfer tcBalance[fromUserAddress].balance -= uint128(_amount); tcBalance[toUserAddress].balance += uint128(_amount); // Emit an event to log the TCP transfer emit TCPointDeducted(fromUserAddress, toUserAddress, _amount); } function fraction(uint256 numerator, uint256 denominator) internal pure returns (uint256) { require(denominator > 0, 'TCPointContract::fraction: division by zero'); if (numerator == 0) return 0; uint256 result = (numerator * (1 ether)) / denominator; require(result <= type(uint224).max, 'TCPointContract::fraction: overflow'); return result; } }