Skip to content

add queryable extension to ERC721Base#560

Merged
nkrishang merged 6 commits into
thirdweb-dev:mainfrom
WhiteOakKong:erc721base-add-queryable
Nov 20, 2023
Merged

add queryable extension to ERC721Base#560
nkrishang merged 6 commits into
thirdweb-dev:mainfrom
WhiteOakKong:erc721base-add-queryable

Conversation

@WhiteOakKong

Copy link
Copy Markdown
Contributor

No description provided.

* @title ERC721A Queryable
* @dev ERC721A subclass with convenience query functions.
*/
abstract contract ERC721AQueryable is ERC721A, IERC721AQueryable {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we already have this file somewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't we already have this file somewhere?

There isn't a non-upgradable version currently.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha

@WhiteOakKong WhiteOakKong marked this pull request as ready for review October 27, 2023 22:58

@joaquim-verges joaquim-verges left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's the contract sizes? Bases should have a bit of wiggle room to add custom fns

@WhiteOakKong

Copy link
Copy Markdown
Contributor Author

How's the contract sizes? Bases should have a bit of wiggle room to add custom fns

ERC721Base has 13kb in left over space. The queryable adds ~1kb.

@joaquim-verges

Copy link
Copy Markdown
Member

Should be plenty. Ship it

Signed-off-by: WhiteOakKong <92236155+WhiteOakKong@users.noreply.github.com>
@joaquim-verges

Copy link
Copy Markdown
Member

@nkrishang @kumaryash90 can you review?

*/
function tokensOfOwner(address owner) external view override returns (uint256[] memory) {
unchecked {
uint256 tokenIdsIdx;

Check warning

Code scanning / Slither

Uninitialized local variables

ERC721AQueryable.tokensOfOwner(address).tokenIdsIdx (contracts/eip/queryable/ERC721AQueryable.sol#148) is a local variable never initialized
* - `burned = `false`
*/
function explicitOwnershipOf(uint256 tokenId) public view override returns (TokenOwnership memory) {
TokenOwnership memory ownership;

Check warning

Code scanning / Slither

Uninitialized local variables

ERC721AQueryable.explicitOwnershipOf(uint256).ownership (contracts/eip/queryable/ERC721AQueryable.sol#34) is a local variable never initialized
function tokensOfOwner(address owner) external view override returns (uint256[] memory) {
unchecked {
uint256 tokenIdsIdx;
address currOwnershipAddr;

Check warning

Code scanning / Slither

Uninitialized local variables

ERC721AQueryable.tokensOfOwner(address).currOwnershipAddr (contracts/eip/queryable/ERC721AQueryable.sol#149) is a local variable never initialized
Comment on lines +73 to +132
function tokensOfOwnerIn(
address owner,
uint256 start,
uint256 stop
) external view override returns (uint256[] memory) {
unchecked {
if (start >= stop) revert InvalidQueryRange();
uint256 tokenIdsIdx;
uint256 stopLimit = _currentIndex;
// Set `start = max(start, _startTokenId())`.
if (start < _startTokenId()) {
start = _startTokenId();
}
// Set `stop = min(stop, _currentIndex)`.
if (stop > stopLimit) {
stop = stopLimit;
}
uint256 tokenIdsMaxLength = balanceOf(owner);
// Set `tokenIdsMaxLength = min(balanceOf(owner), stop - start)`,
// to cater for cases where `balanceOf(owner)` is too big.
if (start < stop) {
uint256 rangeLength = stop - start;
if (rangeLength < tokenIdsMaxLength) {
tokenIdsMaxLength = rangeLength;
}
} else {
tokenIdsMaxLength = 0;
}
uint256[] memory tokenIds = new uint256[](tokenIdsMaxLength);
if (tokenIdsMaxLength == 0) {
return tokenIds;
}
// We need to call `explicitOwnershipOf(start)`,
// because the slot at `start` may not be initialized.
TokenOwnership memory ownership = explicitOwnershipOf(start);
address currOwnershipAddr;
// If the starting slot exists (i.e. not burned), initialize `currOwnershipAddr`.
// `ownership.address` will not be zero, as `start` is clamped to the valid token ID range.
if (!ownership.burned) {
currOwnershipAddr = ownership.addr;
}
for (uint256 i = start; i != stop && tokenIdsIdx != tokenIdsMaxLength; ++i) {
ownership = _ownerships[i];
if (ownership.burned) {
continue;
}
if (ownership.addr != address(0)) {
currOwnershipAddr = ownership.addr;
}
if (currOwnershipAddr == owner) {
tokenIds[tokenIdsIdx++] = i;
}
}
// Downsize the array to fit.
assembly {
mstore(tokenIds, tokenIdsIdx)
}
return tokenIds;
}
}

Check warning

Code scanning / Slither

Assembly usage

ERC721AQueryable.tokensOfOwnerIn(address,uint256,uint256) (contracts/eip/queryable/ERC721AQueryable.sol#73-132) uses assembly - INLINE ASM (contracts/eip/queryable/ERC721AQueryable.sol#127-129)
@codecov

codecov Bot commented Oct 30, 2023

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ade8109) 62.34% compared to head (125c8c0) 57.43%.
Report is 1 commits behind head on main.

❗ Current head 125c8c0 differs from pull request most recent head 105e734. Consider uploading reports for the commit 105e734 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #560      +/-   ##
==========================================
- Coverage   62.34%   57.43%   -4.91%     
==========================================
  Files         216      215       -1     
  Lines        6647     6614      -33     
==========================================
- Hits         4144     3799     -345     
- Misses       2503     2815     +312     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nkrishang nkrishang enabled auto-merge (squash) November 20, 2023 17:49
@nkrishang nkrishang merged commit 8da4090 into thirdweb-dev:main Nov 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants