Skip to content

Commit

Permalink
Implement LazyMintERC721 contract extension (#172)
Browse files Browse the repository at this point in the history
* Implement LazyMintERC721 contract building block

* add permissions

* docs

* add reverts

* fix typo

* fix conflict with OZ Strings lib, add overrideable tokenURI impl
  • Loading branch information
joaquim-verges authored Jun 8, 2022
1 parent 937b134 commit ad487f2
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 6 deletions.
36 changes: 36 additions & 0 deletions contracts/feature/LazyMintERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "./LazyMint.sol";
import "../lib/TWStrings.sol";

abstract contract LazyMintERC721 is LazyMint {
using TWStrings for uint256;

event TokensLazyMinted(uint256 startTokenId, uint256 endTokenId, string baseURI, bytes extraData);

/// @dev the next available non-minted token id
uint256 public nextTokenIdToMint;

/// @dev lazy mint a batch of tokens
function lazyMint(
uint256 amount,
string calldata baseURIForTokens,
bytes calldata extraData
) external virtual override returns (uint256 batchId) {
require(amount > 0, "Amount must be greater than 0");
require(_canLazyMint(), "Not authorized");
uint256 startId = nextTokenIdToMint;
(nextTokenIdToMint, batchId) = _batchMint(startId, amount, baseURIForTokens);
emit TokensLazyMinted(startId, startId + amount, baseURIForTokens, extraData);
}

/// @dev Returns the URI for a given tokenId
function tokenURI(uint256 _tokenId) public view virtual returns (string memory) {
string memory batchUri = getBaseURI(_tokenId);
return string(abi.encodePacked(batchUri, _tokenId.toString()));
}

/// @dev Returns whether lazy minting can be done in the given execution context.
function _canLazyMint() internal virtual returns (bool);
}
10 changes: 5 additions & 5 deletions contracts/feature/Permissions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity ^0.8.0;

import "./interface/IPermissions.sol";
import "../lib/Strings.sol";
import "../lib/TWStrings.sol";

contract Permissions is IPermissions {
mapping(bytes32 => mapping(address => bool)) private _hasRole;
Expand Down Expand Up @@ -68,9 +68,9 @@ contract Permissions is IPermissions {
string(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(account), 20),
TWStrings.toHexString(uint160(account), 20),
" is missing role ",
Strings.toHexString(uint256(role), 32)
TWStrings.toHexString(uint256(role), 32)
)
)
);
Expand All @@ -83,9 +83,9 @@ contract Permissions is IPermissions {
string(
abi.encodePacked(
"Permissions: account ",
Strings.toHexString(uint160(account), 20),
TWStrings.toHexString(uint160(account), 20),
" is missing role ",
Strings.toHexString(uint256(role), 32)
TWStrings.toHexString(uint256(role), 32)
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion contracts/lib/Strings.sol → contracts/lib/TWStrings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pragma solidity ^0.8.0;
/**
* @dev String operations.
*/
library Strings {
library TWStrings {
bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";

/**
Expand Down
139 changes: 139 additions & 0 deletions docs/LazyMintERC721.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
# LazyMintERC721









## Methods

### getBaseURICount

```solidity
function getBaseURICount() external view returns (uint256)
```



*Returns the number of batches of tokens having the same baseURI.*


#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined

### getBatchIdAtIndex

```solidity
function getBatchIdAtIndex(uint256 _index) external view returns (uint256)
```



*Returns the id for the batch of tokens the given tokenId belongs to.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| _index | uint256 | undefined

#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined

### lazyMint

```solidity
function lazyMint(uint256 amount, string baseURIForTokens, bytes extraData) external nonpayable returns (uint256 batchId)
```



*lazy mint a batch of tokens*

#### Parameters

| Name | Type | Description |
|---|---|---|
| amount | uint256 | undefined
| baseURIForTokens | string | undefined
| extraData | bytes | undefined

#### Returns

| Name | Type | Description |
|---|---|---|
| batchId | uint256 | undefined

### nextTokenIdToMint

```solidity
function nextTokenIdToMint() external view returns (uint256)
```



*the next available non-minted token id*


#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined

### tokenURI

```solidity
function tokenURI(uint256 _tokenId) external view returns (string)
```



*Returns the URI for a given tokenId*

#### Parameters

| Name | Type | Description |
|---|---|---|
| _tokenId | uint256 | undefined

#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | string | undefined



## Events

### TokensLazyMinted

```solidity
event TokensLazyMinted(uint256 startTokenId, uint256 endTokenId, string baseURI, bytes extraData)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| startTokenId | uint256 | undefined |
| endTokenId | uint256 | undefined |
| baseURI | string | undefined |
| extraData | bytes | undefined |



12 changes: 12 additions & 0 deletions docs/TWStrings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# TWStrings







*String operations.*



0 comments on commit ad487f2

Please sign in to comment.