Skip to content

Conversation

kien-ngo
Copy link
Contributor

@kien-ngo kien-ngo commented Sep 28, 2023

Problem solved

Add unit test for ERC721 allOwners() to ensure it does not return AddressZero as the owner
@jnsdls @joaquim-verges I'm practicing writing unit tests. Let me know whether this is encouraged or unecessary.

The function targeted for unit test:
path: packages\sdk\src\evm\core\classes\erc-721-supply.ts

  /**
   * Return all the owners of each token id in this contract
   * @returns
   */
  public async allOwners() {
    let totalCount: BigNumber;
    let startTokenId = BigNumber.from(0);
    if (hasFunction<OpenEditionERC721>("startTokenId", this.contractWrapper)) {
      startTokenId = await this.contractWrapper.read("startTokenId", []);
    }
    try {
      totalCount = await this.erc721.totalClaimedSupply();
    } catch (e) {
      totalCount = await this.totalCount();
    }

    totalCount = totalCount.add(startTokenId);

    // TODO use multicall3 if available
    // TODO can't call toNumber() here, this can be a very large number
    const arr = [...new Array(totalCount.toNumber()).keys()];
    const owners = await Promise.all(
      arr.map((i) => this.erc721.ownerOf(i).catch(() => constants.AddressZero)),
    );
    return arr
      .map((i) => ({
        tokenId: i,
        owner: owners[i],
      }))
      .filter((o) => o.owner !== constants.AddressZero); // <----------
  }

Changes made

  • Public API changes: list the public API changes made if any
  • Internal API changes: explain the internal logic changes

How to test

  • Automated tests: link to unit test file
  • Manual tests: step by step instructions on how to test

@changeset-bot
Copy link

changeset-bot bot commented Sep 28, 2023

🦋 Changeset detected

Latest commit: fa8942d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@thirdweb-dev/sdk Patch
thirdweb Patch
@thirdweb-dev/react-core Patch
@thirdweb-dev/react-native Patch
@thirdweb-dev/react Patch
@thirdweb-dev/unity-js-bridge Patch
@thirdweb-dev/wallets Patch
@thirdweb-dev/auth Patch
@thirdweb-dev/react-native-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codecov
Copy link

codecov bot commented Sep 28, 2023

Codecov Report

All modified lines are covered by tests ✅

see 2 files with indirect coverage changes

📢 Thoughts on this report? Let us know!.

@kien-ngo kien-ngo marked this pull request as ready for review September 28, 2023 07:10
@kien-ngo kien-ngo requested a review from a team September 28, 2023 07:10
Copy link
Member

@joaquim-verges joaquim-verges left a comment

Choose a reason for hiding this comment

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

the more unit tests the better! thanks for doing this

@joaquim-verges joaquim-verges added this pull request to the merge queue Sep 28, 2023
Merged via the queue into main with commit de05c2d Sep 28, 2023
@joaquim-verges joaquim-verges deleted the kien/unit_test branch September 28, 2023 12:40
@github-actions github-actions bot mentioned this pull request Sep 28, 2023
jnsdls pushed a commit that referenced this pull request Jun 19, 2024
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.

2 participants