A Hardhat-based Ethereum project for deploying and managing the GymToken (GYM), a standard ERC20 token built with OpenZeppelin contracts.
This project demonstrates:
- ERC20 Token Implementation: A complete token contract using OpenZeppelin standards
- Hardhat Development Environment: Modern Ethereum development toolkit
- TypeScript Integration: Type-safe smart contract interactions
- Multiple Solidity Compiler Support: Configured for different Solidity versions
- Easy Deployment Scripts: Automated deployment with detailed logging
mytoken/
βββ contracts/
β βββ Token.sol # GymToken ERC20 contract
β βββ Counter.sol # Example counter contract
β βββ Counter.t.sol.bak # Foundry test file (backup)
βββ scripts/
β βββ deploy.ts # Main deployment script
β βββ send-op-tx.ts # OP transaction example
βββ ignition/
β βββ modules/
β βββ Counter.ts # Ignition deployment module
βββ test/
β βββ Counter.ts # Test files
βββ hardhat.config.ts # Hardhat configuration
βββ package.json # Dependencies and scripts
- Name: Gym Token
- Symbol: GYM
- Standard: ERC20 (OpenZeppelin implementation)
- Initial Supply: 1,000,000,000 GYM (1 billion tokens)
- Decimals: 18 (standard)
- Features:
- Transfer functionality
- Allowance system
- Minting at deployment
- Standard ERC20 events
- Node.js (v16 or higher)
- npm or yarn
- Git
- Clone the repository (or navigate to your project directory):
cd C:\Projectx\BlockChain\Ethereum\mytoken- Install dependencies:
npm install- Compile contracts:
npx hardhat compileCompile all Solidity contracts:
npx hardhat compileDeploy to local Hardhat network:
npx hardhat run scripts/deploy.ts --network hardhatDeploy to Sepolia testnet:
npx hardhat run scripts/deploy.ts --network sepoliaThe deployment script will:
- Deploy GymToken with 1 billion initial supply
- Mint all tokens to the deployer's address
- Display the deployed contract address
Verify your contract on Etherscan for better transparency:
npx hardhat verify --network sepolia <CONTRACT_ADDRESS> 1000000000Replace <CONTRACT_ADDRESS> with your deployed contract address.
- Contract Address: 0x5dbB770Daa57c7f345E1e55024F0f06247f89682
- Token Name: Gym Token
- Symbol: GYM
- Total Supply: 1,000,000,000 GYM
- Deployed On: June 2024
- Owner Address: 0xdf4D45FbAa4EC85e5A19d8af327671B9B462EEcE
You can interact with the verified contract directly on Etherscan:
- Visit the contract page on Sepolia Etherscan
- Go to the "Contract" tab and select "Read Contract" to view token information
- Go to "Write Contract" to interact with functions like
transfer,approve, etc.
Create custom scripts for specific interactions. For example, to check your token balance:
// scripts/checkBalance.ts
const hre = require("hardhat");
async function main() {
const tokenAddress = "0x5dbB770Daa57c7f345E1e55024F0f06247f89682";
const accountToCheck = "YOUR_ADDRESS_HERE";
const token = await hre.ethers.getContractAt("GymToken", tokenAddress);
const balance = await token.balanceOf(accountToCheck);
console.log(`GYM Token Balance: ${hre.ethers.formatUnits(balance, 18)} GYM`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Run the script with:
npx hardhat run scripts/checkBalance.ts --network sepoliaIf you experience RPC connection issues, you can test multiple endpoints with the provided script:
npx ts-node scripts/findWorkingRpc.tsThis script will:
1. Test the configured RPC endpoint in your `.env` file
2. Try alternative endpoints if the configured one fails
3. Update your `.env` file with a working endpoint
4. Check your wallet balance
## β οΈ Troubleshooting
### Common Issues
#### RPC Connection Problems
```plaintext
Error: could not detect network (event="noNetwork", code=NETWORK_ERROR, version=providers/5.7.2)Solution:
- Try changing the RPC URL in your
.envfile - Run
npx ts-node scripts/findWorkingRpc.tsto find a working endpoint - Increase the timeout in your
hardhat.config.tsfile
Error: insufficient funds for intrinsic transaction cost
Solution:
- Get testnet ETH from a Sepolia faucet
- Check your balance with
npx hardhat run scripts/checkBalance.ts --network sepolia
The constructor arguments don't match
Solution:
- Ensure you're passing the correct constructor arguments
- For GymToken:
npx hardhat verify --network sepolia <CONTRACT_ADDRESS> 1000000000
β
GymToken deployed at: 0x5FbDB2315678afecb367f032d93F642f64180aa3
You can interact with the deployed contract using Hardhat console:
npx hardhat console --network hardhatExample interactions:
// Get contract factory
const Token = await ethers.getContractFactory("GymToken");
// Connect to deployed contract (replace with actual address)
const token = await Token.attach("0x5FbDB2315678afecb367f032d93F642f64180aa3");
// Check token details
await token.name(); // "Gym Token"
await token.symbol(); // "GYM"
await token.totalSupply(); // 1000000000000000000000000000 (1B * 10^18)The project is configured to work with multiple networks. Currently set up for:
- Hardhat Network: Local development network (default)
- Localhost: Local Ethereum node (port 8545)
To add testnets or mainnet, update hardhat.config.ts:
networks: {
sepolia: {
url: "https://sepolia.infura.io/v3/YOUR-PROJECT-ID",
accounts: ["YOUR-PRIVATE-KEY"]
}
}Then deploy with:
npx hardhat run scripts/deploy.ts --network sepoliacontract GymToken is ERC20 {
constructor(uint256 initialSupply) ERC20("Gym Token", "GYM") {
_mint(msg.sender, initialSupply * 10 ** decimals());
}
}Key Features:
- Inherits from OpenZeppelin's ERC20 implementation
- Mints entire supply to contract deployer
- No additional minting functionality (fixed supply)
- Standard transfer and approval mechanisms
- Initial Supply: 1,000,000,000 tokens (specified in deploy.ts)
- Deployed to: Contract deployer's address
- Gas Optimization: Compiler optimization enabled
Edit contracts/Token.sol for any modifications
npx hardhat compilenpx hardhat run scripts/deploy.ts --network hardhatUse Hardhat console or create test scripts to interact with your contract
Edit contracts/Token.sol:
constructor(uint256 initialSupply) ERC20("Your Token Name", "SYMBOL") {
_mint(msg.sender, initialSupply * 10 ** decimals());
}Edit scripts/deploy.ts:
const initialSupply = 5000000000; // 5 billion tokensConsider adding:
- Minting: Allow new token creation
- Burning: Token destruction functionality
- Pausing: Emergency stop functionality
- Access Control: Admin roles and permissions
- @openzeppelin/contracts: Secure, audited smart contract library
- hardhat: Development environment and task runner
- ethers: Ethereum library for JavaScript/TypeScript
- typescript: Type safety and modern JavaScript features
1. Compilation Errors
npx hardhat clean
npx hardhat compile2. Deployment Failures
- Check network connectivity
- Ensure sufficient ETH for gas fees
- Verify contract constructor parameters
3. Type Errors
- Ensure TypeScript dependencies are installed
- Check import paths and contract names
- Check Hardhat Documentation
- Review OpenZeppelin Docs
- Examine error messages carefully
This project is licensed under the ISC License.
