Skip to content

Commit 33ccda0

Browse files
committed
Update code in lesson due to breaking changes in ethers.js/hardhat toolbox
1 parent a6e7b5a commit 33ccda0

File tree

2 files changed

+28
-22
lines changed
  • pages/lessons/projects
  • utils/questions/lesson-3/4-deploy-scripts

2 files changed

+28
-22
lines changed

pages/lessons/projects/3.mdx

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ Now that we have set the stage, it's time to dive into the exciting world of tie
5858
- Creating tiered NFTs with varying levels of rarity and attributes
5959
- Write scripts for deployment
6060
- Exploring additional functionalities and possibilities with tiered NFTs ... in the smart contract!
61-
- Showcasing and trading your tiered NFTs on a public marketplace
61+
- Securely manage sensitive environment variables
62+
- Showcasing your tiered NFTs on a public marketplace
6263

6364
### Developer tooling
6465

@@ -136,7 +137,7 @@ last prompt. If they didn’t install or we accidentally chose ‘n’, we can a
136137
install them manually with:
137138
138139
```bash
139-
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-chai-matchers
140+
npm install --save-dev hardhat @nomicfoundation/hardhat-toolbox
140141
```
141142
</Callout>
142143
In a Hardhat project, some of the default folders are:
@@ -197,6 +198,9 @@ contract defined, we can add the logic and the variables we need to store. As we
197198

198199
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.
199200
201+
{/* @wolovim optional line?: Unlike in our *Basic NFT* lesson, where we added a third parameter to the constructor for storing the token's baseURI, we will have a different approach this time, which we will come to later. */}
202+
203+
200204
```solidity
201205
// SPDX-License-Identifier: MIT
202206
pragma solidity 0.8.12;
@@ -218,7 +222,7 @@ Now let’s go ahead and add a mint function which will call `_safeMint` from th
218222
// SPDX-License-Identifier: MIT
219223
pragma solidity 0.8.12;
220224
221-
import '@openzeppelin/contracts/token/ERC721/ERC721.sol'
225+
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
222226
223227
contract TierNFT is ERC721 {
224228
@@ -839,18 +843,17 @@ const CONTRACT_NAME = 'TierNFT'
839843
const COLLECTION_NAME = 'TierNFT'
840844
const COLLECTION_SYMBOL = 'Tier'
841845

842-
/** Main deploy function **/
843846
async function main() {
844847
const contractFactory = await hre.ethers.getContractFactory(CONTRACT_NAME)
845848
const contract = await contractFactory.deploy(
846849
COLLECTION_NAME,
847850
COLLECTION_SYMBOL,
848851
)
849-
await contract.deployed()
852+
853+
await contract.waitForDeployment()
850854
// Print our newly deployed contract address
851-
console.log(`Contract deployed to ${contract.address}`)
855+
console.log("Contract deployed at ", await contract.getAddress())
852856
}
853-
854857
/** Run Main function - Do not change **/
855858
main().catch((error) => {
856859
console.error(error)
@@ -862,13 +865,13 @@ main().catch((error) => {
862865
will tell Hardhat exactly **what** to deploy.
863866
- With `const COLLECTION_NAME = "TierNFT"` and
864867
`const COLLECTION_SYMBOL = "Tier"` - we define the *name* and *symbol* to pass to the constructor for the deployment.
865-
- `hre.ethers.getContractFactory(CONTRACT_NAME)` - this asks Hardhat Runtime
866-
Environment to get us a contract factory for our contract specified by `CONTRACT_NAME`. A contract factory is a template that allows us to deploy instances of that contract, which allows many advantages.
868+
- `hre.ethers.getContractFactory(CONTRACT_NAME)` - this asks `hre` (Hardhat Runtime
869+
Environment) to get us a contract factory for our contract specified by `CONTRACT_NAME`. A contract factory is a template that allows us to deploy instances of that contract, which allows many advantages.
867870
- `contractFactory.deploy` - we are asking the contract factory to deploy an instance of our
868871
contract. This is the deploy transaction!
869872
- `COLLECTION_NAME, COLLECTION_SYMBOL` - these are the parameters for our
870873
contract's constructor function to set up its initial state.
871-
- `await contract.deployed()` - waits for the transaction to be approved by the network validators, and confirms that the deployment of the contract was successful, telling us that it's ready for use on the blockchain.
874+
- `await contract.waitForDeployment()` - waits for the transaction to be approved by the network validators, and confirms that the deployment of the contract was successful, telling us that it's ready for use on the blockchain.
872875
- Running the `main().catch( … )` script at the very end makes sure that all the previous
873876
code is executed, and also logs any errors, and prints them to the console.
874877

@@ -911,6 +914,8 @@ Note: **Always make sure to use a separate browser profile, with a separate wall
911914

912915
### Getting Some Testnet Funds
913916

917+
{/* @wolovim, maybe we should give a couple of testnet options, just in case. what do you think?*/}
918+
914919
A testnet is a sandbox environment where developers
915920
can test, create and modify functionalities, monitor and simulate a mainnet blockchain's network
916921
performance, fix bugs and other network failures without having to worry about breaking a main chain, and paying in real crypto coins to do so! Mainnets cost - testnets generally don't.
@@ -925,8 +930,7 @@ Note: Testnet and mainnet are separate networks. You can't for example send toke
925930
### Personal Security
926931
Take your time to understand the *good practices* in this section. Before we deploy, we need to add a `.env` file to our root folder to make sure we are **not pushing and therefore exposing our private keys in public repositories**.
927932

928-
For your wallet's private key, the most sensitive data of the project, you need to open Metamask. Click on the three dots next to your *Account Name*, and then on *Account Details*, click on *Export Private Key*. It will ask for your Metamask password, the one you use to log in to your wallet each session, not your seed phrase. It also shows you a
929-
notice so you know that you are entering the *danger zone*. Confirm and you'll be able to copy your private key. Add your private key into the `.env` file like so:
933+
For your wallet's private key, the most sensitive data of the project, you need to open Metamask. Click on the three dots next to your *Account Name*, and then on *Account Details*, click on *Show Private Key*. It will ask for your Metamask password, the one you use to log in to your wallet each session, not your seed phrase. It also shows you a warning to 'Never disclose this key'. Confirm and you'll be able to copy your private key. Add your private key into the `.env` file like so:
930934

931935
```bash
932936
PRIVATE_KEY=f8abc629b...
@@ -988,47 +992,49 @@ async function main() {
988992
const contractFactory = await hre.ethers.getContractFactory(CONTRACT_NAME)
989993
const contract = await contractFactory.attach(CONTRACT_ADDRESS)
990994
// Print our newly deployed contract address
991-
console.log(`Attached contract: ${contract.address}`)
995+
console.log("Attached contract ", await contract.getAddress())
992996

993997
// Call the mint function for Tier 0
994998
let txn = await contract.mint({
995-
value: hre.ethers.utils.parseEther(VALUE_TIER_0),
999+
value: hre.ethers.parseEther(VALUE_TIER_0),
9961000
})
9971001
await txn.wait() // Wait for the NFT to be minted
9981002
console.log('Minted a Tier 0 NFT!')
9991003

10001004
// Call the mint function for Tier 1
10011005
txn = await contract.mint({
1002-
value: hre.ethers.utils.parseEther(VALUE_TIER_1),
1006+
value: hre.ethers.parseEther(VALUE_TIER_1),
10031007
})
10041008
await txn.wait() // Wait for the NFT to be minted
10051009
console.log('Minted a Tier 1 NFT!')
10061010

10071011
// Call the mint function for Tier 2
10081012
txn = await contract.mint({
1009-
value: hre.ethers.utils.parseEther(VALUE_TIER_2),
1010-
})
1013+
value: hre.ethers.parseEther(VALUE_TIER_2),
1014+
})
10111015
await txn.wait() // Wait for the NFT to be minted
10121016
console.log('Minted a Tier 2 NFT!')
10131017

1014-
let totalSupply = await contract.totalSupply()
1015-
console.log("Collection's new totalSupply: ", totalSupply)
1018+
// Print total number of minted NFTs
1019+
let totalSupply = await contract.totalSupply()
1020+
console.log("Collection's new totalSupply: ", totalSupply)
10161021
}
10171022

10181023
/** Run Main function - Do not change **/
10191024
main().catch((error) => {
10201025
console.error(error)
10211026
process.exitCode = 1
10221027
})
1028+
10231029
```
10241030

10251031
- `const contract = await contractFactory.attach(CONTRACT_ADDRESS)` will
10261032
make sure that we are not deploying the contract again. Instead we need
10271033
Hardhat to use the contract addresss we just deployed to the testnet.
10281034
- `let txn = await contract.mint(...` is calling the mint function.
1029-
- `value: hre.ethers.utils.parseEther(VALUE_TIER_0)` - defines the value that we
1035+
- `value: hre.ethers.parseEther(VALUE_TIER_0)` - defines the value that we
10301036
want to send to the mint function. This defines which *Tier* we get.
1031-
- `ethers.utils.parseEther` - here we use *Ethers* to translate the value into wei i.e. multiply it with 10\*\*18
1037+
- `ethers.parseEther` - here we use *Ethers* to translate the value into wei i.e. multiply it with 10\*\*18
10321038
- `let totalSupply = await contract.totalSupply()` - is calling the
10331039
`totalSupply()` function to check if the 3 NFTs minted correctly.
10341040
<br/>

utils/questions/lesson-3/4-deploy-scripts/Q2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"question": "What does the `await contract.deployed()` line do in the code?",
2+
"question": "What does the `await contract.waitForDeployment()` line do in the code?",
33
"options": [
44
{
55
"answer": " It deploys the contract to the blockchain."

0 commit comments

Comments
 (0)