Facebook
From hamja, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 173
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3.  
  4. // Import ERC20 interface and other necessary contracts
  5. import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
  6. import "@openzeppelin/contracts/access/Ownable.sol";
  7.  
  8. contract VPNIDGenerator is Ownable {
  9.     IERC20 public sVPNToken;
  10.     uint256 public nextID;
  11.     uint256 constant public IDLength = 15;
  12.     uint256 public paymentAmountMonthly = 200000; // Default monthly payment amount
  13.     uint256 public paymentAmountYearly = 2200000; // Default yearly payment amount, assuming some discount or different rate
  14.  
  15.     event IDGenerated(address indexed payer, string indexed generatedID, string paymentType);
  16.     event TokenContractUpdated(address indexed newTokenContract);
  17.     event MonthlyPaymentAmountUpdated(uint256 newPaymentAmount);
  18.     event YearlyPaymentAmountUpdated(uint256 newPaymentAmount);
  19.  
  20.     mapping(address => string[]) public userIDs; // Mapping to store user IDs against wallet address
  21.     mapping(string => bool) public usedIDs; // Mapping to track used IDs
  22.  
  23.     constructor(address _sVPNTokenAddress, address initialOwner) Ownable(initialOwner) {
  24.         sVPNToken = IERC20(_sVPNTokenAddress);
  25.         nextID = 1;
  26.     }
  27.  
  28.     modifier validPayment(uint256 paymentAmount) {
  29.         require(sVPNToken.allowance(msg.sender, address(this)) >= paymentAmount, "Insufficient allowance");
  30.         require(sVPNToken.balanceOf(msg.sender) >= paymentAmount, "Insufficient balance");
  31.         _;
  32.     }
  33.  
  34.     function payForUniqueIDMonthly() external validPayment(paymentAmountMonthly) {
  35.         require(sVPNToken.transferFrom(msg.sender, address(this), paymentAmountMonthly), "Token transfer failed");
  36.         string memory generatedID = generateUniqueID();
  37.         userIDs[msg.sender].push(generatedID);
  38.         emit IDGenerated(msg.sender, generatedID, "Monthly");
  39.     }
  40.  
  41.     function payForUniqueIDYearly() external validPayment(paymentAmountYearly) {
  42.         require(sVPNToken.transferFrom(msg.sender, address(this), paymentAmountYearly), "Token transfer failed");
  43.         string memory generatedID = generateUniqueID();
  44.         userIDs[msg.sender].push(generatedID);
  45.         emit IDGenerated(msg.sender, generatedID, "Yearly");
  46.     }
  47.  
  48.     function generateUniqueID() internal returns (string memory) {
  49.         bytes memory characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  50.         bytes memory result = new bytes(IDLength);
  51.  
  52.         uint256 rand = uint256(keccak256(abi.encodePacked(block.timestamp, block.difficulty, nextID))) % characters.length;
  53.  
  54.         for (uint256 i = 0; i < IDLength; i++) {
  55.             if (i == IDLength - 1) {
  56.                 result[i] = bytes1(uint8(48 + (rand % 10))); // ASCII code for numbers 0-9
  57.             } else {
  58.                 result[i] = characters[rand % characters.length];
  59.             }
  60.             rand = rand / characters.length;
  61.         }
  62.  
  63.         string memory newID = string(result);
  64.  
  65.         // Check if the ID already exists, regenerate if necessary
  66.         while (usedIDs[newID]) {
  67.             rand = uint256(keccak256(abi.encodePacked(rand))) % characters.length;
  68.             for (uint256 i = 0; i < IDLength; i++) {
  69.                 if (i == IDLength - 1) {
  70.                     result[i] = bytes1(uint8(48 + (rand % 10))); // ASCII code for numbers 0-9
  71.                 } else {
  72.                     result[i] = characters[rand % characters.length];
  73.                 }
  74.                 rand = rand / characters.length;
  75.             }
  76.             newID = string(result);
  77.         }
  78.  
  79.         usedIDs[newID] = true;
  80.         nextID++;
  81.         return newID;
  82.     }
  83.  
  84.     function getUserIDs(address userAddress) external view returns (string[] memory) {
  85.         return userIDs[userAddress];
  86.     }
  87.  
  88.     function withdrawTokens(uint256 _amount) external onlyOwner {
  89.         require(sVPNToken.transfer(owner(), _amount), "Token transfer failed");
  90.     }
  91.  
  92.     function updateTokenContract(address _newTokenContract) external onlyOwner {
  93.         require(_newTokenContract != address(0), "Invalid token contract address");
  94.         sVPNToken = IERC20(_newTokenContract);
  95.         emit TokenContractUpdated(_newTokenContract);
  96.     }
  97.  
  98.     function updateMonthlyPaymentAmount(uint256 _newPaymentAmount) external onlyOwner {
  99.         require(_newPaymentAmount > 0, "Invalid payment amount");
  100.         paymentAmountMonthly = _newPaymentAmount;
  101.         emit MonthlyPaymentAmountUpdated(_newPaymentAmount);
  102.     }
  103.  
  104.     function updateYearlyPaymentAmount(uint256 _newPaymentAmount) external onlyOwner {
  105.         require(_newPaymentAmount > 0, "Invalid payment amount");
  106.         paymentAmountYearly = _newPaymentAmount;
  107.         emit YearlyPaymentAmountUpdated(_newPaymentAmount);
  108.     }
  109. }