How to scaffold and deploy a smart contract to the Ethereum network.

How to scaffold and deploy a smart contract to the Ethereum network.

New technology can be daunting to delve in at first. Starting off with Ethereum development might have been overwhelming in the past few years, but it has never been easier to take a smart contract from your VS Code editor onto the Ethereum network (testnet or mainnet).

Practicing and mastering anything in development can be relatively less tedious once one is able to take that first step. The first step that makes you feel like you have achieved something cool. That feeling can transform into will and passion, which would then fuel the learning process all the way to senior level. Today we will take that first step together, that first feel. Thanks to tools like Hardhat, Metamask and Alchemy, the workflow of deploying smart contracts has become superfast and easy.

Before I show you how to do this, it is important you are familiar with what these tools actually are.

Hardhat

Hardhat is a development environment for building and testing Ethereum smart contracts. It allows developers to create, test and deploy smart contracts in a local environment, before deploying them to a public blockchain. This allows for faster development and more efficient testing, which is important for ensuring that contracts are secure and functioning as intended.

Included are several built-in tools and services that make it easier to work with smart contracts. It includes a local development blockchain, which allows developers to test and deploy contracts on a private network, without the need for a real-world blockchain. You also get a suite of security and testing tools that can be used to detect and fix potential issues in contracts, such as bugs and vulnerabilities.

Hardhat is an open-source project, which means that it is free to use and can be modified and distributed by anyone! It is also compatible with other Ethereum development tools and frameworks, making it easy to integrate with other parts of a developer's workflow. All these are possible with only a few commands, it is little wonder why Hardhat has become a fan favorite in the blockchain development community.

Metamask

Metamask is a browser extension and a wallet for Ethereum and other Ethereum-based blockchain networks that allows users to interact with decentralized applications (dApps) and manage their blockchain-based assets. It is a client-side software that allows users to interact with the Ethereum blockchain by connecting to a node on the network. It enables users to access the Ethereum network, check their account balance, send and receive transactions and interact with smart contracts in a user-friendly way.

Metamask also acts as a digital wallet, which means that it can store users' private keys and seed phrases, enabling them to securely manage their blockchain-based assets, such as Ether and other ERC-20 tokens.

Metamask is available as a browser extension for Chrome, Firefox, Brave and Edge, and can be easily installed on those browsers and can be used to interact with dApps and smart contracts on the Ethereum network. It also supports other blockchain networks like Binance Smart Chain and Polygon.

Alchemy

Alchemy is a blockchain infrastructure and developer platform that provides a suite of tools and services for building and deploying decentralized applications (dApps) on the Ethereum network. It enables developers to easily build and manage dApps, and provides a range of services such as analytics, monitoring, and security to help them understand the performance and usage of their dApps.

Some of the features of Alchemy include:

  • A developer portal that provides a suite of tools for building, testing, and deploying dApps, including a smart contract development framework, a local blockchain, and a set of testing and debugging tools.

  • A blockchain explorer that allows developers to view and analyze the activity on the Ethereum network, including transactions, blocks, and smart contracts.

  • A performance monitoring and analytics platform that provides real-time insights into the performance and usage of dApps, including information on user engagement, network activity, and smart contract execution.

  • A security platform that helps developers identify and mitigate potential vulnerabilities in their dApps, including smart contract auditing and automated security testing.

Alchemy is designed to make it easier for developers to build and manage dApps on the Ethereum network, and provides a range of tools and services to help them create high-quality and secure dApps. It is an open-source platform, and is freely available to use.

Prerequisites

Ensure you have node.js installed.

npm install -g npm

Ensure you have metamask installed.

Scaffolding Our Smart Contract

Step 1

Create a new folder, drag and drop it on VS Code to open. I am naming mine 'Test', feel free to name yours whatever you want.

Step 2

Open up the terminal in your VS Code or any code editor you use.

Once you have node.js installed, you can use this commands to check.

node -v
npm -v

You should have something that looks like this.

Step 3

Run

npm init

To initialize our project with the package.json file.

Run

npm install --save-dev hardhat

To install hardhat.

Now run

npx hardhat

To set up our project.

Click enter and yes (y) on all the prompts.

You should have this screen. Congratulations!

Understanding our file folder

We notice that we have a bunch of file folders created by Hardhat. What exactly are these folders? Well, they were basically created to organize different components of your smart contract.

  1. contracts/: This folder contains the Solidity smart contracts that make up your project. ( You should have a 'lock.sol' contract ready to deploy! )

  2. artifacts/ folder is also created by Hardhat and contains the compiled bytecode and ABI of the smart contracts. It is used by Hardhat and other tools to interact with the deployed smart contracts. This folder is created automatically when the contracts are compiled, and its contents are used by the migration scripts to deploy the contracts on the blockchain.

  3. migrations/: This folder contains the scripts that are used to deploy and manage the smart contracts on the blockchain.

  4. test/: This folder contains the test files that can be used to test the smart contracts.

  5. node_modules/ : This folder contains all the dependencies installed by npm.

  6. config/: This folder contains the configuration files for Hardhat and its plugins.

  7. scripts/: This folder contains the scripts that can be used to automate common tasks.

  8. .gitignore : This file contains a list of files and directories that should be ignored by git.

  9. hardhat.config.js : This file contains the configuration for the hardhat.

  10. package.json : This file contains the project's metadata, including its dependencies, scripts, and other configuration.

  11. README.md: This file contains the project's documentation.

Notice, we do not have our artifacts folder yet? Let us continue

Step 4

Run

npx hardhat compile

To compile our solidity smart contract (lock.sol)

Notice how you now have the artifact folder and our contract successfully compiled.

Run

npx hardhat test

To see that our sample test parses successfully. Hardhat is able to do this so impressively without needing to run a ganache server.

Run

npx hardhat node

This is just to see that hardhat gives us a bunch of accounts with which you can use for testing your contracts.

Type 'cntr c' to eject the node.

Let us not forget to uncomment the 'console.sol' import, so you can console log from within your solidity code.

Copy this code and paste in your 'hardhat.config.js' file

module.exports = {
  solidity: '0.8.9',
  networks: {
    goerli: {
      url: "https://eth-goerli.alchemyapi.io/v2/123abc123abc123abc123abc123abcde",
    },
  },
};

Ensure the solidity version is the same as your contract version.

You can run npx hardhat test again and our contract compiles successfully.

Step 5

Deploying to goerli test network

To do this, we would need our Alchemy api key and

Go to Alchemy and create an account. I already have one

Navigate to your dashboard and click on create app.

Fill out the fields and click on the create app button

Click on 'view key' and copy your HTTPS link.

Head over to the 'hardhat.config.js' file and paste it in the 'url' column.

Now you need some fake ETH so you can pay for gas to deploy your contract to the Ethereum network. This is because you are deploying to the testnet, on the mainnet you would need real ETH to cover gas fees.

Go to https://goerlifaucet.com/, copy and paste your Metamask goerli testnet address to the field and click on the 'Send Me ETH' button. Almost immediately, you would receive your free ETH!

We are doing some amazing things here. Okay, you are almost done.

Now you need to copy your private key from our metamask wallet.

Copy the private key and add to accounts column right under the url column in the hardhat.config.js file.

Now run

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

You should get a confirmation followed by your contract address!

You can go to etherscan to check our contract address on the Ethereum blockchain explorer.

Wow! Finally, you have your own smart contract live on the Ethereum test network, Congratulations! You did it.

There could be more complex code in our smart contract, but the process remains as smooth as this.

Furthermore...

Remember, the combination of Hardhat, Metamask, and Alchemy gives you the power to deploy smart contracts with confidence and ease. These tools provide a robust development environment, secure wallet functionality, and a powerful deployment platform. Embrace the power of this combination and take your smart contract development to the next level!

Thanks for reading, hope you learned something.