3c/doc/02_minimal.md

112 lines
3.5 KiB
Markdown
Raw Normal View History

2025-02-10 18:37:19 +01:00
- [Compiling and deploying](#compiling-and-deploying)
2025-02-10 20:06:07 +01:00
- [The contract](#the-contract)
- [Configuring Hardhat](#configuring-hardhat)
- [Deployment](#deployment)
2025-02-10 18:37:19 +01:00
- [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
2025-02-10 20:06:07 +01:00
### 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=<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:
```typescript
import { ethers } from "hardhat";
2025-02-10 18:37:19 +01:00
async function main() {
2025-02-10 20:06:07 +01:00
const initialSupply = ethers.parseUnits("1000", 18); // 1000 tokens
const whateverCoin = await ethers.deployContract("WhateverYouNamedIt", [initialSupply]);
await whateverCoin.waitForDeployment();
2025-02-10 18:37:19 +01:00
2025-02-10 20:06:07 +01:00
console.log(`WhateverYouNamedIt deployed to: ${await whateverCoin.getAddress()}`);
2025-02-10 18:37:19 +01:00
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
```
2025-02-10 20:06:07 +01:00
Then, run the deploy script:
2025-02-10 18:37:19 +01:00
```bash
2025-02-10 20:06:07 +01:00
npx hardhat run scripts/deploy.ts --network sepolia
2025-02-10 18:37:19 +01:00
```
2025-02-10 20:06:07 +01:00
2025-02-10 18:37:19 +01:00
## Interacting with the contract
2025-02-10 20:06:07 +01:00
You can see it on your favorite block explorer (e.g., [Etherscan (sepolia)](https://sepolia.etherscan.io/)) by pasting the contract address.
2025-02-10 18:37:19 +01:00
2025-02-10 20:06:07 +01:00
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.
2025-02-10 18:37:19 +01:00
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();
```