Skip to content

Commit 009e608

Browse files
committed
Add educational text re: mappings, require + general improvements
1 parent 5a66104 commit 009e608

File tree

1 file changed

+27
-12
lines changed
  • pages/lessons/projects

1 file changed

+27
-12
lines changed

pages/lessons/projects/3.mdx

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { LessonHeader } from '../../../components/mdx/LessonHeader'
2222
<LessonHeader title="Lesson 3: Tier NFTs" />
2323

2424
## What are we building?
25+
PART ONE: INTRO AND CONTENTS
2526

2627
We are going to build on-chain NFTs, that have different Tiers and prices.
2728

@@ -33,13 +34,15 @@ of access depending on the subscription. We want to let users access different
3334
services depending on the NFT they mint and own.
3435
I'M NOT AT ALL SURE WHAT WE ARE BUILDING..... MAKE SURE WE ADD THAT WE'LL BE INHERITING A FEW MORE OZ CONTRACTS ThIS TIME. MAYBE A NUDGE TO LEARNER TO READ SOME OF THE OZ CODE?
3536

37+
WARM-UP QUESTIONS ⁉️
3638
![1_diagram.png](/assets/lessons/3/1_diagram.png)
3739

40+
PART 2 ASSERTIONS AND DATA TYPES e.g. MAPPINGS
3841
## First things first 👷‍♂️
3942

4043
Before we start coding, we need to create our project template. We are going to follow
41-
the same steps as in previous lessons. Using our package manager (npm, yarn,
42-
etc)WE ARE ONLY USING ONE we create a Hardhat project and remove unnecessary files.
44+
the same steps as in previous lessons. Using `npm` as our package manager, we'll create
45+
a Hardhat project and remove the default files we don't need.
4346

4447
If you’ve done our previous lesson, it’s the exact same process. Make a note of
4548
remembering these steps and what they do, for we will use them a lot in
@@ -132,11 +135,11 @@ widely used and fully tested and audited.
132135
133136
### Let’s start coding!
134137
135-
Fire up your code editor and let’s start hacking. I’m using VSCode, so I run
138+
Fire up your code editor and let’s start hacking. I’m using VSCode, so I just need to run
136139
`code .` in my terminal.
137140
138-
Let’s create an empty file named `TierNFT.sol` inside the `contracts/` folder
139-
and following the contract and file naming convention, create a contract of the same name:
141+
Let’s create an empty file named `TierNFT.sol` inside the `contracts/` folder,
142+
and by following the contract and file naming convention, create a contract of the same name:
140143
141144
```solidity
142145
// SPDX-License-Identifier: MIT
@@ -146,8 +149,8 @@ contract TierNFT {
146149
}
147150
```
148151
149-
Now that we have our license, the version of Solidity we are using and the
150-
contract set up, we can add the logic and the variables we need to store.
152+
Now that we have our license, Solidity version and the
153+
contract defined, we can add the logic and the variables we need to store.
151154
152155
## What do we need?
153156
### we sort of need this 'intro' at the start of the lesson......
@@ -200,7 +203,7 @@ contract TierNFT is ERC721 {
200203
201204
### Adding Tiers and Using Them in the Mint Function
202205
203-
Now to make our code neat and easily readable, just before the contract declaration, add the tiers, and assign their values, where each one represents a service subscription. Note that we assign the `constant` keyword, which is what it sounds like - the values won't change, which in turn cuts down a lot on gas costs. Always good to know, but we'll go into 'gas optimisation' for you in a future lesson.
206+
Next, to make our code neat and easily readable, just before the contract declaration, add the tiers, and assign their values, where each one represents a service subscription. Note that we assign the `constant` keyword, which is what it sounds like, meaning the values won't change, which also cuts down a lot on gas costs. Always good to know, but we'll go into 'gas optimisation' for you in a future lesson.
204207
205208
```solidity
206209
// SPDX-License-Identifier: MIT
@@ -233,12 +236,17 @@ contract TierNFT is ERC721 {
233236
}
234237
````
235238
236-
We have added `Basic`, `Medium` and `Premium` as tiers and assigned their values. We store which tier each NFT holds in `mapping(uint256 => uint256) public tokenTier;`. Mappings are widely used in Solidity, having many advantages. For example, they are a great way to store batches of data and access them efficiently and therefore cheaply through 'key: value' pairs.
239+
We have added `Basic`, `Medium` and `Premium` as tiers and assigned their values. We store the tier each NFT holds in `mapping(uint256 => uint256) public tokenTier;`. Mappings are widely used in Solidity, having many advantages. They are a great way to organise and access data efficiently, and therefore cheaply through 'key: value' pairs e.g. an address to a balance. We can also use nested mappings to allow for more complex data structures.
237240
238-
Now we need to modify the mint function with logic to be able to actually access the three tiers of the NFTs separately. We use a 'conditional' `if/else if` statement to achieve this. If you know Javascript, you know `if`, and you know `else`!
241+
Now we need to modify the mint function with logic that can access the three NFT tiers separately. We use a 'conditional' `if/else if` statement to achieve this. If you've done any coding before now, you'll probably know `if`, and if so, you'll definitely know `if else`!
239242

240243
```solidity
241-
// constructor part of the code...
244+
// state variables and contract definition...
245+
246+
uint256 public totalSupply;
247+
mapping(uint256 => uint256) public tokenTier;
248+
249+
// constructor...
242250

243251
function mint() public payable {
244252
require(
@@ -259,7 +267,11 @@ Now we need to modify the mint function with logic to be able to actually access
259267
}
260268
```
261269

262-
The mint function selects tiers based on the amount of native token value it receives. If it doesn't get enough for tier 0,it will give a message telling us, "Not enough value for the minimum Tier". Otherwise we can select the tier we want..... as long as we can afford it!
270+
The mint function selects tiers based on the amount of native token it receives stored in `msg.value`.
271+
272+
The `require` statement is a simple and powerful function modifier you'll be using a lot in the future, which also helps ensure code we write is 'predictable'. In our case, if we give the function zero token value for TIER_VALUE_0, it will 'revert' with a message telling us, "Not enough value for the minimum Tier", and stop executing. Otherwise we can select the tier we want..... as long as we can afford it!
273+
274+
We already have two `uint256` variables declared. You might wonder which one is which inside the `tokenTier` mapping? See if you can track through the code and find out - DEFO NEED A QUESTION OR TWO ON THIS - TO GET THAT ANSWER! AND ALSO TO CHECK ON STATE AND LOCAL VARIABLES e.g. `tierId` is declared with `0`.
263275

264276
<br/>
265277
<details>
@@ -306,6 +318,9 @@ contract TierNFT is ERC721 {
306318
</details>
307319
<br/>
308320
321+
CHECKPOINT QUESTIONS ⁉️ <br />
322+
PART 3
323+
309324
### Create a (simple) tokenURI function
310325
311326
When we inherited Open Zeppelin's ERC721, it gave us a function for `tokenURI` where we can store an image, or a video, or much more. With the help of this ERC721 contract we have the ability to define **a base path** for creating an

0 commit comments

Comments
 (0)