Skip to content

Commit 74a6c29

Browse files
authored
Fix legacy contract size (#536)
fix size
1 parent 600df98 commit 74a6c29

File tree

3 files changed

+143
-2
lines changed

3 files changed

+143
-2
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
/// @author thirdweb
5+
6+
/**
7+
* @title Batch-mint Metadata
8+
* @notice The `BatchMintMetadata` is a contract extension for any base NFT contract. It lets the smart contract
9+
* using this extension set metadata for `n` number of NFTs all at once. This is enabled by storing a single
10+
* base URI for a batch of `n` NFTs, where the metadata for each NFT in a relevant batch is `baseURI/tokenId`.
11+
*/
12+
13+
contract BatchMintMetadata_V1 {
14+
/// @dev Largest tokenId of each batch of tokens with the same baseURI.
15+
uint256[] private batchIds;
16+
17+
/// @dev Mapping from id of a batch of tokens => to base URI for the respective batch of tokens.
18+
mapping(uint256 => string) private baseURI;
19+
20+
/**
21+
* @notice Returns the count of batches of NFTs.
22+
* @dev Each batch of tokens has an in ID and an associated `baseURI`.
23+
* See {batchIds}.
24+
*/
25+
function getBaseURICount() public view returns (uint256) {
26+
return batchIds.length;
27+
}
28+
29+
/**
30+
* @notice Returns the ID for the batch of tokens at the given index.
31+
* @dev See {getBaseURICount}.
32+
* @param _index Index of the desired batch in batchIds array.
33+
*/
34+
function getBatchIdAtIndex(uint256 _index) public view returns (uint256) {
35+
if (_index >= getBaseURICount()) {
36+
revert("Invalid index");
37+
}
38+
return batchIds[_index];
39+
}
40+
41+
/// @dev Returns the id for the batch of tokens the given tokenId belongs to.
42+
function _getBatchId(uint256 _tokenId) internal view returns (uint256 batchId, uint256 index) {
43+
uint256 numOfTokenBatches = getBaseURICount();
44+
uint256[] memory indices = batchIds;
45+
46+
for (uint256 i = 0; i < numOfTokenBatches; i += 1) {
47+
if (_tokenId < indices[i]) {
48+
index = i;
49+
batchId = indices[i];
50+
51+
return (batchId, index);
52+
}
53+
}
54+
55+
revert("Invalid tokenId");
56+
}
57+
58+
/// @dev Returns the baseURI for a token. The intended metadata URI for the token is baseURI + tokenId.
59+
function _getBaseURI(uint256 _tokenId) internal view returns (string memory) {
60+
uint256 numOfTokenBatches = getBaseURICount();
61+
uint256[] memory indices = batchIds;
62+
63+
for (uint256 i = 0; i < numOfTokenBatches; i += 1) {
64+
if (_tokenId < indices[i]) {
65+
return baseURI[indices[i]];
66+
}
67+
}
68+
revert("Invalid tokenId");
69+
}
70+
71+
/// @dev Sets the base URI for the batch of tokens with the given batchId.
72+
function _setBaseURI(uint256 _batchId, string memory _baseURI) internal {
73+
baseURI[_batchId] = _baseURI;
74+
}
75+
76+
/// @dev Mints a batch of tokenIds and associates a common baseURI to all those Ids.
77+
function _batchMintMetadata(
78+
uint256 _startId,
79+
uint256 _amountToMint,
80+
string memory _baseURIForTokens
81+
) internal returns (uint256 nextTokenIdToMint, uint256 batchId) {
82+
batchId = _startId + _amountToMint;
83+
nextTokenIdToMint = batchId;
84+
85+
batchIds.push(batchId);
86+
87+
baseURI[batchId] = _baseURIForTokens;
88+
}
89+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
/// @author thirdweb
5+
6+
import "../../extension/interface/ILazyMint.sol";
7+
import "./BatchMintMetadata_V1.sol";
8+
9+
/**
10+
* The `LazyMint` is a contract extension for any base NFT contract. It lets you 'lazy mint' any number of NFTs
11+
* at once. Here, 'lazy mint' means defining the metadata for particular tokenIds of your NFT contract, without actually
12+
* minting a non-zero balance of NFTs of those tokenIds.
13+
*/
14+
15+
abstract contract LazyMint_V1 is ILazyMint, BatchMintMetadata_V1 {
16+
/// @notice The tokenId assigned to the next new NFT to be lazy minted.
17+
uint256 internal nextTokenIdToLazyMint;
18+
19+
/**
20+
* @notice Lets an authorized address lazy mint a given amount of NFTs.
21+
*
22+
* @param _amount The number of NFTs to lazy mint.
23+
* @param _baseURIForTokens The base URI for the 'n' number of NFTs being lazy minted, where the metadata for each
24+
* of those NFTs is `${baseURIForTokens}/${tokenId}`.
25+
* @param _data Additional bytes data to be used at the discretion of the consumer of the contract.
26+
* @return batchId A unique integer identifier for the batch of NFTs lazy minted together.
27+
*/
28+
function lazyMint(
29+
uint256 _amount,
30+
string calldata _baseURIForTokens,
31+
bytes calldata _data
32+
) public virtual override returns (uint256 batchId) {
33+
if (!_canLazyMint()) {
34+
revert("Not authorized");
35+
}
36+
37+
if (_amount == 0) {
38+
revert("0 amt");
39+
}
40+
41+
uint256 startId = nextTokenIdToLazyMint;
42+
43+
(nextTokenIdToLazyMint, batchId) = _batchMintMetadata(startId, _amount, _baseURIForTokens);
44+
45+
emit TokensLazyMinted(startId, startId + _amount - 1, _baseURIForTokens, _data);
46+
47+
return batchId;
48+
}
49+
50+
/// @dev Returns whether lazy minting can be performed in the given execution context.
51+
function _canLazyMint() internal view virtual returns (bool);
52+
}

contracts/legacy-contracts/pre-builts/SignatureDrop_V4.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import "../../extension/Royalty.sol";
2424
import "../../extension/PrimarySale.sol";
2525
import "../../extension/Ownable.sol";
2626
import "../../extension/DelayedReveal.sol";
27-
import "../../extension/LazyMint.sol";
27+
import "../extension/LazyMint_V1.sol";
2828
import "../../extension/PermissionsEnumerable.sol";
2929
import "../extension/DropSinglePhase_V1.sol";
3030
import "../../extension/SignatureMintERC721Upgradeable.sol";
@@ -37,7 +37,7 @@ contract SignatureDrop_V4 is
3737
PrimarySale,
3838
Ownable,
3939
DelayedReveal,
40-
LazyMint,
40+
LazyMint_V1,
4141
PermissionsEnumerable,
4242
DropSinglePhase_V1,
4343
SignatureMintERC721Upgradeable,

0 commit comments

Comments
 (0)