npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@middlemarch/erc721r

v1.0.1

Published

A base contract for minting NFTs pseudo randomly

Downloads

3

Readme

ERC721r

An ERC721 base contract that mints tokens in a pseudo-random order. Because the token is revealed in the same transaction as the mint itself, this contract creates a fun but not fully secure experience.

This contract is for entertainment purposes only!

People using Flashbots will be able to predict what they will get and decide not to go through with the mint if they don't like it. There might also be other exploits I am not aware of because the "instant reveal" mint style is impossible to securely randomize.

If randomness is an important part of your app, you should strongly consider moving to a commit-reveal scheme where the user pays in a different transaction from the one in which they learn what NFT they got. Unfortunately this costs more gas and is less fun, so weigh this against the benefit of increased security.

If you want to learn more, this MouseDev thread is a good place to start.

Usage

yarn add @middlemarch/erc721r or npm install @middlemarch/erc721r


import {ERC721r} from "@middlemarch/erc721r/contracts/ERC721r.sol";

contract FashionHatPunks is ERC721r {
    // 10_000 is the collection's maxSupply
    constructor() ERC721r("Fashion Hat Punks", "HATPUNK", 10_000) {}
    
    // You must implement tokenURI
    function tokenURI(uint tokenId) public view override returns (string memory) {
        return "some uri";
    }
    
    function mint(uint quantity) external {
        // ERC721r exposes a public numberMinted(address) that you can optionally use
        // to, e.g., enforce limits instead of using a separate mapping(address => uint)
        // which is more expensive
        require(numberMinted(msg.sender) + quantity <= 10, "Limit 10 per address");
        
        // You do *not* need to do this. ERC721r handles it.
        // require(totalSupply() + quantity <= maxSupply())
        
        _mintRandom(msg.sender, quantity);
    }
}

ERC721r exposes public maxSupply(), totalSupply() and remainingSupply() functions automatically.

It inherits from Solady's ERC721 so you also get _getExtraData(), _setExtraData(), _getAux(), and _setAux(). However you should be aware that the auxes are used internally by ERC721R, so you should not use them in your own contract. Instead, use _setExtraAddressData() and _getExtraAddressData().

There is also the function _mintAtIndex(address to, uint index) which allows you to mint non-randomly, but it will only behave as you expect if you:

  1. Use it before minting randomly
  2. Mint non-random tokens in decreasing order of id. E.g., if you want to mint id 200 to one person and id 100 to another person, you should mint id 200 first (because 200 > 100).

Notes

  • ERC721r assumes the first id is 0
  • Max balance is type(uint32).max
  • Contracts cannot mint

Implementation notes

ERC721r uses the modern version of the Fisher–Yates shuffle which stores the list of available tokens and the list of minted tokens in one data structure to save gas.

Credits

This contract was extracted from Fashion Hat Punks, where it was first used. The core logic and code was copied from CryptoPhunksV2. The repository structure was copied from ERC721A. You can read about its inspiration in this Medium article.

License

Copyright (c) 2022 Tom Lehman. ERC721R is released under the MIT License.