|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.20; |
| 3 | + |
| 4 | +/** |
| 5 | + * @title QFLOP Token V2 |
| 6 | + * @notice ERC-20 token backed by GPU quantum compute production |
| 7 | + * @dev Includes withdraw functionality for self-funding |
| 8 | + */ |
| 9 | +contract QFLOPTokenV2 { |
| 10 | + string public constant name = "QFLOP Quantum Compute"; |
| 11 | + string public constant symbol = "QFLOP"; |
| 12 | + uint8 public constant decimals = 18; |
| 13 | + |
| 14 | + uint256 public totalSupply; |
| 15 | + address public owner; |
| 16 | + address public minter; |
| 17 | + |
| 18 | + mapping(address => uint256) public balanceOf; |
| 19 | + mapping(address => mapping(address => uint256)) public allowance; |
| 20 | + |
| 21 | + // Production tracking |
| 22 | + uint256 public totalQFLOPSMinted; |
| 23 | + uint256 public lastMintTimestamp; |
| 24 | + uint256 public mintRatePerSecond; |
| 25 | + |
| 26 | + event Transfer(address indexed from, address indexed to, uint256 value); |
| 27 | + event Approval(address indexed owner, address indexed spender, uint256 value); |
| 28 | + event Mint(address indexed to, uint256 amount, uint256 qflopsProduced); |
| 29 | + event MinterUpdated(address indexed oldMinter, address indexed newMinter); |
| 30 | + event ProductionRateUpdated(uint256 oldRate, uint256 newRate); |
| 31 | + event Withdraw(address indexed to, uint256 amount); |
| 32 | + event Received(address indexed from, uint256 amount); |
| 33 | + |
| 34 | + modifier onlyOwner() { |
| 35 | + require(msg.sender == owner, "Not owner"); |
| 36 | + _; |
| 37 | + } |
| 38 | + |
| 39 | + modifier onlyMinter() { |
| 40 | + require(msg.sender == minter || msg.sender == owner, "Not authorized to mint"); |
| 41 | + _; |
| 42 | + } |
| 43 | + |
| 44 | + constructor(address _minter, uint256 _initialSupply) { |
| 45 | + owner = msg.sender; |
| 46 | + minter = _minter; |
| 47 | + lastMintTimestamp = block.timestamp; |
| 48 | + mintRatePerSecond = 3000 * 1e18; |
| 49 | + |
| 50 | + // Mint initial supply to owner |
| 51 | + if (_initialSupply > 0) { |
| 52 | + totalSupply = _initialSupply; |
| 53 | + balanceOf[msg.sender] = _initialSupply; |
| 54 | + emit Transfer(address(0), msg.sender, _initialSupply); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Allow contract to receive ETH |
| 59 | + receive() external payable { |
| 60 | + emit Received(msg.sender, msg.value); |
| 61 | + } |
| 62 | + |
| 63 | + fallback() external payable { |
| 64 | + emit Received(msg.sender, msg.value); |
| 65 | + } |
| 66 | + |
| 67 | + function transfer(address to, uint256 amount) external returns (bool) { |
| 68 | + require(balanceOf[msg.sender] >= amount, "Insufficient balance"); |
| 69 | + balanceOf[msg.sender] -= amount; |
| 70 | + balanceOf[to] += amount; |
| 71 | + emit Transfer(msg.sender, to, amount); |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + function approve(address spender, uint256 amount) external returns (bool) { |
| 76 | + allowance[msg.sender][spender] = amount; |
| 77 | + emit Approval(msg.sender, spender, amount); |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + function transferFrom(address from, address to, uint256 amount) external returns (bool) { |
| 82 | + require(balanceOf[from] >= amount, "Insufficient balance"); |
| 83 | + require(allowance[from][msg.sender] >= amount, "Insufficient allowance"); |
| 84 | + allowance[from][msg.sender] -= amount; |
| 85 | + balanceOf[from] -= amount; |
| 86 | + balanceOf[to] += amount; |
| 87 | + emit Transfer(from, to, amount); |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @notice Mint tokens based on GPU production |
| 93 | + */ |
| 94 | + function mintFromProduction(address to, uint256 qflopsProduced) external onlyMinter { |
| 95 | + uint256 tokensToMint = qflopsProduced / 1e9; |
| 96 | + require(tokensToMint > 0, "Production too low to mint"); |
| 97 | + |
| 98 | + totalSupply += tokensToMint; |
| 99 | + balanceOf[to] += tokensToMint; |
| 100 | + totalQFLOPSMinted += qflopsProduced; |
| 101 | + lastMintTimestamp = block.timestamp; |
| 102 | + |
| 103 | + emit Transfer(address(0), to, tokensToMint); |
| 104 | + emit Mint(to, tokensToMint, qflopsProduced); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @notice Mint tokens based on time elapsed |
| 109 | + */ |
| 110 | + function mintFromTime(address to) external onlyMinter { |
| 111 | + uint256 elapsed = block.timestamp - lastMintTimestamp; |
| 112 | + require(elapsed > 0, "No time elapsed"); |
| 113 | + |
| 114 | + uint256 tokensToMint = (mintRatePerSecond * elapsed) / 1e18; |
| 115 | + require(tokensToMint > 0, "Too soon to mint"); |
| 116 | + |
| 117 | + totalSupply += tokensToMint * 1e18; |
| 118 | + balanceOf[to] += tokensToMint * 1e18; |
| 119 | + lastMintTimestamp = block.timestamp; |
| 120 | + |
| 121 | + emit Transfer(address(0), to, tokensToMint * 1e18); |
| 122 | + emit Mint(to, tokensToMint * 1e18, tokensToMint * 1e9); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @notice Direct mint function for owner |
| 127 | + */ |
| 128 | + function mint(address to, uint256 amount) external onlyMinter { |
| 129 | + totalSupply += amount; |
| 130 | + balanceOf[to] += amount; |
| 131 | + lastMintTimestamp = block.timestamp; |
| 132 | + emit Transfer(address(0), to, amount); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @notice Withdraw ETH from contract |
| 137 | + * @param to Recipient address |
| 138 | + * @param amount Amount to withdraw (0 = all) |
| 139 | + */ |
| 140 | + function withdraw(address payable to, uint256 amount) external onlyOwner { |
| 141 | + uint256 balance = address(this).balance; |
| 142 | + require(balance > 0, "No ETH to withdraw"); |
| 143 | + |
| 144 | + uint256 withdrawAmount = amount == 0 ? balance : amount; |
| 145 | + require(withdrawAmount <= balance, "Insufficient contract balance"); |
| 146 | + |
| 147 | + (bool success, ) = to.call{value: withdrawAmount}(""); |
| 148 | + require(success, "ETH transfer failed"); |
| 149 | + |
| 150 | + emit Withdraw(to, withdrawAmount); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * @notice Withdraw all ETH to owner |
| 155 | + */ |
| 156 | + function withdrawAll() external onlyOwner { |
| 157 | + uint256 balance = address(this).balance; |
| 158 | + require(balance > 0, "No ETH to withdraw"); |
| 159 | + |
| 160 | + (bool success, ) = payable(owner).call{value: balance}(""); |
| 161 | + require(success, "ETH transfer failed"); |
| 162 | + |
| 163 | + emit Withdraw(owner, balance); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @notice Emergency token recovery |
| 168 | + */ |
| 169 | + function recoverTokens(address token, uint256 amount) external onlyOwner { |
| 170 | + require(token != address(this), "Cannot recover own tokens"); |
| 171 | + (bool success, bytes memory data) = token.call( |
| 172 | + abi.encodeWithSignature("transfer(address,uint256)", owner, amount) |
| 173 | + ); |
| 174 | + require(success && (data.length == 0 || abi.decode(data, (bool))), "Token recovery failed"); |
| 175 | + } |
| 176 | + |
| 177 | + function setMinter(address newMinter) external onlyOwner { |
| 178 | + emit MinterUpdated(minter, newMinter); |
| 179 | + minter = newMinter; |
| 180 | + } |
| 181 | + |
| 182 | + function setMintRate(uint256 newRate) external onlyOwner { |
| 183 | + emit ProductionRateUpdated(mintRatePerSecond, newRate); |
| 184 | + mintRatePerSecond = newRate; |
| 185 | + } |
| 186 | + |
| 187 | + function transferOwnership(address newOwner) external onlyOwner { |
| 188 | + require(newOwner != address(0), "Invalid owner"); |
| 189 | + owner = newOwner; |
| 190 | + } |
| 191 | + |
| 192 | + function pendingMint() external view returns (uint256) { |
| 193 | + uint256 elapsed = block.timestamp - lastMintTimestamp; |
| 194 | + return (mintRatePerSecond * elapsed) / 1e18; |
| 195 | + } |
| 196 | + |
| 197 | + function contractBalance() external view returns (uint256) { |
| 198 | + return address(this).balance; |
| 199 | + } |
| 200 | +} |
0 commit comments