Token Locker Contract
The TokenLocker
contract provides a token locking mechanism where users can lock their tokens for a specific period of time and then withdraw them after the lock period ends. It also implements a panic withdrawal feature that allows users to withdraw their tokens early but with a penalty fee. The contract supports multiple tokens and maintains balances for each token holder.
State Variables
owner
: The address of the contract owner.price
: The required price in ETH for locking tokens.penaltyfee
: The penalty fee percentage deducted during panic withdrawals.
Structs
holder
struct holder {
address holderAddress;
mapping(address => Token) tokens;
}
Represents a token holder's information.
holderAddress
: The address of the token holder.tokens
: A mapping of token addresses to their correspondingToken
struct.
Token
struct Token {
uint256 balance;
address tokenAddress;
uint256 unlockTime;
}
Represents a locked token.
balance
: The balance of the locked tokens.tokenAddress
: The address of the locked token.unlockTime
: The timestamp when the tokens can be withdrawn.
Modifiers
onlyOwner
modifier onlyOwner() {
require(msg.sender == owner, "Only available to the contract owner.");
_;
}
Ensures that the function can only be called by the contract owner.
Events
Hold
event Hold(
address indexed holder,
address token,
uint256 amount,
uint256 unlockTime
);
Emitted when tokens are locked.
holder
: The address of the token holder.token
: The address of the locked token.amount
: The amount of tokens locked.unlockTime
: The timestamp when the tokens can be withdrawn.
PanicWithdraw
event PanicWithdraw(
address indexed holder,
address token,
uint256 amount,
uint256 unlockTime
);
Emitted when a panic withdrawal occurs.
holder
: The address of the token holder.token
: The address of the withdrawn token.amount
: The amount of tokens withdrawn.unlockTime
: The original unlock timestamp of the tokens.
Withdrawal
event Withdrawal(address indexed holder, address token, uint256 amount);
Emitted when tokens are successfully withdrawn.
holder
: The address of the token holder.token
: The address of the withdrawn token.amount
: The amount of tokens withdrawn.
FeesClaimed
event FeesClaimed();
Emitted when fees are claimed by the contract owner.
SetOwnerSuccess
event SetOwnerSuccess(address owner);
Emitted when the contract owner address is updated.
owner
: The new contract owner address.
SetPriceSuccess
event SetPriceSuccess(uint256 _price);
Emitted when the locking price is updated.
_price
: The new locking price.
SetPenaltyFeeSuccess
event SetPenaltyFeeSuccess(uint256 _fee);
Emitted when the penalty fee percentage is updated.
_fee
: The new penalty fee percentage.
OwnerWithdrawSuccess
event OwnerWithdrawSuccess(uint256 amount);
Emitted when the contract owner successfully withdraws ETH.
amount
: The amount of ETH
withdrawn.
Public Functions
tokenLock
function tokenLock(
address token,
uint256 amount,
uint256 unlockTime,
address withdrawer
) public payable
Locks the specified amount of tokens for the given withdrawer
address. The function requires a payment of price
in ETH to execute the lock operation.
token
: The address of the token to be locked.amount
: The amount of tokens to be locked.unlockTime
: The timestamp when the tokens can be withdrawn.withdrawer
: The address of the token holder.
withdraw
function withdraw(address token) public
Withdraws the locked tokens for the caller if the unlock time has been reached.
token
: The address of the token to be withdrawn.
panicWithdraw
function panicWithdraw(address token) public
Allows the token owner to perform a panic withdrawal, withdrawing the tokens before the unlock time with a penalty fee. The penalty fee is deducted from the withdrawal amount, and the remaining amount is transferred to the token owner. The penalty fee is also transferred to the contract owner.
token
: The address of the token to be panic-withdrawn.
OwnerWithdraw
function OwnerWithdraw() public onlyOwner
Allows the contract owner to withdraw the contract's balance (ETH).
getcurtime
function getcurtime() public view returns (uint256)
Returns the current block timestamp.
GetBalance
function GetBalance(address token) public view returns (uint256)
Returns the balance of the specified token locked for the caller.
token
: The address of the locked token.
SetOwner
function SetOwner(address contractowner) public onlyOwner
Sets a new contract owner address.
contractowner
: The new contract owner address.
SetPrice
function SetPrice(uint256 _price) public onlyOwner
Sets a new locking price.
_price
: The new locking price.
SetPenaltyFee
function SetPenaltyFee(uint256 _penaltyfee) public onlyOwner
Sets a new penalty fee percentage.
_penaltyfee
: The new penalty fee percentage.
GetUnlockTime
function GetUnlockTime(address token) public view returns (uint256)
Returns the unlock time of the specified token locked for the caller.
token
: The address of the locked token.