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
+21-22Lines changed: 21 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,21 +31,22 @@ differentiate between different levels or categories in that service. Think of s
31
31
streaming services out there such as Netflix, Disney+, etc. Some have different levels
32
32
of access depending on the subscription. We want to let users access different
33
33
services depending on the NFT they mint and own.
34
+
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?
34
35
35
36

36
37
37
38
## First things first 👷♂️
38
39
39
40
Before we start coding, we need to create our project template. We are going to follow
40
41
the same steps as in previous lessons. Using our package manager (npm, yarn,
41
-
etc) we create a Hardhat project and remove unnecessary files.
42
+
etc)WE ARE ONLY USING ONE we create a Hardhat project and remove unnecessary files.
42
43
43
44
If you’ve done our previous lesson, it’s the exact same process. Make a note of
44
45
remembering these steps and what they do, for we will use them a lot in
45
46
the future.
46
47
47
-
Let’s first open a console and cd into our `d_d_academy` folder, or create it
48
-
first if you don't have it. Then let's create a folder for our NFT project:
48
+
Let’s first open a console and `cd` into our `d_d_academy` folder, or create it
49
+
first if you don't have it. Then let's create a folder for our NFT project and make sure to `cd` into it too:
49
50
50
51
```bash
51
52
## (OPTIONAL) create a folder for our D_D Academy projects
In a Hardhat project, the default folders are supposed to be for:
105
+
106
+
- `contracts/` is where the source files for your contracts should be.
107
+
- `scripts/` is where simple automation scripts go.
108
+
- `test/` is where your tests should go.
103
109
104
-
We delete some files so we start fresh:
110
+
We want to delete some of the default files so we start fresh, e.g. we will create our own .sol file for our contract, etc:
105
111
106
112
```bash
107
113
rm contracts/*.sol
108
114
rm scripts/*.js
109
115
rm test/*.js
110
116
```
111
117
112
-
In a Hardhat project, the default folders are supposed to be for:
113
118
114
-
- `contracts/` is where the source files for your contracts should be.
115
-
- `scripts/` is where simple automation scripts go.
116
-
- `test/` is where your tests should go.
117
-
118
-
We now need to add our last dependency (OpenZeppelin contracts):
119
+
We now need to add our last dependency. The OpenZeppelin contracts:
119
120
120
121
```bash
121
122
npm install @openzeppelin/contracts
@@ -135,7 +136,7 @@ Fire up your code editor and let’s start hacking. I’m using VSCode, so I run
135
136
`code .` in my terminal.
136
137
137
138
Let’s create an empty file named `TierNFT.sol` inside the `contracts/` folder
138
-
and copy this code inside:
139
+
and following the contract and file naming convention, create a contract of the same name:
139
140
140
141
```solidity
141
142
// SPDX-License-Identifier: MIT
@@ -145,19 +146,18 @@ contract TierNFT {
145
146
}
146
147
```
147
148
148
-
Now that we have our License, the version of solidity we are using and the
149
+
Now that we have our license, the version of Solidity we are using and the
149
150
contract set up, we can add the logic and the variables we need to store.
150
151
151
-
## What do we need?
152
+
## What do we need?
153
+
### we sort of need this 'intro' at the start of the lesson......
152
154
153
155
As we are creating tiers for the categories of our NFTs, we need to also store
154
-
information about tiers in our contracts.
155
-
156
-
We'll write our smart contract step by step in four stages.
156
+
information about tiers in our contracts. We'll write our smart contract step by step in four stages.
157
157
158
158
### Inheriting OpenZeppelin ERC721 and adding a Mint Function
159
159
160
-
Let’s get started with a simple contract, inheriting OpenZeppelin ERC721 like we did last time.
160
+
Let’s get started by inheriting OpenZeppelin's ERC721.sol like we did last time. We add a constructor to our contract, which will mirror the one from ERC721.sol.
161
161
162
162
```solidity
163
163
// SPDX-License-Identifier: MIT
@@ -173,9 +173,8 @@ contract TierNFT is ERC721 {
173
173
}
174
174
```
175
175
176
-
We add a constructor to our contract, which will mirror the one inherited from ERC721.sol. If we had no constructor, the compiler would throw errors.
177
176
178
-
Now let’s go ahead and add a mint function that only uses`_safeMint`**i don't understand 'only uses'....as opposed to what?**'
177
+
Now let’s go ahead and add a mint function which will call `_safeMint` from the inherited contract, and we'll make it `payable`. We'll see why shortly. Can you remember what `++` does to our total supply?
179
178
180
179
```solidity
181
180
// SPDX-License-Identifier: MIT
@@ -201,7 +200,7 @@ contract TierNFT is ERC721 {
201
200
202
201
### Adding Tiers and Using Them in the Mint Function
203
202
204
-
Now add the tiers and assign their values, where each one represents a service subscription.
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.
205
204
206
205
```solidity
207
206
// SPDX-License-Identifier: MIT
@@ -234,9 +233,9 @@ contract TierNFT is ERC721 {
234
233
}
235
234
````
236
235
237
-
We have added `Basic`, `Medium` and `Premium` as tiers and assigned their values. We store what tier each NFT holds in `mapping(uint256 => uint256) public tokenTier;`.
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.
238
237
239
-
Now we need to modify the mint function for the tier NFTs:
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`!
0 commit comments