- [Compiling and deploying](#compiling-and-deploying) - [The contract](#the-contract) - [Configuring Hardhat](#configuring-hardhat) - [Deployment](#deployment) - [Interacting with the contract](#interacting-with-the-contract) This document focuses on the minimal setup required to deploy a smart contract to the Ethereum blockchain. It assumes you have already set up your development environment and have a test Ethereum wallet. ## Compiling and deploying ### The contract First, create a new contract in the `contracts` directory called `CoinCoinCoin.sol` (or whatever) and paste the following code: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract CoinCoinCoin is ERC20 { constructor(uint256 initialSupply) ERC20("CoinCoinCoin", "3CCC") { _mint(msg.sender, initialSupply * 10 ** decimals()); // Mints tokens to the contract creator } } ``` ### Configuring Hardhat Then, if using Typescript, edit the config file `hardhat.config.ts` to include the following: ```typescript import { HardhatUserConfig } from "hardhat/config"; import "@nomicfoundation/hardhat-toolbox"; import dotenv from "dotenv"; dotenv.config(); const config: HardhatUserConfig = { solidity: "0.8.20", networks: { sepolia: { url: process.env.ALCHEMY_SEPOLIA_RPC, accounts: [process.env.PRIVATE_KEY as string], }, }, etherscan: { apiKey: process.env.ETHERSCAN_API_KEY, // Optional, for contract verification }, }; export default config; ``` To explain each environment variable: - `ALCHEMY_SEPOLIA_RPC`: The RPC URL for the Sepolia test network. You can get this from [Alchemy](https://www.alchemy.com/chain-connect/endpoints/rpc-sepolia-sepolia). - `PRIVATE_KEY`: The private key of your test wallet. You can get this from MetaMask by clicking on the three dots next to your wallet and selecting "Account details". - `ETHERSCAN_API_KEY`: Your Etherscan API key. You can get this by creating an account on [Etherscan](https://etherscan.io/). Create a `.env` file in the root directory and add the following: ```plaintext ALCHEMY_SEPOLIA_RPC= PRIVATE_KEY= ETHERSCAN_API_KEY= ``` ### Deployment We need to create a deploy script in the `scripts` directory. Create a new file called `deploy.ts` and paste the following code: ```typescript import { ethers } from "hardhat"; async function main() { const initialSupply = ethers.parseUnits("1000", 18); // 1000 tokens const whateverCoin = await ethers.deployContract("WhateverYouNamedIt", [initialSupply]); await whateverCoin.waitForDeployment(); console.log(`WhateverYouNamedIt deployed to: ${await whateverCoin.getAddress()}`); } main().catch((error) => { console.error(error); process.exitCode = 1; }); ``` Then, run the deploy script: ```bash npx hardhat run scripts/deploy.ts --network sepolia ``` ## Interacting with the contract You can see it on your favorite block explorer (e.g., [Etherscan (sepolia)](https://sepolia.etherscan.io/)) by pasting the contract address. You can send it via the MetaMask extension by clicking "Send" and pasting the contract address. You have to import the contract first by clicking "Import" and pasting the contract address. You can also interact with it programmatically using the Hardhat console: ```bash npx hardhat console --network sepolia ``` ```js const MyToken = await ethers.getContract("WhateverYouNamedIt", "0xYourContractAddress"); await MyToken.totalSupply(); ```