Giter Site home page Giter Site logo

erc721r's Introduction

ERC721R

A fully-compliant implementation of IERC721 that selects token ids pseudo-randomly. You can read about its inspiration in this Medium article.

Usage

contract FashionHatPunks is ERC721r {
    // 10_000 is the number of tokens in the colletion
    constructor() ERC721r("Fashion Hat Punks", "HATPUNK", 10_000) {}
    
    function mint(uint quantity) {
        _mintRandom(msg.sender, quantity);
    }
}

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).

ERC721R pseudo-random numbers are generated like this:

uint256 randomNum = uint256(
    keccak256(
        abi.encode(
            mintTargetAddress,
            tx.gasprice,
            block.number,
            block.timestamp,
            block.difficulty,
            blockhash(block.number - 1),
            address(this),
            updatedNumAvailableTokens
        )
    )
);

Miners can game this, but the assumption is that in most cases it will not be worth the cost on their part.

Finally, ERC721R exposes public totalSupply() and maxSupply() functions.

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.

Limitations

  • At this time ERC721R does not include safeMint() functionality. This is the result of the original author's position that the benefits of preventing users from minting to the wrong contract are outweighed by the cost of increasing surface area for reentrancy attacks.
  • ERC721R disallows contracts from minting for security reasons. Read more.

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.

License

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

erc721r's People

Contributors

rogerpodacter avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.