Skip to content

Commit ad487f2

Browse files
Implement LazyMintERC721 contract extension (#172)
* Implement LazyMintERC721 contract building block * add permissions * docs * add reverts * fix typo * fix conflict with OZ Strings lib, add overrideable tokenURI impl
1 parent 937b134 commit ad487f2

File tree

5 files changed

+193
-6
lines changed

5 files changed

+193
-6
lines changed

contracts/feature/LazyMintERC721.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.0;
3+
4+
import "./LazyMint.sol";
5+
import "../lib/TWStrings.sol";
6+
7+
abstract contract LazyMintERC721 is LazyMint {
8+
using TWStrings for uint256;
9+
10+
event TokensLazyMinted(uint256 startTokenId, uint256 endTokenId, string baseURI, bytes extraData);
11+
12+
/// @dev the next available non-minted token id
13+
uint256 public nextTokenIdToMint;
14+
15+
/// @dev lazy mint a batch of tokens
16+
function lazyMint(
17+
uint256 amount,
18+
string calldata baseURIForTokens,
19+
bytes calldata extraData
20+
) external virtual override returns (uint256 batchId) {
21+
require(amount > 0, "Amount must be greater than 0");
22+
require(_canLazyMint(), "Not authorized");
23+
uint256 startId = nextTokenIdToMint;
24+
(nextTokenIdToMint, batchId) = _batchMint(startId, amount, baseURIForTokens);
25+
emit TokensLazyMinted(startId, startId + amount, baseURIForTokens, extraData);
26+
}
27+
28+
/// @dev Returns the URI for a given tokenId
29+
function tokenURI(uint256 _tokenId) public view virtual returns (string memory) {
30+
string memory batchUri = getBaseURI(_tokenId);
31+
return string(abi.encodePacked(batchUri, _tokenId.toString()));
32+
}
33+
34+
/// @dev Returns whether lazy minting can be done in the given execution context.
35+
function _canLazyMint() internal virtual returns (bool);
36+
}

contracts/feature/Permissions.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
pragma solidity ^0.8.0;
33

44
import "./interface/IPermissions.sol";
5-
import "../lib/Strings.sol";
5+
import "../lib/TWStrings.sol";
66

77
contract Permissions is IPermissions {
88
mapping(bytes32 => mapping(address => bool)) private _hasRole;
@@ -68,9 +68,9 @@ contract Permissions is IPermissions {
6868
string(
6969
abi.encodePacked(
7070
"Permissions: account ",
71-
Strings.toHexString(uint160(account), 20),
71+
TWStrings.toHexString(uint160(account), 20),
7272
" is missing role ",
73-
Strings.toHexString(uint256(role), 32)
73+
TWStrings.toHexString(uint256(role), 32)
7474
)
7575
)
7676
);
@@ -83,9 +83,9 @@ contract Permissions is IPermissions {
8383
string(
8484
abi.encodePacked(
8585
"Permissions: account ",
86-
Strings.toHexString(uint160(account), 20),
86+
TWStrings.toHexString(uint160(account), 20),
8787
" is missing role ",
88-
Strings.toHexString(uint256(role), 32)
88+
TWStrings.toHexString(uint256(role), 32)
8989
)
9090
)
9191
);

contracts/lib/Strings.sol renamed to contracts/lib/TWStrings.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pragma solidity ^0.8.0;
66
/**
77
* @dev String operations.
88
*/
9-
library Strings {
9+
library TWStrings {
1010
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
1111

1212
/**

docs/LazyMintERC721.md

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# LazyMintERC721
2+
3+
4+
5+
6+
7+
8+
9+
10+
11+
## Methods
12+
13+
### getBaseURICount
14+
15+
```solidity
16+
function getBaseURICount() external view returns (uint256)
17+
```
18+
19+
20+
21+
*Returns the number of batches of tokens having the same baseURI.*
22+
23+
24+
#### Returns
25+
26+
| Name | Type | Description |
27+
|---|---|---|
28+
| _0 | uint256 | undefined
29+
30+
### getBatchIdAtIndex
31+
32+
```solidity
33+
function getBatchIdAtIndex(uint256 _index) external view returns (uint256)
34+
```
35+
36+
37+
38+
*Returns the id for the batch of tokens the given tokenId belongs to.*
39+
40+
#### Parameters
41+
42+
| Name | Type | Description |
43+
|---|---|---|
44+
| _index | uint256 | undefined
45+
46+
#### Returns
47+
48+
| Name | Type | Description |
49+
|---|---|---|
50+
| _0 | uint256 | undefined
51+
52+
### lazyMint
53+
54+
```solidity
55+
function lazyMint(uint256 amount, string baseURIForTokens, bytes extraData) external nonpayable returns (uint256 batchId)
56+
```
57+
58+
59+
60+
*lazy mint a batch of tokens*
61+
62+
#### Parameters
63+
64+
| Name | Type | Description |
65+
|---|---|---|
66+
| amount | uint256 | undefined
67+
| baseURIForTokens | string | undefined
68+
| extraData | bytes | undefined
69+
70+
#### Returns
71+
72+
| Name | Type | Description |
73+
|---|---|---|
74+
| batchId | uint256 | undefined
75+
76+
### nextTokenIdToMint
77+
78+
```solidity
79+
function nextTokenIdToMint() external view returns (uint256)
80+
```
81+
82+
83+
84+
*the next available non-minted token id*
85+
86+
87+
#### Returns
88+
89+
| Name | Type | Description |
90+
|---|---|---|
91+
| _0 | uint256 | undefined
92+
93+
### tokenURI
94+
95+
```solidity
96+
function tokenURI(uint256 _tokenId) external view returns (string)
97+
```
98+
99+
100+
101+
*Returns the URI for a given tokenId*
102+
103+
#### Parameters
104+
105+
| Name | Type | Description |
106+
|---|---|---|
107+
| _tokenId | uint256 | undefined
108+
109+
#### Returns
110+
111+
| Name | Type | Description |
112+
|---|---|---|
113+
| _0 | string | undefined
114+
115+
116+
117+
## Events
118+
119+
### TokensLazyMinted
120+
121+
```solidity
122+
event TokensLazyMinted(uint256 startTokenId, uint256 endTokenId, string baseURI, bytes extraData)
123+
```
124+
125+
126+
127+
128+
129+
#### Parameters
130+
131+
| Name | Type | Description |
132+
|---|---|---|
133+
| startTokenId | uint256 | undefined |
134+
| endTokenId | uint256 | undefined |
135+
| baseURI | string | undefined |
136+
| extraData | bytes | undefined |
137+
138+
139+

docs/TWStrings.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# TWStrings
2+
3+
4+
5+
6+
7+
8+
9+
*String operations.*
10+
11+
12+

0 commit comments

Comments
 (0)