# Deploying a custom-created cryptocurrency on the Ethereum network.

The advent of web3 has brought with it an ability to design tokens that act as a store of value or as virtual concurrency on the blockchain, the ERC20 is an Ethereum standard for creating fungible tokens, it provides the standard functionalities that should be incorporated to make it follow best practices. We'll be looking at how to set up and deploy a custom coin with the necessary attributes of an ERC20 token.

To get this done we'll need a decentralized wallet like Metamask and a web3 node provider like Alchemy. An Ethereum development environment like Truffle or hardhat plays an essential role in producing a smooth code-building experience we'll use Hardhat to write, configure and deploy our solidity code for this exercise.

### **Initializing our project**

Make sure you have Node js installed on the system, create and navigate to the token project folder and run npm init to initialize the project

```bash
mkdir PokeCoin
cd PokeCoin
npm init -y
```

### Install and create hardhat project

```bash
npm install --save-dev hardhat
npx hardhat
```

A welcome message and some options will be shown, go ahead to select ' Create a JavaScript Project'. Delete the default *Lock.sol* and *Lock.js* files in the contracts and test folders respectively.

Install the *openzeppelin* library with the below command to access the ERC20 standard that will be imported to create our token

```bash
npm i @openzeppelin/contracts
```

Under the contract folder create a file called PokeCoin.sol, this is where we'll write the smart contract that implements our PokeCoin token. The contract is quite self-explanatory, the first two lines specify the license type of the contract and the solidity compiler version to be used for the file, the ERC20 library is then imported and then immediately inherited by the PokeCoin contract, the token name and symbol are then loaded and then passed through the constructor parameters, we'll go ahead to use the base function *mint* in the ERC20 contract, this will mint the initial desired amount of token and send it to the creator's wallet.

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;

 import "@openzeppelin/contracts/token/ERC20/ERC20.sol";


contract PokeCoin is ERC20{

    uint constant _initial_supply= 100 * (10**18); // Initial deposit in creators wallet 

    constructor() ERC20("PokeCoin", "POK")  { // Token description
        
        _mint(msg.sender, _initial_supply);

    }

}
```

### **Connect Wallet and Node Provider**

We'll use Metamask as our wallet, and Alchemy as the node provider, our wallet's private key and the unique Alchemy project key, these will be stored and used in a .env file for the deployment and interaction of the contract. Follow these [steps](https://metamask.zendesk.com/hc/en-us/articles/360015289632-How-to-Export-an-Account-Private-Key) to get your Metamask private key and the below image for [Alchemy](https://www.alchemy.com/)

![HTTP Alchemy API URL](https://3169887760-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MB17w56kk7ZnRMWdqOL%2F-MUtwV_6rca7wa6qwo-C%2F-MUtx0zE2RLVnSseCfFG%2Fget%20alchemy%20api%20key.gif?alt=media&token=7cfb36de-acb4-480a-aac8-bc64dc134339 align="left")

Make sure when committing this project to your git repository that your .env file is part of the .gitignore list of files and folders as the information there are quite sensitive and could be easily used by hackers to tamper with your accounts.

Create your .*env* file with this command..

```bash
npm install dotenv --save
```

and put the necessary keys in the .env file like this

```bash
API_URL = "https://eth-goerli.g.alchemy.com/v2/your-api-key"
PRIVATE_KEY = "your-metamask-private-key"
```

### **Install Ether.js and configure deployment**

We'll use the hardhat plugin *hardhat-ethers* that is integrated with *Ethers.js* this allows for simple interactions with the Ethereum blockchain

```bash
npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
```

Then go ahead to configure the *hardhat.config.js* to require the ether library and the Goerli test network we're going to be using

```javascript
require("@nomicfoundation/hardhat-toolbox");




require('dotenv').config();
require("@nomiclabs/hardhat-ethers");

const { API_URL, PRIVATE_KEY } = process.env;

module.exports = {
  solidity: "0.8.17",
  defaultNetwork: "goerli",
  networks: {
    hardhat: {},
    goerli: {
       url: API_URL,
       accounts: [`0x${PRIVATE_KEY}`]
    }
 },
};
```

We can go ahead to compile the solidity file with the command

```bash
npx hardhat compile
```

Once this is successful, we'll configure our deployment script *deploy.js* to work like this

```javascript


async function main() {

  const [deployer]= await ethers.getSigners();
  console.log("Deploying contracts with the account: ",deployer.address);
  const weiAmount = (await deployer.getBalance()).toString();

  console.log("Account balance:", (await ethers.utils.formatEther(weiAmount)));
  const Token = await ethers.getContractFactory("Mikoin");
  const token = await Token.deploy();

  console.log("Token Address", token.address);

}

main()
.then(() => process.exit(0))
.catch((error) => {
  console.error(error);
  process.exit(1);
}
)

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
```

Go ahead to deploy your contract to the goerli testnet

```javascript
npx hardhat run scripts/deploy.js --network goerli
```

When the script is run successfully from the deployment script the console output logs the contract address, account balance, and token address. Congratulations! You can check out the record of the goerli transaction using the contract or token address using [https://goerli.etherscan.io/](https://goerli.etherscan.io/). Your custom token has also been sent to your Metamask account. To see your token, use your token's contract address to import your token.

![Import custom token MetaMask Extension](https://metamask.zendesk.com/hc/article_attachments/10106386660251 align="left")

Awesome! What other token functionalities can you implement? Tinker with your smart contract and find out!
