Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Created February 28, 2022 08:25
Show Gist options
  • Save z0r0z/3f7407b763aebe83f826ca550997272c to your computer and use it in GitHub Desktop.
Save z0r0z/3f7407b763aebe83f826ca550997272c to your computer and use it in GitHub Desktop.
make your NFTs compatible with more smart contracts that expect erc20s with this 1 weird trick
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.4;
import 'https://github.com/Rari-Capital/solmate/blob/main/src/tokens/ERC721.sol';
error NotOwner();
error InvalidRecipient();
contract EIP4521 is ERC721("WEIRD", "WEIRD") {
constructor() {
_mint(msg.sender, 1);
}
function tokenURI(uint256) public override pure returns (string memory) {
return "PLACEHOLDER";
}
/// -----------------------------------------------------------------------
/// ERC-20-like Logic (EIP-4521)
/// -----------------------------------------------------------------------
function transfer(address to, uint256 tokenId) public virtual returns (bool) {
if (msg.sender != ownerOf[tokenId]) revert NotOwner();
if (to == address(0)) revert InvalidRecipient();
// underflow of the sender's balance is impossible because we check for
// ownership above and the recipient's balance can't realistically overflow
unchecked {
balanceOf[msg.sender]--;
balanceOf[to]++;
}
delete getApproved[tokenId];
ownerOf[tokenId] = to;
emit Transfer(msg.sender, to, tokenId);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment