From 4acf08f5975e376e3f763746f6751e41ef7b9bef Mon Sep 17 00:00:00 2001 From: FiveMovesAhead Date: Thu, 16 Jan 2025 22:08:33 +0000 Subject: [PATCH] Add relock function to TokenLocker. --- tig-token/TokenLocker.sol | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tig-token/TokenLocker.sol b/tig-token/TokenLocker.sol index e14004e0..1b6fe105 100644 --- a/tig-token/TokenLocker.sol +++ b/tig-token/TokenLocker.sol @@ -12,12 +12,14 @@ interface ITokenLocker { event TokensUnlocked(address indexed user, uint256 amount, uint256 locked, uint256 withdrawableTime); event TokensWithdrawn(address indexed user, uint256 amount); event TokensRewarded(address indexed user, uint256 amount, uint256 locked); + event TokensRelocked(address indexed user, uint256 amount, uint256 locked); function getNumPendingWithdrawals(address account) external view returns (uint256); function getTimeUntilWithdrawable(address account, uint256 index) external view returns (uint256); function lock(uint256 amount) external; function unlock(uint256 amount) external; function withdraw(uint256 index) external; + function relock(uint256 index) external; function rewardTokens(address account, uint256 amount) external; } @@ -107,6 +109,25 @@ contract TokenLocker is ITokenLocker, ReentrancyGuard, Ownable { emit TokensWithdrawn(msg.sender, amount); } + function relock(uint256 index) external override nonReentrant { + require(index < pendingWithdrawals[msg.sender].length, "Invalid withdrawal index"); + PendingWithdrawal[] storage userWithdrawals = pendingWithdrawals[msg.sender]; + PendingWithdrawal memory withdrawal = userWithdrawals[index]; + + uint256 amount = withdrawal.amount; + + // Swap and pop + userWithdrawals[index] = userWithdrawals[userWithdrawals.length - 1]; + userWithdrawals.pop(); + + // Update state + totalPendingWithdrawal = totalPendingWithdrawal.sub(amount); + locked[msg.sender] = locked[msg.sender].add(amount); + totalLocked = totalLocked.add(amount); + + emit TokensRelocked(msg.sender, amount, locked[msg.sender]); + } + function rewardTokens(address account, uint256 amount) external override onlyOwner nonReentrant { require(account != address(0), "Invalid address"); require(amount > 0, "Amount must be greater than 0");