30 Solidity怎么能正常获取销毁后所有者名下的所有ERC721数组

Solidity完整的代码,请问需要怎么改才能正常获取销毁后所有者名下的所有NFT数组,请帮忙看一下 实在改不出来。

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";

contract Yinka is AccessControl,  ERC721URIStorage  {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    event OpenBox(uint256 leixing,uint256 create);

    struct Prop{
        uint16 power;        
    }

    mapping(uint=>Prop) public props;

    constructor() ERC721("Yinka", "Yinka") {
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _grantRole(MINTER_ROLE, msg.sender);
    }

    function tokenIds() public view returns(uint256){
        return _tokenIds.current();
    }

    function burn(uint256 tokenId) public virtual {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721Burnable: caller is not owner nor approved");
        _burn(tokenId);
    }

    function mintYinka(address player, string memory tokenURI)public onlyRole(MINTER_ROLE) returns(uint256){
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        Prop memory i;
        i.power=uint16(rand(100,0));
        props[newItemId]=i;
        _mint(player, newItemId);
        _setTokenURI(newItemId, tokenURI);
        return newItemId;
    }

    function getByOwner(address _owner) external view returns(uint256[] memory) {
        uint256[] memory result = new uint256[](balanceOf(_owner));
        uint256 counter = 0;
        for (uint256 i = 1; i <= _tokenIds.current(); i++) {
            if (ownerOf(i) == _owner) {
                result[counter] = i;
                counter++;
            }
        }
        return result;
    }

    function setRole(address addr) public onlyRole(DEFAULT_ADMIN_ROLE){
        _grantRole(MINTER_ROLE,addr);
    }

    function supportsInterface(bytes4 interfaceId)public view override(ERC721, AccessControl)returns (bool){
        return super.supportsInterface(interfaceId);
    }

    function rand(uint256 _length,uint256 _nonce) public view returns(uint256) {
        uint256 random = uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp,_nonce)));
        return random%_length+1;
    }
}
请先 登录 后评论

最佳答案 2022-06-24 08:40

function getList(address _addr, uint256 startIndex, uint256 endIndex) public view returns (uint256[]memory idArr){
        uint256 ba = balanceOf(_addr);
        require(startIndex <= endIndex, "err1");
        require(endIndex < ba, "err2");
        uint len = endIndex.sub(startIndex).add(1);
        idArr = new uint256[](len);
        uint index;
        for (; startIndex <= endIndex; startIndex++) {
            uint256 nftId = tokenOfOwnerByIndex(_addr, startIndex);
            idArr[index] = nftId;
            index++;
        }
    }
请先 登录 后评论

其它 0 个回答

  • 0 关注
  • 0 收藏,2405 浏览
  • 五哈希 提出于 2022-06-23 12:18