You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/lessons/projects/3.mdx
+27-12Lines changed: 27 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,7 @@ import { LessonHeader } from '../../../components/mdx/LessonHeader'
22
22
<LessonHeadertitle="Lesson 3: Tier NFTs" />
23
23
24
24
## What are we building?
25
+
PART ONE: INTRO AND CONTENTS
25
26
26
27
We are going to build on-chain NFTs, that have different Tiers and prices.
27
28
@@ -33,13 +34,15 @@ of access depending on the subscription. We want to let users access different
33
34
services depending on the NFT they mint and own.
34
35
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?
35
36
37
+
WARM-UP QUESTIONS ⁉️
36
38

37
39
40
+
PART 2 ASSERTIONS AND DATA TYPES e.g. MAPPINGS
38
41
## First things first 👷♂️
39
42
40
43
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.
43
46
44
47
If you’ve done our previous lesson, it’s the exact same process. Make a note of
45
48
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.
132
135
133
136
### Let’s start coding!
134
137
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
136
139
`code .` in my terminal.
137
140
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:
140
143
141
144
```solidity
142
145
// SPDX-License-Identifier: MIT
@@ -146,8 +149,8 @@ contract TierNFT {
146
149
}
147
150
```
148
151
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.
151
154
152
155
## What do we need?
153
156
### we sort of need this 'intro' at the start of the lesson......
@@ -200,7 +203,7 @@ contract TierNFT is ERC721 {
200
203
201
204
### Adding Tiers and Using Them in the Mint Function
202
205
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.
204
207
205
208
```solidity
206
209
// SPDX-License-Identifier: MIT
@@ -233,12 +236,17 @@ contract TierNFT is ERC721 {
233
236
}
234
237
````
235
238
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.
237
240
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, youknow `if`, and youknow `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`!
239
242
240
243
```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...
242
250
243
251
function mint() public payable {
244
252
require(
@@ -259,7 +267,11 @@ Now we need to modify the mint function with logic to be able to actually access
259
267
}
260
268
```
261
269
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`.
263
275
264
276
<br/>
265
277
<details>
@@ -306,6 +318,9 @@ contract TierNFT is ERC721 {
306
318
</details>
307
319
<br/>
308
320
321
+
CHECKPOINT QUESTIONS ⁉️ <br />
322
+
PART 3
323
+
309
324
### Create a (simple) tokenURI function
310
325
311
326
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