Updated docs for typescript
This commit is contained in:
parent
a615443abe
commit
0dcdd3b34d
@ -13,7 +13,9 @@
|
|||||||
```bash
|
```bash
|
||||||
npm init -y
|
npm init -y
|
||||||
npm install --save-dev hardhat
|
npm install --save-dev hardhat
|
||||||
npx hardhat
|
npx hardhat init # you can pick whatever you'd like, I like typescript
|
||||||
|
npm install ethers @openzeppelin/contracts
|
||||||
|
npm install --save-dev @nomicfoundation/hardhat-toolbox dotenv # To not hardcode your wallet's private key
|
||||||
```
|
```
|
||||||
|
|
||||||
> What's Hardhat?
|
> What's Hardhat?
|
||||||
|
@ -1,22 +1,84 @@
|
|||||||
|
|
||||||
- [Compiling and deploying](#compiling-and-deploying)
|
- [Compiling and deploying](#compiling-and-deploying)
|
||||||
|
- [The contract](#the-contract)
|
||||||
|
- [Configuring Hardhat](#configuring-hardhat)
|
||||||
|
- [Deployment](#deployment)
|
||||||
- [Interacting with the contract](#interacting-with-the-contract)
|
- [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.
|
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
|
## Compiling and deploying
|
||||||
First, modify `scripts/deploy.js` in the Hardhat project:
|
|
||||||
|
|
||||||
```js
|
### The contract
|
||||||
const hre = require("hardhat");
|
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";
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const initialSupply = hre.ethers.parseUnits("1000", 18); // 1000 MTK tokens
|
const initialSupply = ethers.parseUnits("1000", 18); // 1000 tokens
|
||||||
const MyToken = await hre.ethers.deployContract("MyToken", [initialSupply]);
|
const whateverCoin = await ethers.deployContract("WhateverYouNamedIt", [initialSupply]);
|
||||||
await MyToken.waitForDeployment();
|
await whateverCoin.waitForDeployment();
|
||||||
|
|
||||||
console.log(`MyToken deployed to: ${MyToken.target}`);
|
console.log(`WhateverYouNamedIt deployed to: ${await whateverCoin.getAddress()}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
main().catch((error) => {
|
main().catch((error) => {
|
||||||
@ -25,16 +87,18 @@ main().catch((error) => {
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
then, deploy like this:
|
Then, run the deploy script:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npx hardhat run scripts/deploy.js --network sepolia
|
npx hardhat run scripts/deploy.ts --network sepolia
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## Interacting with the contract
|
## Interacting with the contract
|
||||||
|
|
||||||
You can see it on your favorite block explorer (e.g., [Etherscan](https://etherscan.io/)) by pasting the contract address.
|
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 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:
|
You can also interact with it programmatically using the Hardhat console:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user