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
+22-22Lines changed: 22 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -653,16 +653,17 @@ contract TierNFT is ERC721 {
653
653
</details>
654
654
<br/>
655
655
656
-
### Where are all our funds? Add withdraw Function
656
+
### Where are all our funds? Let's add a withdraw Function!
657
657
658
-
We need to find a way to actually withdraw any funds our contract generates, otherwise they'll get stuck **in the contract** .... that we created!
658
+
We need to find a way to actually withdraw any funds our contract generates, otherwise they'll get stuck **in the contract** .... that **we** created! Let's import `Ownable.sol`, to get those permissions, so that only **we** can withdraw those funds, and not anyone else. Clever, eh?
659
659
660
660
```solidity
661
661
// Place this next to the other imports at the top:
We import `Ownable.sol`, so that only *we* can withdraw those funds, and not anyone else. Clever, eh?
665
+
666
+
And inherit `Ownable` from the Open Zeppelin contract into our own.
666
667
667
668
```solidity
668
669
// Modify the contract definition, by adding 'Ownable' at the end of the line:
@@ -674,7 +675,6 @@ contract TierNFT is ERC721, Ownable {
674
675
}
675
676
```
676
677
677
-
We inherit `Ownable` from the Open Zeppelin contract into ours.
678
678
679
679
If your phone is ringing, or someone is knocking at your door right now, ignore all of it! Let’s get this withdraw function coded in here!!
680
680
@@ -697,15 +697,15 @@ If your phone is ringing, or someone is knocking at your door right now, ignore
697
697
}
698
698
```
699
699
700
-
-`onlyOwner` - You're going to see this modifier a lot. It comes from the Ownable contract we just imported. It's very powerful. It makes
701
-
sure that only the account that deployed the contract (owner is assigned on its
702
-
constructor) can execute the function that it appears in.
700
+
-`onlyOwner` - you're going to see this modifier a lot. It comes from the *Ownable* contract we just imported. It's very powerful. It makes sure that only the account that deployed the contract, i.e. the owner is assigned on its
701
+
constructor, can execute the function that it appears in.
702
+
- In Solidity, `this` refers to the current contract instance. `address(this)` gets the contract's address, and `.balance` grabs the current balance, which gets stored in `uint256 balance`.
703
703
- By checking `uint256 balance = address(this).balance` against `require(balance > 0, "Balance should be > 0");`, we can see if we actually have something to withdraw. And we would hope so, as the function consumes gas. And, as you've probably guessed, it will throw an error otherwise.
704
-
-`(bool success, ) = payable(owner()).call{value: balance}("")`- is an actual
704
+
-`(bool success, ) = payable(owner()).call{value: balance}("")` is an actual
705
705
transfer of funds which uses the whole balance that we checked in the previous
706
-
instruction.
706
+
instruction. The `owner`'s address can allow the transfer thanks to the `payable()` function. We can leave the `("")` data parameter blank in our case.
707
707
-`require(success, "Withdraw failed")` - This is a good practice because
708
-
`call` doesn’t revert. With this practice we can make sure that the transfer
708
+
the `.call()` function doesn’t revert. With this practice we can make sure that the transfer
709
709
occurred and throw an error if it doesn’t.
710
710
711
711
<br/>
@@ -812,7 +812,7 @@ contract TierNFT is ERC721, Ownable {
812
812
813
813
We need a script so we can get our smart contract deployed. Let’s write that.
814
814
815
-
Create a new javascript file named `deploy.js` in the `scripts` folder with this
815
+
Create a new Javascript file named `deploy.js` in the `scripts` folder with this
816
816
code:
817
817
818
818
```jsx
@@ -843,22 +843,22 @@ main().catch((error) => {
843
843
```
844
844
845
845
-`CONTRACT_NAME = "TierNFT"` - is the name of our contract, which
846
-
will tell Hardhat exactly *what* to deploy.
846
+
will tell Hardhat exactly **what** to deploy.
847
847
- With `const COLLECTION_NAME = "TierNFT"` and
848
-
`const COLLECTION_SYMBOL = "Tier"` - we define the Name and Symbol to pass to the constructor for the deployment.
848
+
`const COLLECTION_SYMBOL = "Tier"` - we define the *name* and *symbol* to pass to the constructor for the deployment.
849
849
-`hre.ethers.getContractFactory(CONTRACT_NAME)` - this asks Hardhat Runtime
850
-
Environment to get us a contract factory for our contract.
851
-
-`contractFactory.deploy` - We are asking the contract factory to deploy our
850
+
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.
851
+
-`contractFactory.deploy` - we are asking the contract factory to deploy an instance of our
852
852
contract. This is the deploy transaction!
853
-
-`COLLECTION_NAME, COLLECTION_SYMBOL` - These are the parameters for our
854
-
contract's constructor function.
855
-
-`await contract.deployed()` - It waits for the transaction to be approved and our
856
-
contract is finished deploying.
857
-
-The `main().catch( … )` at the very end makes sure that all the previous
858
-
code is executed when this script is run, and also prints any errors to the console.
853
+
-`COLLECTION_NAME, COLLECTION_SYMBOL` - these are the parameters for our
854
+
contract's constructor function to set up its initial state.
855
+
-`await contract.deployed()` - waits for the transaction to be approved and our
856
+
contract to be finished deploying.
857
+
-Running the `main().catch( … )` script at the very end makes sure that all the previous
858
+
code is executed, and also logs any errors, and prints them to the console.
859
859
860
860
861
-
### Time to DEPLOY but also some changes
861
+
### Time to deploy, but first some changes
862
862
863
863
Now the time has come for us to deploy our smart contract. But before we can do
864
864
that, we need to modify the code in `hardhat.config.js` placed in our root folder to this:
0 commit comments