Git Product home page Git Product logo

Comments (2)

0xClandestine avatar 0xClandestine commented on May 21, 2024 1

Would be interesting to create token extensions for solmate πŸ˜‹

Since there's not an existing ERC721 contract in this repo I thought I'd drop a reference ERC721Permit extension here instead:

// SPDX-License-Identifier: WTFPL
pragma solidity >=0.8.0;

import "solmate/tokens/ERC721.sol";

abstract contract ERC721Permit is ERC721 {

    // ----------------------------------------------------------
    // Mutables
    // ----------------------------------------------------------

    mapping(uint256 => uint256) public nonces;

    // ----------------------------------------------------------
    // Immutables
    // ----------------------------------------------------------

    uint256 internal immutable INITIAL_CHAIN_ID;

    bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

    constructor(
        string memory _name, 
        string memory _symbol
    ) ERC721(_name, _symbol) {

        INITIAL_CHAIN_ID = block.chainid;
        
        INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
    }

    // ----------------------------------------------------------
    // Actions
    // ----------------------------------------------------------

    function permit(
        address spender,
        uint256 id,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) public virtual {

        require(spender != address(0), "INVALID_SPENDER");

        require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

        // Unchecked because the only math done is incrementing
        // the tokens's nonce which cannot realistically overflow.
        unchecked {
            address recoveredAddress = ecrecover(
                keccak256(
                    abi.encodePacked(
                        "\x19\x01",
                        DOMAIN_SEPARATOR(),
                        keccak256(
                            abi.encode(
                                keccak256("Permit(address spender,uint256 id,uint256 nonce,uint256 deadline)"),
                                spender,
                                id,
                                nonces[id]++,
                                deadline
                            )
                        )
                    )
                ),
                v,
                r,
                s
            );

            require(recoveredAddress != address(0) && recoveredAddress == _ownerOf[id], "INVALID_SIGNER");

            getApproved[id] = spender;

            emit Approval(recoveredAddress, spender, id);
        }
    }

    // ----------------------------------------------------------
    // Viewables
    // ----------------------------------------------------------

    function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
        return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
    }

    function computeDomainSeparator() internal view virtual returns (bytes32) {
        return
            keccak256(
                abi.encode(
                    keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                    keccak256(bytes(name)),
                    keccak256("1"),
                    block.chainid,
                    address(this)
                )
            );
    }
}

from solady.

z0r0z avatar z0r0z commented on May 21, 2024

closed as token extensions seem out of scope of solady utilities πŸ•Ί

from solady.

Related Issues (20)

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.