Skip to main content

Creating your Contract

In this guide, we will take you through the entire process, step-by-step to create and launch your smart contract.

Once completed, you will have a smart contract address that essentially acts as a bank and mints new ERC-20 tokens when users send Ether to the said contract address.

Getting Started​

To get started, please follow the steps below:

Cloning the Repository​

  1. Please ensure that you have Git installed. If not, please download and install it from here.

  2. Once installed, open up a terminal window and clone the repository with the following command:

git clone https://github.com/realstorypro/the-media-dao.git
  1. Git will download the entire repository to your local machine. Once the clone is complete, you can navigate into the repository directory and start working with the files.

Installation​

  1. Install the dependencies with NPM install.
npm install
  1. Compile the contract with
npx hardhat compile
  1. Run Tests with
REPORT_GAS=true npx hardhat test --network hardhat

Customization​

  1. Navigate to contracts directory and open the file named Token.sol 
  2. Change the token name and symbol by editing the following line and replacing Token and TKN with the values of your choosing.
constructor(uint256 initialMint, uint256 initialPrice) ERC20("Token", "TKN") {
  1. Open up scripts/deploy.ts and change the following line to suit your requirements.
const token = await Token.deploy(100, ethers.utils.parseEther("0.05"));

The first number represents the number of tokens to be sent to the contract deployer (pre-mint). The second number represents the initial token price when purchased form a Bank (0.05 ether).

  1. Open up scripts/etherscan-arguments.js and modify the arguments to match the values from step 3. Note that the second value is in Wei.
module.exports = [
100,
50000000000000000n
]

Deployment​

In this example we will be deploying to Sepolia Testnet.

Getting the API Keys​

Alchemy​

  1. Signup for a free Alchemy account.
  2. On the menubar, click Apps -> Create App.
  3. Ensure that the new app you are creating is using Sepolia network.
Screenshot of Sepolia Create App Screen
  1. After the app has been created click on the View Keys button located underneath the main navigation.
Screenshot of Alchemy Keys Screen
  1. Create .env file in the root directory and populate it with the API_KEY with value from API_KEY and API_URL with the value from the HTTPS section.
    API_KEY = "API_KEY" 
API_URL = "https://eth-sepolia.g.alchemy.com/v2/API_KEY"

Etherscan​

  1. Visit etherscan.io and signup for a free account.
  2. Once logged in, press your username on the top right, and select the My profile button.
  3. Click on the API-KEY button on the left hand side and then press the Add button.
Screenshot of Etherscan Keys Screen
  1. Add the API key to the .env file you've created in the previous step.
ETHERSCAN_API_KEY = "YOUR_API_KEY"

Deploying to Blockchain​

Run the following command to deploy the smart contract to Sepolia Testnet.

npx hardhat run scripts/deploy.ts --network sepolia

If successful this will return deployed CONTRACT ADDRESS.

Deployed to 0x562B9B7BE96E1687DA93589db0568d80Ec0dADB6

Upload to Etherscan​

The final step is to upload the contract to etherscan so people can read the actual code. Replace CONTRACT_ADDRESS with the contract address from the previous step and run the following command.

npx hardhat verify --constructor-args scripts/etherscan-arguments.js  --network sepolia CONTRACT_ADDRESS

Congratulations! You have successfully created and launched a smart contract