// SPDX-License-Identifier: MIT pragma solidity 0.8.24; /** * GMTronTokenLocker (TronMinter, 26 Nov 2026) * * Time-locks any TRC-20, built for SunSwap V2 LP tokens so buyers can see a * pool's liquidity cannot be pulled before a set date. It also works for team * or marketing tokens a project wants to show are locked. TRON port of * ethtokenlaunch.com/contracts/GMTokenLocker.sol, same functions and events. * * - While locked, nobody can move the tokens: not the lock owner, not us. * - The lock owner can push the unlock date later (never earlier) and hand the * lock to another wallet. After the date only the lock owner can withdraw. * - The amount recorded is what actually arrived, so a token with a transfer * tax cannot make a lock claim more than the contract holds. * - No owner, no admin, no fee logic, no upgrade path. The service fee is a * separate TRX payment checked by the website. * * TRON differences handled here: * - TRC-20 USDT on TRON returns false from transfer() even when it worked. * lock() already measures what arrived, so a false return there is judged * by the balance. On withdraw a false return is accepted only if this * contract's balance dropped by the full amount. A true or empty return is * trusted, as on the EVM version, so reflection tokens cannot get stuck. * - The token must have code (a call to an empty address "succeeds"). */ interface ITRC20Min { function balanceOf(address) external view returns (uint256); } contract GMTronTokenLocker { struct Lock { address token; address owner; uint256 amount; uint64 lockedAt; uint64 unlockTime; bool withdrawn; } uint64 public constant MAX_LOCK_SECONDS = 100 * 365 days; Lock[] private _locks; mapping(address => uint256[]) private _ownerLocks; mapping(address => uint256[]) private _tokenLocks; /// Amount of each token still held under an active lock. mapping(address => uint256) public lockedAmount; uint256 private _entered; event Locked(uint256 indexed lockId, address indexed owner, address indexed token, uint256 amount, uint64 unlockTime); event Extended(uint256 indexed lockId, uint64 oldUnlockTime, uint64 newUnlockTime); event OwnerChanged(uint256 indexed lockId, address indexed oldOwner, address indexed newOwner); event Withdrawn(uint256 indexed lockId, address indexed owner, address indexed token, uint256 amount); error NotLockOwner(); error BadUnlockTime(); error BadAmount(); error StillLocked(); error AlreadyWithdrawn(); error NoSuchLock(); error ZeroAddress(); error NotAContract(); error TransferFailed(); error Reentrancy(); modifier nonReentrant() { if (_entered == 1) revert Reentrancy(); _entered = 1; _; _entered = 0; } /// Lock `amount` of `token` until `unlockTime` (unix seconds). Approve this contract for the amount first. function lock(address token, uint256 amount, uint64 unlockTime) external nonReentrant returns (uint256 lockId) { if (token == address(0)) revert ZeroAddress(); if (token.code.length == 0) revert NotAContract(); if (amount == 0) revert BadAmount(); if (unlockTime <= block.timestamp || unlockTime > block.timestamp + MAX_LOCK_SECONDS) revert BadUnlockTime(); uint256 before = ITRC20Min(token).balanceOf(address(this)); // The return value is not read: what arrived is judged by the balance below. (bool ok, ) = token.call(abi.encodeWithSelector(0x23b872dd, msg.sender, address(this), amount)); if (!ok) revert TransferFailed(); uint256 afterBal = ITRC20Min(token).balanceOf(address(this)); if (afterBal <= before) revert BadAmount(); uint256 received = afterBal - before; lockId = _locks.length; _locks.push(Lock({ token: token, owner: msg.sender, amount: received, lockedAt: uint64(block.timestamp), unlockTime: unlockTime, withdrawn: false })); _ownerLocks[msg.sender].push(lockId); _tokenLocks[token].push(lockId); lockedAmount[token] += received; emit Locked(lockId, msg.sender, token, received, unlockTime); } function extend(uint256 lockId, uint64 newUnlockTime) external { Lock storage l = _ownedLock(lockId); if (l.withdrawn) revert AlreadyWithdrawn(); if (newUnlockTime <= l.unlockTime || newUnlockTime > block.timestamp + MAX_LOCK_SECONDS) revert BadUnlockTime(); emit Extended(lockId, l.unlockTime, newUnlockTime); l.unlockTime = newUnlockTime; } function transferLockOwnership(uint256 lockId, address newOwner) external { if (newOwner == address(0)) revert ZeroAddress(); Lock storage l = _ownedLock(lockId); if (l.withdrawn) revert AlreadyWithdrawn(); address old = l.owner; l.owner = newOwner; _ownerLocks[newOwner].push(lockId); emit OwnerChanged(lockId, old, newOwner); } function withdraw(uint256 lockId) external nonReentrant { Lock storage l = _ownedLock(lockId); if (l.withdrawn) revert AlreadyWithdrawn(); if (block.timestamp < l.unlockTime) revert StillLocked(); l.withdrawn = true; lockedAmount[l.token] -= l.amount; address token = l.token; uint256 amount = l.amount; uint256 before = ITRC20Min(token).balanceOf(address(this)); (bool ok, bytes memory ret) = token.call(abi.encodeWithSelector(0xa9059cbb, l.owner, amount)); if (!ok) revert TransferFailed(); if (ret.length != 0) { if (ret.length < 32) revert TransferFailed(); if (abi.decode(ret, (uint256)) == 0) { uint256 afterBal = ITRC20Min(token).balanceOf(address(this)); if (afterBal > before || before - afterBal < amount) revert TransferFailed(); } } emit Withdrawn(lockId, l.owner, token, amount); } // ---- views ---- function lockCount() external view returns (uint256) { return _locks.length; } function getLock(uint256 lockId) external view returns (Lock memory) { if (lockId >= _locks.length) revert NoSuchLock(); return _locks[lockId]; } /// Lock ids this wallet has ever owned. Check getLock().owner, since a /// handed-over lock stays in the old owner's list. function locksOf(address owner) external view returns (uint256[] memory) { return _ownerLocks[owner]; } /// Every lock ever made for a token or LP pair, active or withdrawn. function locksForToken(address token) external view returns (uint256[] memory) { return _tokenLocks[token]; } function _ownedLock(uint256 lockId) private view returns (Lock storage l) { if (lockId >= _locks.length) revert NoSuchLock(); l = _locks[lockId]; if (l.owner != msg.sender) revert NotLockOwner(); } }