erc721b
v0.2.1
Published
An improvement on the [ERC721A](https://github.com/chiru-labs/ERC721A) implementation. [Read more](https://www.badbabybearbots.com/erc721b.html).
Downloads
7
Readme
ERC721B
An improvement on the ERC721A implementation. Read more.
ERC721B is a stripped down version of ERC721. No bells, no whistles. Some key considerations are the following.
- Primary designed for cheaply mass minting by a special token id incrementer. If batch minting is not a concern, you might want to still use ERC721.
tokenURI()
in ERC721 cannot be used in all cases. It was added to satisfy theIERC721Metadata
interface. We moved this requirement topresets/ERC721BPresetStandard.sol
instead.name()
is stored though usually never changes. returning aname() pure
is more efficient. It was added to ERC721 in order to
satisfy theIERC721Metadata
interface. We moved this requirement toextensions/ERC721BMetadata.sol
instead.symbol()
is stored though usually never changes. returning asymbol() pure
is more efficient. It was added to ERC721 in order to
satisfy theIERC721Metadata
interface. We moved this requirement toextensions/ERC721BMetadata.sol
instead.
1. Install
$ npm i --save-dev erc721b
2. Usage
A basic example on how to inherit the ERC721B in your contract would
look like the following. This example uses ERC721BBaseTokenURI
to
generate a dynamic URI in tokenURI
import "@openzeppelin/contracts/access/Ownable.sol";
import "erc721b/contracts/extensions/ERC721BBaseTokenURI.sol";
contract MyCollection is Ownable, ERC721BBaseTokenURI
{
function mint(address to, uint256 quantity) external onlyOwner {
_safeMint(to, quantity);
}
function name() external view returns(string memory) {
return "My Collection";
}
function symbol() external view returns(string memory) {
return "MYC";
}
function supportsInterface(bytes4 interfaceId)
public view virtual override(ERC721B, IERC165) returns(bool)
{
return interfaceId == type(IERC721Metadata).interfaceId
|| super.supportsInterface(interfaceId);
}
}
If you would like to manually assign a token's URI you could use the following example.
import "@openzeppelin/contracts/access/Ownable.sol";
import "erc721b/contracts/extensions/ERC721BStaticTokenURI.sol";
contract MyCollection is Ownable, ERC721BBaseTokenURI
{
function mint(address to, uint256 quantity) external onlyOwner {
_safeMint(to, quantity);
}
function name() external view returns(string memory) {
return "My Collection";
}
function symbol() external view returns(string memory) {
return "MYC";
}
function setTokenURI(uint256 tokenId, string memory uri) external onlyOwner {
_setTokenURI(tokenId, uri);
}
function supportsInterface(bytes4 interfaceId)
public view virtual override(ERC721B, IERC165) returns(bool)
{
return interfaceId == type(IERC721Metadata).interfaceId
|| super.supportsInterface(interfaceId);
}
}
See presets for more examples.
3. Extensions
This library comes with the following extensions.
3A. ERC721BBaseTokenURI
Use this extension if you want token URIs to be determined with a base URI.
You need to manually call _setBaseURI(string memory)
in your constructor
or in another function.
3B. ERC721BBurnable
Use this extension if you want your tokens to be burnable. By default it is not.
3C. ERC721BContractURIStorage
Use this extension if you want your contract have an associated URI.
Marketplaces like OpenSea accept this. You need to manually call
_setContractURI(string memory)
in your constructor or in another
function.
3D. ERC721BPausable
Use this extension if you want manually pause minting and transferring.
You need to manually call _pause()
and _unpause()
in another function.
3E. ERC721BSignedTransfer
Use this extension if you want want a cheaper way to approve transfers.
signedTransferFrom()
is similar to safeTransferFrom()
except it
accepts a signed message from the owner as authorization.
3F. ERC721BStaticTokenURI
Use this extension if you want to assign static URIs to a token.
3G. ERC721Metadata
Use this extension if you want to assign store the name and symbol in
your contract. We separated this out because it's cheaper to use a pure
function like function name() pure view returns(string memory)
.
4. Auditing
Clone this repo in terminal and cd
to that folder. Run the following
commands.
$ cp .env.sample to .env
$ npm install
Sign up to CoinmarketCap and
generate an API key. In .env
to set the BLOCKCHAIN_CMC_KEY
to your
API key.
5. Testing
Make sure in .env
to set the BLOCKCHAIN_NETWORK
to hardhat
.
$ npm test
6. Reports
We've measured the gas costs and prices for minting, comparing OpenZeppelin's ERC721 vs ERC721A vs ERC721B. In our measurements, the same application-level logic is used.
The following example is an example cost conversion from the gas above in USD ($3,000/eth).
The following is an example gas report from the tests ran in this
project and could change based on the cost of ETH
itself.