Skip to content

Commit

Permalink
Using Solady BitMaps instead of solidity-bits (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: nidhhoggr <persie.joseph@gmail.com>
  • Loading branch information
nidhhoggr and nidhhoggr authored Jan 16, 2023
1 parent 4dda527 commit 4accc83
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 72 deletions.
10 changes: 5 additions & 5 deletions contracts/ERC721Psi.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";
import "./interface/IERC721Psi.sol";

/**
Expand All @@ -35,16 +35,16 @@ contract ERC721Psi is IERC721Psi {

using Address for address;
using Strings for uint256;
using BitMaps for BitMaps.BitMap;
using LibBitmap for LibBitmap.Bitmap;

BitMaps.BitMap private _batchHead;
LibBitmap.Bitmap private _batchHead;

string private _name;
string private _symbol;

// Mapping from token ID to owner address
mapping(uint256 => address) internal _owners;
uint256 private _currentIndex;
uint256 internal _currentIndex;

mapping(uint256 => address) private _tokenApprovals;
mapping(address => mapping(address => bool)) private _operatorApprovals;
Expand Down Expand Up @@ -518,7 +518,7 @@ contract ERC721Psi is IERC721Psi {
}

function _getBatchHead(uint256 tokenId) internal view returns (uint256 tokenIdBatchHead) {
tokenIdBatchHead = _batchHead.scanForward(tokenId);
tokenIdBatchHead = _batchHead.findLastSet(tokenId);
}


Expand Down
6 changes: 3 additions & 3 deletions contracts/ERC721PsiUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {ERC721PsiStorage} from "./storage/ERC721PsiStorage.sol";
import "./storage/ERC721PsiInitializable.sol";
import "@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol";
import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";

interface ERC721PsiUpgradeable__IERC721Receiver {
function onERC721Received(
Expand All @@ -34,7 +34,7 @@ contract ERC721PsiUpgradeable is ERC721PsiInitializable, IERC721Psi {
using ERC721PsiStorage for ERC721PsiStorage.Layout;
using AddressUpgradeable for address;
using StringsUpgradeable for uint256;
using BitMaps for BitMaps.BitMap;
using LibBitmap for LibBitmap.Bitmap;

// The mask of the lower 160 bits for addresses.
uint256 private constant _BITMASK_ADDRESS = (1 << 160) - 1;
Expand Down Expand Up @@ -512,7 +512,7 @@ contract ERC721PsiUpgradeable is ERC721PsiInitializable, IERC721Psi {
}

function _getBatchHead(uint256 tokenId) internal view returns (uint256 tokenIdBatchHead) {
tokenIdBatchHead = ERC721PsiStorage.layout()._batchHead.scanForward(tokenId);
tokenIdBatchHead = ERC721PsiStorage.layout()._batchHead.findLastSet(tokenId);
}


Expand Down
1 change: 0 additions & 1 deletion contracts/extension/ERC721PsiAddressData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "../ERC721Psi.sol";

/**
Expand Down
1 change: 0 additions & 1 deletion contracts/extension/ERC721PsiAddressDataUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*/
pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "../ERC721PsiUpgradeable.sol";
import {ERC721PsiAddressDataStorage} from "../storage/ERC721PsiAddressDataStorage.sol";

Expand Down
8 changes: 4 additions & 4 deletions contracts/extension/ERC721PsiBatchMetaData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pragma solidity ^0.8.0;


import "../ERC721Psi.sol";
import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";

abstract contract ERC721PsiBatchMetaData is ERC721Psi {
using BitMaps for BitMaps.BitMap;
BitMaps.BitMap private _metaDataBatchHead;
using LibBitmap for LibBitmap.Bitmap;
LibBitmap.Bitmap private _metaDataBatchHead;

function _safeMint(
address to,
Expand All @@ -35,6 +35,6 @@ abstract contract ERC721PsiBatchMetaData is ERC721Psi {
* The returned tokenId will remain the same after the token transfer.
*/
function _getMetaDataBatchHead(uint256 tokenId) internal view returns (uint256 tokenIdMetaDataBatchHead) {
tokenIdMetaDataBatchHead = _metaDataBatchHead.scanForward(tokenId);
tokenIdMetaDataBatchHead = _metaDataBatchHead.findLastSet(tokenId);
}
}
5 changes: 2 additions & 3 deletions contracts/extension/ERC721PsiBatchMetaDataUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ pragma solidity ^0.8.0;


import "../ERC721PsiUpgradeable.sol";
import "solidity-bits/contracts/BitMaps.sol";
import {ERC721PsiBatchMetaDataStorage} from "../storage/ERC721PsiBatchMetaDataStorage.sol";

abstract contract ERC721PsiBatchMetaDataUpgradeable is ERC721PsiUpgradeable {
using ERC721PsiBatchMetaDataStorage for ERC721PsiBatchMetaDataStorage.Layout;
using BitMaps for BitMaps.BitMap;
using LibBitmap for LibBitmap.Bitmap;

function _safeMint(
address to,
Expand All @@ -36,6 +35,6 @@ abstract contract ERC721PsiBatchMetaDataUpgradeable is ERC721PsiUpgradeable {
* The returned tokenId will remain the same after the token transfer.
*/
function _getMetaDataBatchHead(uint256 tokenId) internal view returns (uint256 tokenIdMetaDataBatchHead) {
tokenIdMetaDataBatchHead = ERC721PsiBatchMetaDataStorage.layout()._metaDataBatchHead.scanForward(tokenId);
tokenIdMetaDataBatchHead = ERC721PsiBatchMetaDataStorage.layout()._metaDataBatchHead.findLastSet(tokenId);
}
}
26 changes: 5 additions & 21 deletions contracts/extension/ERC721PsiBurnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
*/
pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";
import "../ERC721Psi.sol";

abstract contract ERC721PsiBurnable is ERC721Psi {
using BitMaps for BitMaps.BitMap;
BitMaps.BitMap private _burnedToken;
using LibBitmap for LibBitmap.Bitmap;
LibBitmap.Bitmap private _burnedToken;

/**
* @dev Destroys `tokenId`.
Expand Down Expand Up @@ -75,29 +75,13 @@ abstract contract ERC721PsiBurnable is ERC721Psi {
* @dev See {IERC721Enumerable-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalMinted() - _burned();
return _currentIndex - _burned() - _startTokenId();
}

/**
* @dev Returns number of token burned.
*/
function _burned() internal view returns (uint256 burned){
uint256 startBucket = _startTokenId() >> 8;
uint256 lastBucket = (_nextTokenId() >> 8) + 1;

for(uint256 i=startBucket; i < lastBucket; i++) {
uint256 bucket = _burnedToken.getBucket(i);
burned += _popcount(bucket);
}
}

/**
* @dev Returns number of set bits.
*/
function _popcount(uint256 x) private pure returns (uint256 count) {
unchecked{
for (count=0; x!=0; count++)
x &= x - 1;
}
return _burnedToken.popCount( _startTokenId(), _totalMinted());
}
}
24 changes: 4 additions & 20 deletions contracts/extension/ERC721PsiBurnableUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
*/
pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";
import "../ERC721PsiUpgradeable.sol";
import {ERC721PsiBurnableStorage} from "../storage/ERC721PsiBurnableStorage.sol";

abstract contract ERC721PsiBurnableUpgradeable is ERC721PsiUpgradeable {
using ERC721PsiBurnableStorage for ERC721PsiBurnableStorage.Layout;
using BitMaps for BitMaps.BitMap;
using LibBitmap for LibBitmap.Bitmap;

/**
* @dev Destroys `tokenId`.
Expand Down Expand Up @@ -76,29 +76,13 @@ abstract contract ERC721PsiBurnableUpgradeable is ERC721PsiUpgradeable {
* @dev See {IERC721Enumerable-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return _totalMinted() - _burned();
return _totalMinted() - _burned();
}

/**
* @dev Returns number of token burned.
*/
function _burned() internal view returns (uint256 burned){
uint256 startBucket = _startTokenId() >> 8;
uint256 lastBucket = (_nextTokenId() >> 8) + 1;

for(uint256 i=startBucket; i < lastBucket; i++) {
uint256 bucket = ERC721PsiBurnableStorage.layout()._burnedToken.getBucket(i);
burned += _popcount(bucket);
}
}

/**
* @dev Returns number of set bits.
*/
function _popcount(uint256 x) private pure returns (uint256 count) {
unchecked{
for (count=0; x!=0; count++)
x &= x - 1;
}
return ERC721PsiBurnableStorage.layout()._burnedToken.popCount( _startTokenId(), _totalMinted());
}
}
5 changes: 2 additions & 3 deletions contracts/storage/ERC721PsiBatchMetaDataStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";

library ERC721PsiBatchMetaDataStorage {
using BitMaps for BitMaps.BitMap;

struct Layout {
BitMaps.BitMap _metaDataBatchHead;
LibBitmap.Bitmap _metaDataBatchHead;
}

bytes32 internal constant STORAGE_SLOT = keccak256('ERC721Psi.contracts.storage.BatchMetaData');
Expand Down
5 changes: 2 additions & 3 deletions contracts/storage/ERC721PsiBurnableStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";

library ERC721PsiBurnableStorage {
using BitMaps for BitMaps.BitMap;

struct Layout {
BitMaps.BitMap _burnedToken;
LibBitmap.Bitmap _burnedToken;
}

bytes32 internal constant STORAGE_SLOT = keccak256('ERC721Psi.contracts.storage.Burnable');
Expand Down
4 changes: 2 additions & 2 deletions contracts/storage/ERC721PsiStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.0;

import "solidity-bits/contracts/BitMaps.sol";
import "solady/src/utils/LibBitmap.sol";

library ERC721PsiStorage {

Expand All @@ -20,7 +20,7 @@ library ERC721PsiStorage {
// Mapping from owner to operator approvals
mapping(address => mapping(address => bool)) _operatorApprovals;
// stores batchhead
BitMaps.BitMap _batchHead;
LibBitmap.Bitmap _batchHead;
}

bytes32 internal constant STORAGE_SLOT = keccak256('ERC721Psi.contracts.storage.ERC721Psi');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@chainlink/contracts": "^0.4.0",
"@openzeppelin/contracts": "^4.4.2",
"@openzeppelin/contracts-upgradeable": "^4.5.1",
"solidity-bits": "^0.3.2"
"solady": "^0.0.62"
},
"devDependencies": {
"@nomicfoundation/hardhat-chai-matchers": "^1.0.5",
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9642,6 +9642,11 @@ socks@^2.6.1:
ip "^1.1.5"
smart-buffer "^4.2.0"

solady@^0.0.62:
version "0.0.62"
resolved "https://registry.yarnpkg.com/solady/-/solady-0.0.62.tgz#f2ae706c5cbdd412f101a624536f6eafb0bbb032"
integrity sha512-L32eTb/CeZOsolx4aLL3x1egEYKT0soDUTy6BCWO5nEUIIG16idse2M0/2rT/dhfgXxuGYQZuXFQboS17OWKWA==

solc@0.7.3:
version "0.7.3"
resolved "https://registry.yarnpkg.com/solc/-/solc-0.7.3.tgz#04646961bd867a744f63d2b4e3c0701ffdc7d78a"
Expand Down Expand Up @@ -9687,11 +9692,6 @@ solidity-ast@^0.4.15:
resolved "https://registry.yarnpkg.com/solidity-ast/-/solidity-ast-0.4.30.tgz#402d8277311d6680c786f756ba27e1c19f809293"
integrity sha512-3xsQIbZEPx6w7+sQokuOvk1RkMb5GIpuK0GblQDIH6IAkU4+uyJQVJIRNP+8KwhzkViwRKq0hS4zLqQNLKpxOA==

solidity-bits@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/solidity-bits/-/solidity-bits-0.3.2.tgz#0d631b06214ca19b01de23a6b35077183ec0e20b"
integrity sha512-RpqHF9g5i3bW6gr0KxKTsuYNJqZfPko6EFhq5z1V19Wsj/xatZNdq/Z9W4wFZRfhKijxbEyQXj9ePbcJM3tBkg==

source-map-resolve@^0.5.0:
version "0.5.3"
resolved "https://registry.yarnpkg.com/source-map-resolve/-/source-map-resolve-0.5.3.tgz#190866bece7553e1f8f267a2ee82c606b5509a1a"
Expand Down

0 comments on commit 4accc83

Please sign in to comment.