Skip to content

Commit 0f95e35

Browse files
authored
SDK Updates (#247)
* Add reentrancy guards to ERC721/1155LazyMint.claim() * transferTokensOnClaim hook, for minting and state updates * run prettier
1 parent 6d44826 commit 0f95e35

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

contracts/base/ERC1155LazyMint.sol

+20-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import "../extension/LazyMint.sol";
1212
import "../extension/interface/IClaimableERC1155.sol";
1313

1414
import "../lib/TWStrings.sol";
15+
import "../openzeppelin-presets/security/ReentrancyGuard.sol";
1516

1617
/**
1718
* BASE: ERC1155Base
@@ -53,7 +54,8 @@ contract ERC1155LazyMint is
5354
Multicall,
5455
BatchMintMetadata,
5556
LazyMint,
56-
IClaimableERC1155
57+
IClaimableERC1155,
58+
ReentrancyGuard
5759
{
5860
using TWStrings for uint256;
5961

@@ -110,12 +112,11 @@ contract ERC1155LazyMint is
110112
address _receiver,
111113
uint256 _tokenId,
112114
uint256 _quantity
113-
) public payable virtual {
114-
verifyClaim(msg.sender, _tokenId, _quantity); // add your claim verification logic by overriding this function
115-
115+
) public payable virtual nonReentrant {
116116
require(_tokenId < nextTokenIdToMint(), "invalid id");
117+
verifyClaim(msg.sender, _tokenId, _quantity); // Add your claim verification logic by overriding this function.
117118

118-
_mint(_receiver, _tokenId, _quantity, "");
119+
transferTokensOnClaim(_receiver, _tokenId, _quantity); // Mints tokens. Apply any state updates by overriding this function.
119120
emit TokensClaimed(msg.sender, _receiver, _tokenId, _quantity);
120121
}
121122

@@ -205,6 +206,20 @@ contract ERC1155LazyMint is
205206
Internal functions
206207
//////////////////////////////////////////////////////////////*/
207208

209+
/**
210+
* @notice Mints tokens to receiver on claim.
211+
* Can also use this function to apply any state changes before minting.
212+
*
213+
* @dev Override this function to add logic for state updation.
214+
*/
215+
function transferTokensOnClaim(
216+
address _receiver,
217+
uint256 _tokenId,
218+
uint256 _quantity
219+
) internal virtual {
220+
_mint(_receiver, _tokenId, _quantity, "");
221+
}
222+
208223
/// @dev Returns whether lazy minting can be done in the given execution context.
209224
function _canLazyMint() internal view virtual override returns (bool) {
210225
return msg.sender == owner();

contracts/base/ERC721LazyMint.sol

+22-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import "../extension/LazyMint.sol";
1212
import "../extension/interface/IClaimableERC721.sol";
1313

1414
import "../lib/TWStrings.sol";
15+
import "../openzeppelin-presets/security/ReentrancyGuard.sol";
1516

1617
/**
1718
* BASE: ERC721A
@@ -47,7 +48,8 @@ contract ERC721LazyMint is
4748
Royalty,
4849
BatchMintMetadata,
4950
LazyMint,
50-
IClaimableERC721
51+
IClaimableERC721,
52+
ReentrancyGuard
5153
{
5254
using TWStrings for uint256;
5355

@@ -107,11 +109,11 @@ contract ERC721LazyMint is
107109
* @param _receiver The recipient of the NFT to mint.
108110
* @param _quantity The number of NFTs to mint.
109111
*/
110-
function claim(address _receiver, uint256 _quantity) public payable virtual {
111-
verifyClaim(msg.sender, _quantity); // add your claim verification logic by overriding this function
112-
uint256 startTokenId = _currentIndex;
112+
function claim(address _receiver, uint256 _quantity) public payable virtual nonReentrant {
113113
require(_currentIndex + _quantity <= nextTokenIdToLazyMint, "Not enough lazy minted tokens.");
114-
_safeMint(_receiver, _quantity, "");
114+
verifyClaim(msg.sender, _quantity); // Add your claim verification logic by overriding this function.
115+
116+
uint256 startTokenId = transferTokensOnClaim(_receiver, _quantity); // Mints tokens. Apply any state updates by overriding this function.
115117
emit TokensClaimed(msg.sender, _receiver, startTokenId, _quantity);
116118
}
117119

@@ -150,6 +152,21 @@ contract ERC721LazyMint is
150152
Internal functions
151153
//////////////////////////////////////////////////////////////*/
152154

155+
/**
156+
* @notice Mints tokens to receiver on claim.
157+
* Can also use this function to apply any state changes before minting.
158+
*
159+
* @dev Override this function to add logic for state updation.
160+
*/
161+
function transferTokensOnClaim(address _receiver, uint256 _quantity)
162+
internal
163+
virtual
164+
returns (uint256 startTokenId)
165+
{
166+
startTokenId = _currentIndex;
167+
_safeMint(_receiver, _quantity);
168+
}
169+
153170
/// @dev Returns whether lazy minting can be done in the given execution context.
154171
function _canLazyMint() internal view virtual override returns (bool) {
155172
return msg.sender == owner();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-License-Identifier: MIT
2+
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)
3+
4+
pragma solidity ^0.8.0;
5+
6+
abstract contract ReentrancyGuard {
7+
uint256 private constant _NOT_ENTERED = 1;
8+
uint256 private constant _ENTERED = 2;
9+
10+
uint256 private _status;
11+
12+
constructor() {
13+
_status = _NOT_ENTERED;
14+
}
15+
16+
/**
17+
* @dev Prevents a contract from calling itself, directly or indirectly.
18+
*/
19+
modifier nonReentrant() {
20+
// On the first call to nonReentrant, _notEntered will be true
21+
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
22+
23+
// Any calls to nonReentrant after this point will fail
24+
_status = _ENTERED;
25+
26+
_;
27+
28+
// By storing the original value once again, a refund is triggered (see
29+
// https://eips.ethereum.org/EIPS/eip-2200)
30+
_status = _NOT_ENTERED;
31+
}
32+
}

docs/ReentrancyGuard.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# ReentrancyGuard
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
12+

0 commit comments

Comments
 (0)