Twitter: Jack Lee|Conflux DAO
Community: JackLee.io |Conflux Forum
All code and tutorials are open source on GitHub: https://github.com/jackleeio/TokenCraft
A pausable token is a type of ERC20 token that can be paused and unpaused by the contract owner. When paused, all token transfers are suspended, which can be useful in emergency situations or during maintenance periods.
Pausable Token: ERC20WithPausable.sol
To run the tests for the ERC20WithPausable contract, use the following command:
forge test --match-contract test/ERC20WithPausable.t.sol -vvv (TODO)
The tests should cover the following scenarios:
- Initial token distribution
- Pausing and unpausing the contract
- Transfer behavior when paused and unpaused
- Access control for pause and unpause functions
The ERC20WithPausable contract inherits from both ERC20 and ERC20Pausable. Here are the main method calls:
pause()
: Pauses the contract, preventing transfers.unpause()
: Unpauses the contract, allowing transfers.transfer(address to, uint256 amount)
: Transfers tokens to a specified address.transferFrom(address from, address to, uint256 amount)
: Transfers tokens from one address to another.balanceOf(address account)
: Returns the token balance of the specified address.approve(address spender, uint256 amount)
: Approves the specified address to spend a certain amount of tokens.allowance(address owner, address spender)
: Returns the remaining number of tokens that the spender is allowed to spend on behalf of the owner.
To deploy the ERC20WithPausable token using Foundry, follow these steps:
-
Make sure you have Foundry 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
-
Create a deployment script
script/ERC20WithPausable.s.sol
:// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import "forge-std/Script.sol"; import "../src/ERC20/ERC20WithPausable.sol"; contract DeployERC20WithPausable is Script { function run() external { uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY"); vm.startBroadcast(deployerPrivateKey); ERC20WithPausable token = new ERC20WithPausable( "Pausable Token", "PAUSE", 1000000 * 10**18 // Total supply of 1,000,000 tokens ); vm.stopBroadcast(); } }
-
Run the following command to deploy:
forge script script/ERC20/ERC20WithPausable.s.sol:DeployERC20WithPausable --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, ensure your account has enough native tokens (e.g., ETH, CFX) to cover gas fees.