// 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 DCPoints is Ownable { e2XUsernames public userDataContract; IERC20 public e2xToken; uint256 public pointsPerToken; address public dcpBurntAccount; struct DCPointBalance { uint128 balance; // Represents DCPoints balance in multiples of 100 } mapping(address => DCPointBalance) public dcpBalance; mapping(address => uint256) public totalDCPBalance; mapping(address => bool) public dcPointsAdmins; mapping(address => bool) public authorizedContracts; event PointsRatioUpdated(uint256 newPointsPerToken); event DCPUpdated(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 DCPointDeducted(address indexed from, address indexed to, uint256 amount); modifier onlyDCPointsAdminOrContract() { require(dcPointsAdmins[msg.sender] || authorizedContracts[msg.sender], "Sender is not authorized"); _; } constructor(address _userDataContract, address _e2xToken) { userDataContract = e2XUsernames(_userDataContract); e2xToken = IERC20(_e2xToken); pointsPerToken = 100; // 100 DCP per E2X token initially dcPointsAdmins[msg.sender] = true; // Set contract deployer as admin by default //dcpBurntAccount = 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"); dcPointsAdmins[_admin] = true; emit AdminAdded(_admin); } function removeAdmin(address _admin) external onlyOwner { require(dcPointsAdmins[_admin], "Address is not an admin"); dcPointsAdmins[_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; dcpBalance[userAddress].balance += uint128(_amount * pointsPerToken); totalDCPBalance[userAddress] += _amount * pointsPerToken; emit DCPUpdated(userAddress, _amount * pointsPerToken, true); } function transferDCP(address _fromUser, address _toUser, uint256 _amount) external onlyDCPointsAdminOrContract { // Ensure that the sender has sufficient DCP balance require(dcpBalance[_fromUser].balance >= _amount, "Insufficient DCP balance"); // Deduct 1 DCPoint from the sender and transfer it to the admin dcpBalance[_fromUser].balance -= 1; dcpBalance[owner()].balance += 1; // Emit an event to log the deduction of 1 DCPoint emit DCPointDeducted(_fromUser, owner(), 1); // Transfer the remaining DCPoints from _fromUser to _toUser dcpBalance[_fromUser].balance -= uint128(_amount); dcpBalance[_toUser].balance += uint128(_amount); // Emit an event to log the DCP transfer emit DCPointDeducted(_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(dcPointsAdmins[_newAdmin], "Address is not an admin"); transferOwnership(_newAdmin); } function getUserDCPBalance(address _user) external view returns (uint256) { return dcpBalance[_user].balance; } function getTotalDCPBalanceInSystem() external view returns (uint256) { return totalDCPBalance[address(this)]; } function deductDCP(address _fromUser, address _toUser, uint256 _amount) public { require(dcpBalance[_fromUser].balance >= _amount, "Insufficient DCP balance"); dcpBalance[_fromUser].balance -= uint128(_amount); dcpBalance[_toUser].balance += uint128(_amount); emit DCPointDeducted(_fromUser, _toUser, _amount); } function transferDCPBetweenMembers(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 DCP balance require(dcpBalance[fromUserAddress].balance >= _amount, "Insufficient DCP balance"); // Deduct 1 DCPoint from the sender and transfer it to the admin dcpBalance[fromUserAddress].balance -= 1; dcpBalance[owner()].balance += 1; // Emit an event to log the deduction of 1 DCPoint emit DCPointDeducted(fromUserAddress, owner(), 1); // Perform the DCP transfer dcpBalance[fromUserAddress].balance -= uint128(_amount); dcpBalance[toUserAddress].balance += uint128(_amount); // Emit an event to log the DCP transfer emit DCPointDeducted(fromUserAddress, toUserAddress, _amount); } }