This project is a demonstration application showcasing how to build a decentralized application (DApp) that interacts with the Ethereum blockchain using ethers.js. It demonstrates core functionalities such as connecting to a user's wallet, displaying token information, and performing token transfers.
- Wallet Connection: Connects to a user's Ethereum wallet (e.g., MetaMask) using
ethers.js. - Network Detection: Detects the current network and prompts the user to switch to the Sepolia testnet if they are on the wrong network.
- Token Information: Fetches and displays the name and symbol of a deployed ERC-20 token contract.
- Token Balance: Retrieves and displays the user's token balance.
- Token Transfers: Allows users to transfer tokens to other Ethereum addresses.
- Transaction Handling: Handles transaction sending, waiting for confirmation, and error handling.
- Error Handling: Gracefully handles various errors, including network issues, user-rejected transactions, and contract-related errors.
- Disconnect Wallet: Allows users to disconnect their wallet from the DApp.
- Contract verification: Verify if the contract exists at the specified address.
- Add network: Add the Sepolia network to MetaMask if it's not already added.
- React: A JavaScript library for building user interfaces.
- Ethers.js: A powerful library for interacting with the Ethereum blockchain.
- ERC-20 Token: A standard interface for fungible tokens on Ethereum.
- Sepolia Testnet: An Ethereum test network used for development and testing.
- Hardhat: A development environment to compile, deploy, test, and debug your Ethereum software.
- Node.js and npm: Make sure you have Node.js and npm (or yarn) installed on your system.
- Ethereum Wallet: You'll need an Ethereum wallet like MetaMask installed in your browser.
- Sepolia Testnet ETH: You'll need some Sepolia ETH to interact with the contract. You can get some from a Sepolia faucet.
-
Clone the Repository:
git clone <your-repository-url> cd hard-hat-tutorial
-
Install Dependencies:
cd frontend yarn install -
Environment Variables:
- Create a
.envfile in the root of the project. - Add the following variable:
NETWORK_ID=11155111 - You can change the
NETWORK_IDto another network if you want to deploy to another network.
- Create a
-
Start the Development Server:
yarn start
This will start the React development server, and you can access the DApp in your browser at
http://localhost:3000.
-
Deploy the Token Contract:
-
Navigate to the root of the project.
-
Deploy the
Tokencontract using Hardhat:npx hardhat run scripts/deploy.js --network sepolia
-
Note: You'll need to configure your Hardhat project with your Sepolia RPC URL and private key.
-
Note: You can change the network to another network if you want to deploy to another network.
-
-
Update the
TOKENConstant:-
After deploying the
Tokencontract, copy the deployed contract address. -
Open
frontend/src/components/Dapp.js. -
Replace the value of the
TOKENconstant with the deployed contract address:const TOKEN = '0x<your-deployed-token-contract-address>';
-
-
Copy the files:
- Copy the
Token.jsonfile fromartifacts/contractstofrontend/src/contracts. - Copy the
ContractAddress.jsonfile fromartifacts/contractstofrontend/src/contracts.
- Copy the
- Connect Wallet: Click the "Connect Wallet" button to connect your Ethereum wallet.
- View Token Information: Once connected, the DApp will display the token's name, symbol, and your balance.
- Transfer Tokens: Use the transfer form to send tokens to another Ethereum address.
- Disconnect Wallet: Click the "Disconnect Wallet" button to disconnect your wallet.
frontend/src/components/Dapp.js: The main component that handles wallet connection, contract interaction, and UI rendering.frontend/src/components/ConnectWallet.js: The component that displays the "Connect Wallet" button.frontend/src/components/Loading.js: The component that displays a loading message.frontend/src/components/NoWalletDetected.js: The component that displays a message if no wallet is detected.frontend/src/components/Transfer.js: The component that displays the token transfer form.frontend/src/components/TransactionErrorMessage.js: The component that displays transaction error messages.frontend/src/components/WaitingForTransactionMessage.js: The component that displays a message while waiting for a transaction to be mined.frontend/src/components/NoTokensMessage.js: The component that displays a message when the user has no tokens.frontend/src/contracts/Token.json: The ABI of theTokencontract.frontend/src/contracts/ContractAddress.json: The ABI of theContractAddresscontract.scripts/deploy.js: The script to deploy the contract.
ethers.js:BrowserProvider: Connecting to a browser-injected Ethereum provider.Contract: Interacting with smart contracts.getSigner: Getting a signer to send transactions.formatEther: Formatting Ether values for display.parseEther: Parsing Ether values from string.Network: Creating a custom network.
- React Hooks:
useState: Managing component state.useEffect: Handling side effects.useCallback: Memoizing functions.
- Ethereum Wallet Interaction:
eth_requestAccounts: Requesting access to user accounts.accountsChanged: Handling account changes.wallet_switchEthereumChain: Requesting network switching.wallet_addEthereumChain: Requesting to add a network.
- ERC-20 Token Interaction:
name(): Getting the token name.symbol(): Getting the token symbol.balanceOf(): Getting the token balance.transfer(): Transferring tokens.
- Contract verification: Verify if the contract exists at the specified address.
- Add network: Add the Sepolia network to MetaMask if it's not already added.
MIT