Ethereum, Smart Contracts, and Decentralized Applications

This is module 5 of 10 for the Blockchain and Cryptocurrency course.

Module 4 << | >> Module 6

Readings

Ethereum

Ether (ETH)

Smart Contracts

Decentralized Applications (Dapps)

+

Self-Directed Learning

  • Buy some ETH. Ethereum is the leading blockchain for smart contracts and you will need some ETH to interact with Ethereum smart contracts. Buying $15-$20 worth of ETH the same seller as in module 1 should be sufficient.
  • Transfer some ETH into your digital wallet. Metamask is an Ethereum wallet, so this is the same process we followed before for transferring funds from a centralized exchange. Simply send funds to your address on the Ethereum blockchain.
  • Explore the range of decentralized applications. There are dapps in the arts, games, finance, etc. Your wallet may have a form of browser for you to explore the possibilities. Maybe you can look into a dapp that would have some practical use and one that would be just for fun.
  • Use a decentralized application. Wallets are intended to interact with decentralized applications. You can think of your wallet as being a single log-in mechanism for every dapp, rather than requiring the unique logins in every centralized application. Use your wallet to do some things on several dapps. For most dapps, this will require some ETH to interact with the dapp.

+

Online Discussion of Module Topic

Write down what you are learning and questions that you may already have about smart contracts and decentralized applications. Here are some prompts to get you thinking:

  • Try to explain what Ethereum is and how it works in your own words.
  • Explain what a smart contract is in your own words.
  • Can you find an example of a transaction on a blockchain explorer (like Etherscan) where the “To:” (recipient) is a “Contract”?
  • Explain what a decentralized application is in your own words. How do they differ from the apps on your phone?
  • How do decentralized applications use smart contracts?
  • What was your experience in using a decentralized application?
  • How did you use ETH and your wallet to interact with the decentralized application?
  • How are your interactions with the decentralized application reflected on Etherscan?
  • Are smart contracts legally enforceable?

As you begin developing a dapp, here are some additional prompts to consider:

  • What makes for a good dapp?
  • When is a service best done with a decentralized app rather than a centralized app?
  • How could you use the basic structure of an existing dapp and make a modification?
  • What is the business case (the value proposition) for your dapp idea?
  • Describe the target audience for your proposed dapp and the benefit that your dapp would provide to this group of users.
  • How would you go about starting to build a proof-of-concept?
  • What types of perspectives are important for a team that is working together on developing a dapp? How do you combine the business and technical aspects?

+

Technical exercise

The technical exercise for Module 5 introduces you to another tool used to build Ethereum apps, called Hardhat (https://hardhat.org). Hardhat is an Ethereum development environment for professionals. One advantage of Hardhat is that it allows you to set up a built-in local network called the “Hardhat Network”.  For this module, you will be working through the first half of the Hardhat tutorial (Steps 1-5). In the next module, you will complete the tutorial and learn how to deploy to the Goerli Testnet by using your Alchemy API key and Metamask Private Key from Modules 2 and 3. 

Step 1. Open the Hardhat tutorial (https://hardhat.org/tutorial/).

Step 2. Read through steps 1 and 2 of the Hardhat tutorial. Note how all the things listed for being able to follow the tutorial are things that we are doing in the course! For setting up the environment, you should already have Node.js installed from Module 1. However, if you are a Windows user, note the strong recommendation about using Windows Subsystem for Linux. This will come up in the next step.

Step 3. Complete step 3 of the Hardhat tutorial. During the installation of hardhat, just hit enter whenever there is a default option presented. Step 3 of the tutorial should be straightforward if you are on a Mac. If you are on a Windows machine, it will be more challenging. If you are a Windows user and the npx hardhat command does not work, follow the two steps below and then start again with npm init in Ubuntu. The npx hardhat command should work in Ubuntu. You will get some deprecated warnings when you install the hardhat toolbox and that is OK. For the last part of Step 3, just open the hardhat.config.js file in your directory using a code editor and paste in the highlighted line on line 1.

**Windows Step 1: You will need to install Windows Subsystem for Linux (WSL) to create a new Hardhat project. Start by following the first set of instructions on Microsoft’s Install Linux on Windows with WSL page. When you run “wsl --install” in the Windows Command Prompt, it will then explain the next command to install Ubuntu as a distribution of Linux. Continuing through the Microsoft instructions and installing WSL 2 is optional. Once you have Ubuntu installed, you can restart your computer and open the Ubuntu application.

**Windows Step 2: Next open the instructions on Microsoft’s Install Node.js on Windows Subsystem for Linux (WSL2) page. You can skip down to the instructions in the section “Install nvm, node.js, and npm.” Follow the steps to install cURL and nvm. Once you have nvm installed, you install Node.js and npm (together) by running the command “nvm install --lts“, which will install the latest version of the software with long time support. Note that you likely did this on GitBash in Module 1. Here, you are installing Node.js and npm on Ubuntu. You can run the next steps to confirm the installation or stop here and return to the Hardhat tutorial. Once you have Node.js and npm, you should navigate to your hardhat-tutorial directory from within Ubuntu. You may need to start with cd /mnt/c to get to your C: drive. Once you are in your tutorial directory, start with the npm init command in step 3 of the tutorial and continue again from there.

Step 4. Complete step 4 in the Hardhat tutorial on “Writing and compiling contracts.” This step in the tutorial introduces the concept of smart contracts and the Solidity programming language. The contracts directory you create should be in your hardhat tutorial directory. You’ll be writing a basic Solidity Smart Contract to create your own “My Hardhat Token”.  You are encouraged to change the Solidity code in the following way to make it your own:

  • change the token name
  • change the token symbol
  • change the total supply.

While working on this step, you should refer to these two recommended readings for additional explanation:

Step 5. Complete step 5 in the Hardhat tutorial on “Testing contracts.” In this step, you will be writing and running some basic tests to see if your smart contract code is working correctly. If you run the npx hardhat test command and get the error message, Error: Timeout of 40000ms exceeded, then you will need to extent the timeout limit in the Step 5 code. Add the following timeout line to your Token.js file so that it looks like the following:

This will extend the Timeout limit from 40000ms to 1000000ms, which should be sufficient. If you still get an error, you can increase this further. Your goal is to run the first code example and get one successfully passed test.

Once you’ve completed the first test, move your Token.js file out of your test directory and into your hardhat-tutorial directory. Then follow the tutorial’s instructions under “Full Coverage” for copying the code into a new, longer Token.js file in your test directory. If you had a timeout issue previously, you should add a line again here:

Completing this step of the tutorial should result in 5 successful tests: 2 for deployment and 3 for transactions.

Step 6. Take a screenshot of your terminal output after running `npx hardhat test` and post your screenshot in the D2L discussion.