From 6e2f8b14588f93ec0765db291d2ceb4a4538e947 Mon Sep 17 00:00:00 2001
From: gongtao245 <gongtao245@gmail.com>
Date: Mon, 30 Oct 2023 10:53:16 +0800
Subject: [PATCH] import latest openzeppelin

---
 .../contracts/erc721/ERC721Basic.sol                   |  2 +-
 .../contracts/erc721/ERC721LazyMint.sol                |  5 +++--
 .../contracts/erc721/ERC721LazyMintWith712.sol         |  4 ++--
 .../erc721/ERC721LazyMintWith712SignatureChecker.sol   |  4 ++--
 .../contracts/erc721/SmartWallet.sol                   |  3 +--
 .../contracts/redpacket/HappyRedPacket.sol             | 10 +++++-----
 .../contracts/redpacket/SimpleToken.sol                |  8 ++++----
 basic/42-merkle-distributor-airdrop/hardhat.config.js  |  9 ++++++++-
 basic/42-merkle-distributor-airdrop/package.json       |  4 ++--
 9 files changed, 28 insertions(+), 21 deletions(-)

diff --git a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721Basic.sol b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721Basic.sol
index 23f326086..2c05599d7 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721Basic.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721Basic.sol
@@ -11,7 +11,7 @@ contract ERC721Basic is ERC721, AccessControl {
     constructor(string memory name, string memory symbol)
     ERC721(name, symbol)
     {
-        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
+        _grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
     }
 
     function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, AccessControl) returns (bool) {
diff --git a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMint.sol b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMint.sol
index 243be5ffe..c80e781b2 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMint.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMint.sol
@@ -4,6 +4,7 @@ pragma solidity ^0.8.0;
 import "@openzeppelin/contracts/access/AccessControl.sol";
 import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
 import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
+import "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
 
 contract ERC721LazyMint is ERC721, AccessControl {
     bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
@@ -11,7 +12,7 @@ contract ERC721LazyMint is ERC721, AccessControl {
     constructor(string memory name, string memory symbol)
     ERC721(name, symbol)
     {
-        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
+        _grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
     }
 
     function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, AccessControl) returns (bool) {
@@ -28,7 +29,7 @@ contract ERC721LazyMint is ERC721, AccessControl {
     function _hash(address account, uint256 tokenId)
     internal pure returns (bytes32)
     {
-        return ECDSA.toEthSignedMessageHash(keccak256(abi.encodePacked(tokenId, account)));
+        return MessageHashUtils.toEthSignedMessageHash(keccak256(abi.encodePacked(tokenId, account)));
     }
 
     function _verify(bytes32 digest, bytes memory signature)
diff --git a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712.sol b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712.sol
index deef807eb..9dfee7335 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712.sol
@@ -3,7 +3,7 @@ pragma solidity ^0.8.0;
 
 import "@openzeppelin/contracts/access/AccessControl.sol";
 import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
-import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
+import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
 import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
 
 contract ERC721LazyMintWith712 is ERC721, EIP712, AccessControl {
@@ -13,7 +13,7 @@ contract ERC721LazyMintWith712 is ERC721, EIP712, AccessControl {
     ERC721(name, symbol)
     EIP712(name, "1.0.0")
     {
-        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
+        _grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
     }
 
     function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, AccessControl) returns (bool) {
diff --git a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712SignatureChecker.sol b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712SignatureChecker.sol
index c7130bcc7..85457456e 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712SignatureChecker.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/erc721/ERC721LazyMintWith712SignatureChecker.sol
@@ -3,7 +3,7 @@ pragma solidity ^0.8.0;
 
 import "@openzeppelin/contracts/access/AccessControl.sol";
 import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
-import "@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol";
+import "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
 import "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol";
 
 contract ERC721LazyMintWith712SignatureChecker is ERC721, EIP712, AccessControl {
@@ -13,7 +13,7 @@ contract ERC721LazyMintWith712SignatureChecker is ERC721, EIP712, AccessControl
     ERC721(name, symbol)
     EIP712(name, "1.0.0")
     {
-        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
+        _grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
     }
 
     function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, AccessControl) returns (bool) {
diff --git a/basic/42-merkle-distributor-airdrop/contracts/erc721/SmartWallet.sol b/basic/42-merkle-distributor-airdrop/contracts/erc721/SmartWallet.sol
index 14e2cf749..e2b627c2f 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/erc721/SmartWallet.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/erc721/SmartWallet.sol
@@ -6,8 +6,7 @@ import "@openzeppelin/contracts/interfaces/IERC1271.sol";
 import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
 
 contract SmartWallet is Ownable, IERC1271 {
-    constructor(address initialOwner) {
-        transferOwnership(initialOwner);
+    constructor(address initialOwner) Ownable(initialOwner) {
     }
 
     function execute(address target, uint256 value, bytes memory data) external onlyOwner() returns (bytes memory) {
diff --git a/basic/42-merkle-distributor-airdrop/contracts/redpacket/HappyRedPacket.sol b/basic/42-merkle-distributor-airdrop/contracts/redpacket/HappyRedPacket.sol
index 01d2f0370..3ebbeeb3e 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/redpacket/HappyRedPacket.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/redpacket/HappyRedPacket.sol
@@ -12,12 +12,11 @@ pragma solidity >= 0.8.0;
 import "../lib/IERC20.sol";
 import "../lib/SafeERC20.sol";
 import "../lib/Initializable.sol";
-import "@openzeppelin/contracts/utils/math/SafeMath.sol";
 import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
+import "@openzeppelin/contracts/utils/math/Math.sol";
 import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
 
 contract HappyRedPacket is Initializable {
-    using SafeMath for uint256;
 
     struct RedPacket {
         Packed packed;
@@ -91,7 +90,8 @@ contract HappyRedPacket is Initializable {
             uint256 balance_before_transfer = IERC20(_token_addr).balanceOf(address(this));
             IERC20(_token_addr).safeTransferFrom(msg.sender, address(this), _total_tokens);
             uint256 balance_after_transfer = IERC20(_token_addr).balanceOf(address(this));
-            received_amount = balance_after_transfer.sub(balance_before_transfer);
+            // received_amount = balance_after_transfer.sub(balance_before_transfer);
+            received_amount = balance_after_transfer - balance_before_transfer;
             require(received_amount >= _number, "#received > #packets");
         }
 
@@ -143,7 +143,7 @@ contract HappyRedPacket is Initializable {
                 // reserve minium amount => (total_number - claimed_number) * 0.1
                 uint reserve_amount = (total_number - claimed_number) * minium_value;
                 uint distribute_tokens = remaining_tokens - reserve_amount;
-                claimed_tokens = random(seed, nonce) % SafeMath.div(SafeMath.mul(distribute_tokens, 2), total_number - claimed_number);
+                claimed_tokens = random(seed, nonce) % Math.mulDiv(distribute_tokens, 2, total_number - claimed_number);
                 // minium claimed_tokens for user is 0.1 ; and round the claimed_tokens to decimal 0.1
                 claimed_tokens = claimed_tokens < minium_value ? minium_value : (claimed_tokens - (claimed_tokens % minium_value));
             }
@@ -151,7 +151,7 @@ contract HappyRedPacket is Initializable {
             if (total_number - claimed_number == 1) 
                 claimed_tokens = remaining_tokens;
             else
-                claimed_tokens = SafeMath.div(remaining_tokens, (total_number - claimed_number));
+                claimed_tokens = Math.ceilDiv(remaining_tokens, (total_number - claimed_number));
         }
 
         rp.packed.packed1 = rewriteBox(packed.packed1, 128, 96, remaining_tokens - claimed_tokens);
diff --git a/basic/42-merkle-distributor-airdrop/contracts/redpacket/SimpleToken.sol b/basic/42-merkle-distributor-airdrop/contracts/redpacket/SimpleToken.sol
index 2476f9739..82ae4ca76 100644
--- a/basic/42-merkle-distributor-airdrop/contracts/redpacket/SimpleToken.sol
+++ b/basic/42-merkle-distributor-airdrop/contracts/redpacket/SimpleToken.sol
@@ -1,9 +1,9 @@
 // SPDX-License-Identifier: MIT
-pragma solidity ^0.8.0;
+pragma solidity ^0.8.20;
 
-import "@openzeppelin/contracts/token/ERC20/presets/ERC20PresetMinterPauser.sol";
+import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
 
-contract SimpleToken is ERC20PresetMinterPauser {
+contract SimpleToken is ERC20 {
     /**
      * @dev Constructor that gives msg.sender all of existing tokens.
      */
@@ -20,7 +20,7 @@ contract SimpleToken is ERC20PresetMinterPauser {
         string memory symbol,
         uint8 decimals_,
         uint256 initial_supply
-    ) ERC20PresetMinterPauser(name, symbol) {
+    ) ERC20(name, symbol) {
         _decimals = decimals_;
         INITIAL_SUPPLY = initial_supply * (10**uint256(decimals_));
         _mint(msg.sender, initial_supply * (10**uint256(decimals_)));
diff --git a/basic/42-merkle-distributor-airdrop/hardhat.config.js b/basic/42-merkle-distributor-airdrop/hardhat.config.js
index 34ce427e2..d0bb5c38e 100644
--- a/basic/42-merkle-distributor-airdrop/hardhat.config.js
+++ b/basic/42-merkle-distributor-airdrop/hardhat.config.js
@@ -25,10 +25,13 @@ module.exports = {
       { version: '0.7.6',  settings },
       { version: '0.6.11', settings },
       { version: '0.5.16', settings },
+      { version: '0.8.20', settings },
     ],
   },
   etherscan: {
-    apiKey: process.env.API_KEY
+    apiKey: {
+      optimisticEthereum: 'xxx'
+    },
   },
   networks: {
     localhost: {
@@ -59,6 +62,10 @@ module.exports = {
       url: 'https://polygon-mainnet.infura.io/v3/' + process.env.INFURA_ID,
       accounts: mnemonic()
     },
+    optim: {
+      url: "https://optimism-mainnet.infura.io/v3/" + process.env.INFURA_ID,
+      accounts: mnemonic()
+    },
   },
   gasReporter: {
     currency: 'USD',
diff --git a/basic/42-merkle-distributor-airdrop/package.json b/basic/42-merkle-distributor-airdrop/package.json
index 522c9545c..a91586b9f 100644
--- a/basic/42-merkle-distributor-airdrop/package.json
+++ b/basic/42-merkle-distributor-airdrop/package.json
@@ -20,8 +20,6 @@
   "devDependencies": {
     "@nomiclabs/hardhat-ethers": "^2.0.1",
     "@nomiclabs/hardhat-waffle": "^2.0.1",
-    "@nomiclabs/hardhat-etherscan": "^3.0.0",
-    "@openzeppelin/contracts": "^4.4.2",
     "chai": "^4.3.4",
     "ethereum-waffle": "^3.3.0",
     "ethereumjs-util": "^7.0.4",
@@ -32,6 +30,8 @@
     "merkletreejs": "^0.2.13"
   },
   "dependencies": {
+    "@nomiclabs/hardhat-etherscan": "^3.1.7",
+    "@openzeppelin/contracts": "^5.0.0",
     "dotenv": "^16.0.0",
     "web3": "^1.4.0"
   }