Twitter: Jack Lee|Conflux DAO
Community: JackLee.io |Conflux Forum
All code and tutorials are open source on GitHub: https://github.com/jackleeio/CoinCraft
Timelock tokens are a special type of ERC20 token that allows tokens to be locked for a certain period, preventing transfers. This mechanism is commonly used for team tokens, investor tokens, or reward programs to ensure that tokens can only be used after a specific time.
Timelock Token: ERC20WithTimelock.sol
forge test --match-contract ERC20WithTimelockTest -vvv
The tests should cover the following scenarios:
- Initial token distribution
- Transfer restrictions during the lock period
- Transfer behavior after unlocking
- Querying locked balances and unlock times
The ERC20WithTimelock contract inherits from ERC20 and Ownable. Here are the main method calls:
unlockTokens()
: Unlocks tokens after the lock period ends.getLockedBalance(address account)
: Gets the locked balance for a specified address.getLockEndTime(address account)
: Gets the lock end time for a specified address.getTotalLockedTokens()
: Gets the total amount of locked tokens.transfer(address to, uint256 amount)
: Transfers tokens (includes balance check).transferFrom(address from, address to, uint256 amount)
: Transfers tokens from one address to another (includes balance check).balanceOf(address account)
: Returns the token balance of the specified address (including locked tokens).
To deploy the ERC20WithTimelock token using Foundry, follow these steps:
-
Make sure Foundry is installed. If not, refer to the Foundry Installation Guide.
-
Create a
.env
file in the project root directory and add the following content:PRIVATE_KEY=your_private_key RPC_URL=your_target_network_rpc_url
-
Run the following command to deploy:
forge script script/ERC20/ERC20WithTimelock.s.sol:DeployERC20WithTimelock --rpc-url $RPC_URL --broadcast --verify
-
After deployment, you will see the deployed contract address in the console output. Save this address for future use.
Note: Before deploying, make sure your account has enough native tokens (such as ETH, CFX) to pay for gas fees.