Skip to content

Commit 9bfd817

Browse files
committed
Improve gas efficiency and apply fix to OZ vulnerability OpenZeppelin/openzeppelin-contracts#3552
1 parent 2cd921b commit 9bfd817

File tree

5 files changed

+1903
-875
lines changed

5 files changed

+1903
-875
lines changed

contracts/NFT.sol

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,26 @@ pragma solidity ^0.8.4;
44

55
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
66
import "@openzeppelin/contracts/access/Ownable.sol";
7-
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
7+
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
88

99
/**
1010
* @title NFT
1111
* @dev This is a very basic NFT that do not provide any name changing functionality.
1212
*
1313
* Authors: 0xSimo
1414
* Created: 01.07.2021
15-
* Last revision: 22.03.2022: gas optimizations, ERC721Enumerable
15+
* Last revision: 22.07.2022: gas optimizations
1616
*/
17-
contract NFT is ERC721Enumerable, Ownable {
17+
contract NFT is ERC721, Ownable {
1818
using SafeMath for uint256;
1919

2020
// The maximum number of tokens, this is just an example
2121
uint256 public constant MAX_NFT_SUPPLY = 233;
2222
// The NFT minting price, this is just an example
2323
uint256 public constant NFT_MINT_PRICE = 0.1 ether;
24+
// the (current) total supply
25+
uint256 public totalSupply;
26+
2427

2528
/**
2629
* @dev Constructor
@@ -31,16 +34,16 @@ contract NFT is ERC721Enumerable, Ownable {
3134
* @dev Mint 'numberOfNfts' new tokens
3235
*/
3336
function mintNFT(uint256 numberOfNfts) public payable {
34-
require(totalSupply() < MAX_NFT_SUPPLY, "Sale has already ended");
37+
require(totalSupply < MAX_NFT_SUPPLY, "Sale has already ended");
3538
require(numberOfNfts > 0, "numberOfNfts cannot be 0");
3639
require(numberOfNfts <= 20, "You may not buy more than 20 NFTs at once");
37-
require(totalSupply().add(numberOfNfts) <= MAX_NFT_SUPPLY, "Sale has already ended");
40+
require(totalSupply.add(numberOfNfts) <= MAX_NFT_SUPPLY, "Sale has already ended");
3841
require(NFT_MINT_PRICE.mul(numberOfNfts) == msg.value, "Ether value sent is not correct");
3942

4043
for (uint i = 0; i < numberOfNfts; i++) {
41-
uint256 mintIndex = totalSupply();
42-
if (mintIndex < MAX_NFT_SUPPLY) {
43-
_safeMint(_msgSender(), mintIndex);
44+
if (totalSupply < MAX_NFT_SUPPLY) {
45+
_mint(_msgSender(), totalSupply);
46+
totalSupply += 1; // can be unchecked no overflow here
4447
}
4548
}
4649
}

contracts/UC1.sol

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "@openzeppelin/contracts/access/Ownable.sol";
66
import "@openzeppelin/contracts/utils/Strings.sol";
77
import "@openzeppelin/contracts/utils/Base64.sol";
88
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
9-
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
9+
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
1010
import "./INCT.sol";
1111

1212
/**
@@ -17,9 +17,9 @@ import "./INCT.sol";
1717
* Authors: 0xSimo
1818
* Created: 01.07.2021
1919
* Last revision: 26.07.2021: set name change price already ready for a real example
20-
* 22.03.2022: on-chain metadata, gas optimizations, ERC721Enumerable
20+
* 22.07.2022: gas optimizations
2121
*/
22-
contract UC1 is ERC721Enumerable, Ownable {
22+
contract UC1 is ERC721, Ownable {
2323
using Strings for uint256;
2424
using SafeMath for uint256;
2525

@@ -29,6 +29,8 @@ contract UC1 is ERC721Enumerable, Ownable {
2929
uint256 public constant NAME_CHANGE_PRICE = 10 * (10 ** 18);
3030
// The NFT minting price, this is just an example
3131
uint256 public constant NFT_MINT_PRICE = 0.1 ether;
32+
// the (current) total supply
33+
uint256 public totalSupply;
3234

3335
// Mapping from token ID to name
3436
mapping (uint256 => string) private _tokenName;
@@ -99,16 +101,16 @@ contract UC1 is ERC721Enumerable, Ownable {
99101
* @dev Mint 'numberOfNfts' new tokens
100102
*/
101103
function mintNFT(uint256 numberOfNfts) public payable {
102-
require(totalSupply() < MAX_NFT_SUPPLY, "Sale has already ended");
104+
require(totalSupply < MAX_NFT_SUPPLY, "Sale has already ended");
103105
require(numberOfNfts > 0, "numberOfNfts cannot be 0");
104106
require(numberOfNfts <= 20, "You may not buy more than 20 NFTs at once");
105-
require(totalSupply().add(numberOfNfts) <= MAX_NFT_SUPPLY, "Sale has already ended");
107+
require(totalSupply.add(numberOfNfts) <= MAX_NFT_SUPPLY, "Sale has already ended");
106108
require(NFT_MINT_PRICE.mul(numberOfNfts) == msg.value, "Ether value sent is not correct");
107109

108110
for (uint i = 0; i < numberOfNfts; i++) {
109-
uint256 mintIndex = totalSupply();
110-
if (mintIndex < MAX_NFT_SUPPLY) {
111-
_safeMint(_msgSender(), mintIndex);
111+
if (totalSupply < MAX_NFT_SUPPLY) {
112+
_mint(_msgSender(), totalSupply);
113+
totalSupply += 1; // can be unchecked no overflow here
112114
}
113115
}
114116
}

0 commit comments

Comments
 (0)