3c/doc/02_minimal.md

3.5 KiB

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:

// 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:

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.
  • 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.

Create a .env file in the root directory and add the following:

ALCHEMY_SEPOLIA_RPC=<your_rpc_url>
PRIVATE_KEY=<your_private_key>
ETHERSCAN_API_KEY=<your_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:

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:

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)) 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:

npx hardhat console --network sepolia
const MyToken = await ethers.getContract("WhateverYouNamedIt", "0xYourContractAddress");
await MyToken.totalSupply();