ERC721A 智能合约

ERC721A smart contract

我正在编写 ERC721A 智能合约并在 Remix IDE 中收到警告。有代码:

function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
    if (index >= balanceOf(owner)) revert OwnerIndexOutOfBounds();
    uint256 numMintedSoFar = totalSupply();
    uint256 tokenIdsIdx;
    address currOwnershipAddr;

    // Counter overflow is impossible as the loop breaks when uint256 i is equal to another uint256 numMintedSoFar.
    unchecked {
        for (uint256 i; i < numMintedSoFar; i++) {
            TokenOwnership memory ownership = _ownerships[i];
            if (ownership.addr != address(0)) {
                currOwnershipAddr = ownership.addr;
            }
            if (currOwnershipAddr == owner) {
                if (tokenIdsIdx == index) {
                    return i;
                }
                tokenIdsIdx++;
            }
        }
    }

    // Execution should never reach this point.
    assert(false);
}

此代码来自 Square Bears 系列 (https://etherscan.io/address/0x2b1037def2aa4ed427627903bdef9bdd27ae1ea3#code)。我是从 YouTube 教程中得到的。我认为代码有效,但我不断收到警告。

Warning: Unnamed return variable can remain unassigned. Add an explicit return with value to all non-reverting code paths or name the variable.
--> contracts/ERC721A.sol:103:94:
|
103 | function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
| ^^^^^^^

我假设我必须提供一个命名的 return 值或变量,但代码似乎 return 一个迭代值 (i)。

因为您已经告诉编译器您将 return 来自函数的值,但您没有。

你应该return一个模拟值,即使你不需要。

   ...

    // Execution should never reach this point.
    assert(false);

    return tokenIdsIdx; // or simply return 0;
}