Table of Contents
Introduction
Getting Our Feet Wet
Create Your First Smart Contract
ThePrimeagen creates the project folder and installs hardhat which is the node module used to compile the contracts. The first Hello World contract is coded and compiled. Some common compile errors are also discussed in this segment.Setup & Deploy to Test Network
ThePrimeagen installs the necessary testing dependencies and writes the unit test for the HelloWorld contract. The test checks to make sure the hello() method in the contract returns the string "Hello World".Create a Deployment Script
ThePrimeagen uses hardhat to launch a local node-based network. Then a deployment script is written to deploy the contract to the local network. Twenty default accounts are auto-generated on the network for hardhat to use for any transactions. A question about the generation of the private keys is also covered in this segment.Setup MetaMask Extension
ThePrimeagen installs the MetaMask browser extension. When the private key is imported and the localhost test network is selected, the extension will show the current Ethereum balance and track any activity.Deploy Contract & Connect MetaMask
ThePrimeagen codes a frontend to access the test network through the web browser. To access the contract, the page is supplied an address, and interface, and a provider. The provider is an ethereum object added to the window object by the MetaMask extension.Web3 Terminology
ThePrimeagen explains some common terminology like Web3, Ethereum, provider, contract, and wallet. The ethers library is abstracting the communication between the application and the network. The hardhat library provides the compiling, testing and deployment support.Solidity Language Basics
ThePrimeagen introduces the Solidity language and shares some common code structures like conditions, loops, and type declarations. Contracts are similar to classes. The base type in Solidity is a 256-bit unsigned integer.
Solidity Fundamentals
Counter Contract
ThePrimeagen creates a counter contract as well as a deploy script. Unlike the hello world example, this contract changes state. The change in state creates a new block and triggers a gas fee.View Methods vs. Transactional Methods
ThePrimeagen modifies the contract so there are separate methods for viewing the count and mutating the count. This best-practice allows method calls to occur without gas fees or verification across the blockchain. Working with larger numbers than JavaScript can support is also covered in this segment.Display Contract Count on Website
ThePrimeagen refactors the website to display the count using the new view method and adds an increment button. The contract's address is moved into an environment variable to make it more reusable.Signers & Waiting for Transactions
ThePrimeagen adds a signer to the application which provides state-changing privileges. A signer is required because a provider only specifies how the contract can be accessessed and cannot change state. Since a transaction is asynchronous, the application needs to wait until the transaction is complete before updating the UI. Debugging NONCE errors is also covered in this segment.Contract Events
ThePrimeagen adds a CounterInc event to the contract. When the counter is updated, the event is emitted. Any connected clients will have the event pushed to them. Additional filters like the address of the sender can be included as well.Alchemy Test Network
ThePrimeagen demonstrates how Alchemy is used to deploy the contract to a live test network. In this case, the test network is Rinkeby. An Alchemy API endpoint is specified in the hardhat.config.ts file. MetaMask then needs an authorized account for Rinkeby.Gas Overview
ThePrimeagen explains that the term "gas" refers to the transaction cost. The wei is the fundamental unit of ether. The gwei, or Gigawei is the typical measurement for gas.Gas Reporter
ThePrimeagen demonstrates how to use the hardhat-gas-reporter module to estimate gas fees for a contract. The module is included in the hardhat config file. After the contract is created, a test script is written to call methods in the contract. The gas report is displayed in the console.
The Game
Game Contract Setup
ThePrimeagen begins building a basic RPG game and introduces some additional language features of Solidity. The contract is created and uses the enum type to manage the different player types. The payment modifier is added to any function that requires a monetary payment to be included with the function call.Game Contract Storage
ThePrimeagen uses a mapping to store the heroes generated by the user. The mapping uses the address of the user as the key. The address will point to a list of uint values.Generating Random Numbers
ThePrimeagen uses a random number generator to select a player attribute and assign a value. The attribute is then pushed into the addressToHeroes array. Issues with generating random numbers on the same block chain are also discussed in this segment.Bits & Bit Shifting
ThePrimeagen explains the differences between base10, base16, and base2 numbers. The left shift operator (<<) shifts the bits in a binary number to the left by adding zeros to the right of the number. It's the binary equivalent of multiplying a decimal number by 10.Logical Operations on Bits
ThePrimeagen demonstrates how logical operators can be used to swap values or determine if a variable contains a certain value.Reading from Storage Methods
ThePrimeagen creates helper functions for reading the player attributes from memory. Since the attribute values are stored at specific byte locations within the mapping, the helper function left shifts to correct position and use the logical AND (&) operator to return the value.Testing Storage Methods
ThePrimeagen creates tests for a few player attributes. Since the attributes are randomly generated, the Hero contract is extended and the generateRandom method is overridden to return a value set by the test script.Deploying Contract
ThePrimeagen shares a couple of the constraints around deploying contracts. There is a size limit of 24Kb and when a contract is updated, it gets a new address. Any previous transactions are associated with the old contract.
Diamond Pattern
Diamond Pattern Fallbacks
ThePrimeagen explains how the Diamond Pattern helps address contracts that are too large and scenarios where a contract needs to be updated. An external fallback method is created to handle any calls to methods that do not exist. In order for the fallback to work, non-existent methods should be defined in an interface.Diamond Pattern Delegate Calls
ThePrimeagen creates a delegate function that can call a method in another contract using the context of the current contract. This is similar to using the call() method in JavaScript. The data remains in the diamond contract and the external address does not change.Diamond Pattern Storage Overview
ThePrimeagen demonstrates how memory is stored within the diamond pattern. Solidity attempts to pack bits as tight as possible. They will be added to storage in the order they are declared. Variables will share adjacent slots if they are small enough.Diamond Pattern App Storage
ThePrimeagen creates an AppStorage struct to act as a common location for storing application data. The only issue with this storage solution is it has the potential to write into an already allocated storage block. The solution is to use assembly to select a random storage location.Diamond Contract Walkthrough
ThePrimeagen clones a repository containing a diamond pattern template and walks through a more complex use case. Within the diamond pattern, a facet refers to a contract within the diamond. When a facet is cut, it is either added, removed, or changed. An init method is used to initialize a contract with the diamond's storage location rather than the individual contracts storage location.Replacing Functions from the Diamond
ThePrimeagen replaces a function in contract A with a new return value. After the replacement, the function can still be called at the same address by using the diamond. If a function is removed, it can still be called using the diamond but an error will be thrown indicating the function no longer exists.Q&A
ThePrimeagen answers questions about mutability in the blockchain, deploying new contracts, alternatives to the diamond pattern, and the public nature of code on the block chain.