Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Added in modular contracts documentation #538

Merged
merged 7 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"escape-string-regexp": "^5.0.0",
"flexsearch": "^0.7.43",
"github-slugger": "^2.0.0",
"lucide-react": "^0.286.0",
"lucide-react": "^0.416.0",
"next": "^14.2.3",
"nextjs-toploader": "^1.6.12",
"node-html-parser": "^6.1.13",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
import { Details } from "@doc";
import { GithubButtonLink } from "@doc";
import { createMetadata } from "@doc";

export const metadata = createMetadata({
title: "ERC1155Base | thirdweb contracts",
description:
"The ERC1155Base smart contract implements the ERC1155 NFT standard. It allows for minting NFTs to yourself (or to someone else) and selling those NFTs on a marketplace.",
image: {
title: "ERC1155Base contract",
icon: "contract",
},
});

# ERC1155Base

```solidity
import "@thirdweb-dev/contracts/base/ERC1155Base.sol";
```

The `ERC1155Base` smart contract implements the [ERC1155](https://eips.ethereum.org/EIPS/eip-1155) NFT standard.
It allows for minting NFTs to yourself (or to someone else) and selling those NFTs on a marketplace.

<GithubButtonLink href="https://github.com/thirdweb-dev/contracts/blob/main/contracts/base/ERC1155Base.sol" />

## Usage

Import the contract and inherit from it.

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@thirdweb-dev/contracts/base/ERC1155Base.sol";

contract MyNFT is ERC1155Base {
constructor(
address _defaultAdmin,
string memory _name,
string memory _symbol,
address _royaltyRecipient,
uint128 _royaltyBps
)
ERC1155Base(
_defaultAdmin,
_name,
_symbol,
_royaltyRecipient,
_royaltyBps
)
{}
}
```

## Detected Extensions

Once deployed, you can use the features made available by these extensions on the SDK and dashboard:

Click on each feature to learn more about what functions are available.

- [ERC1155](/contracts/build/extensions/erc-1155/ERC1155)
- [ERC1155Mintable](/contracts/build/extensions/erc-1155/ERC1155Mintable)
- [ERC1155BatchMintable](/contracts/build/extensions/erc-1155/ERC1155BatchMintable)
- [ERC1155Burnable](/contracts/build/extensions/erc-1155/ERC1155Burnable)
- [ERC1155Enumerable](/contracts/build/extensions/erc-1155/ERC1155Enumerable)
- [Royalty](/contracts/build/extensions/general/Royalty)
- [ContractMetadata](/contracts/build/extensions/general/ContractMetadata)
- [Ownable](/contracts/build/extensions/general/Ownable)

## Functions to Override

The following functions have been implemented on this contract & are available to be overridden to add custom logic:

<Details id="uri" summary="uri">

```solidity
/// @notice Returns the metadata URI for the given tokenId.
function uri(uint256 _tokenId) public view virtual override returns (string memory) {
string memory uriForToken = _uri[_tokenId];
if (bytes(uriForToken).length > 0) {
return uriForToken;
}

string memory batchUri = _getBaseURI(_tokenId);
return string(abi.encodePacked(batchUri, _tokenId.toString()));
}
```

</Details>

<Details id="mintTo" summary="mintTo">

```solidity
/**
* @notice Lets an authorized address mint NFTs to a recipient.
* @dev - The logic in the `_canMint` function determines whether the caller is authorized to mint NFTs.
* - If `_tokenId == type(uint256).max` a new NFT at tokenId `nextTokenIdToMint` is minted. If the given
* `tokenId < nextTokenIdToMint`, then additional supply of an existing NFT is being minted.
*
* @param _to The recipient of the NFTs to mint.
* @param _tokenId The tokenId of the NFT to mint.
* @param _tokenURI The full metadata URI for the NFTs minted (if a new NFT is being minted).
* @param _amount The amount of the same NFT to mint.
*/
function mintTo(
address _to,
uint256 _tokenId,
string memory _tokenURI,
uint256 _amount
) public virtual {
require(_canMint(), "Not authorized to mint.");

uint256 tokenIdToMint;
uint256 nextIdToMint = nextTokenIdToMint();

if (_tokenId == type(uint256).max) {
tokenIdToMint = nextIdToMint;
nextTokenIdToMint_ += 1;
_setTokenURI(nextIdToMint, _tokenURI);
} else {
require(_tokenId < nextIdToMint, "invalid id");
tokenIdToMint = _tokenId;
}

_mint(_to, tokenIdToMint, _amount, "");
}
```

</Details>

<Details id="batchMintTo" summary="batchMintTo">

```solidity
/**
* @notice Lets an authorized address mint multiple NEW NFTs at once to a recipient.
* @dev The logic in the `_canMint` function determines whether the caller is authorized to mint NFTs.
* If `_tokenIds[i] == type(uint256).max` a new NFT at tokenId `nextTokenIdToMint` is minted. If the given
* `tokenIds[i] < nextTokenIdToMint`, then additional supply of an existing NFT is minted.
* The metadata for each new NFT is stored at `baseURI/{tokenID of NFT}`
*
* @param _to The recipient of the NFT to mint.
* @param _tokenIds The tokenIds of the NFTs to mint.
* @param _amounts The amounts of each NFT to mint.
* @param _baseURI The baseURI for the `n` number of NFTs minted. The metadata for each NFT is `baseURI/tokenId`
*/
function batchMintTo(
address _to,
uint256[] memory _tokenIds,
uint256[] memory _amounts,
string memory _baseURI
) public virtual {
require(_canMint(), "Not authorized to mint.");
require(_amounts.length > 0, "Minting zero tokens.");
require(_tokenIds.length == _amounts.length, "Length mismatch.");

uint256 nextIdToMint = nextTokenIdToMint();
uint256 startNextIdToMint = nextIdToMint;

uint256 numOfNewNFTs;

for (uint256 i = 0; i < _tokenIds.length; i += 1) {
if (_tokenIds[i] == type(uint256).max) {
_tokenIds[i] = nextIdToMint;

nextIdToMint += 1;
numOfNewNFTs += 1;
} else {
require(_tokenIds[i] < nextIdToMint, "invalid id");
}
}

if (numOfNewNFTs > 0) {
_batchMintMetadata(startNextIdToMint, numOfNewNFTs, _baseURI);
}

nextTokenIdToMint_ = nextIdToMint;
_mintBatch(_to, _tokenIds, _amounts, "");

}

```

</Details>

<Details id="burn" summary="burn">

```solidity
/**
* @notice Lets an owner or approved operator burn NFTs of the given tokenId.
*
* @param _owner The owner of the NFT to burn.
* @param _tokenId The tokenId of the NFT to burn.
* @param _amount The amount of the NFT to burn.
*/
function burn(
address _owner,
uint256 _tokenId,
uint256 _amount
) external virtual {
address caller = msg.sender;

require(caller == _owner || isApprovedForAll[_owner][caller], "Unapproved caller");
require(balanceOf[_owner][_tokenId] >= _amount, "Not enough tokens owned");

_burn(_owner, _tokenId, _amount);
}
```

</Details>

<Details id="burnBatch" summary="burnBatch">

```solidity
/**
* @notice Lets an owner or approved operator burn NFTs of the given tokenIds.
*
* @param _owner The owner of the NFTs to burn.
* @param _tokenIds The tokenIds of the NFTs to burn.
* @param _amounts The amounts of the NFTs to burn.
*/
function burnBatch(
address _owner,
uint256[] memory _tokenIds,
uint256[] memory _amounts
) external virtual {
address caller = msg.sender;

require(caller == _owner || isApprovedForAll[_owner][caller], "Unapproved caller");
require(_tokenIds.length == _amounts.length, "Length mismatch");

for (uint256 i = 0; i < _tokenIds.length; i += 1) {
require(balanceOf[_owner][_tokenIds[i]] >= _amounts[i], "Not enough tokens owned");
}

_burnBatch(_owner, _tokenIds, _amounts);
}
```

</Details>

<Details id="_beforeTokenTransfer" summary="_beforeTokenTransfer">

```solidity
/// @dev Runs before every token transfer / mint / burn.
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual override {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);

if (from == address(0)) {
for (uint256 i = 0; i < ids.length; ++i) {
totalSupply[ids[i]] += amounts[i];
}
}

if (to == address(0)) {
for (uint256 i = 0; i < ids.length; ++i) {
totalSupply[ids[i]] -= amounts[i];
}
}
}
```

</Details>
Loading