Pārlūkot izejas kodu

getter added for an array of tokens held by an owner (#1522)

* signing prefix added

* Minor improvement

* Tests changed

* Successfully tested

* Minor improvements

* Minor improvements

* Revert "Dangling commas are now required. (#1359)"

This reverts commit a6889776f46adca374b6ebf014aa7b0038112a9d.

* updates

* fixes #1404

* approve failing test

* suggested changes done

* ISafeERC20 removed

* conflict fixes

* fixes #1512

* Update test/token/ERC721/ERC721Full.test.js

Co-Authored-By: Aniket-Engg <30843294+Aniket-Engg@users.noreply.github.com>
Aniket 6 gadi atpakaļ
vecāks
revīzija
5caecf548c

+ 6 - 2
contracts/mocks/ERC721FullMock.sol

@@ -7,8 +7,8 @@ import "../token/ERC721/ERC721Burnable.sol";
 
 /**
  * @title ERC721FullMock
- * This mock just provides a public mint and burn functions for testing purposes,
- * and a public setter for metadata URI
+ * This mock just provides public functions for setting metadata URI, getting all tokens of an owner,
+ * checking token existence, removal of a token from an address 
  */
 contract ERC721FullMock is ERC721Full, ERC721Mintable, ERC721MetadataMintable, ERC721Burnable {
     constructor (string name, string symbol) public ERC721Mintable() ERC721Full(name, symbol) {}
@@ -17,6 +17,10 @@ contract ERC721FullMock is ERC721Full, ERC721Mintable, ERC721MetadataMintable, E
         return _exists(tokenId);
     }
 
+    function tokensOfOwner(address owner) public view returns (uint256[] memory) {
+        return _tokensOfOwner(owner);
+    }
+
     function setTokenURI(uint256 tokenId, string uri) public {
         _setTokenURI(tokenId, uri);
     }

+ 9 - 0
contracts/token/ERC721/ERC721Enumerable.sol

@@ -144,4 +144,13 @@ contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
         _allTokensIndex[tokenId] = 0;
         _allTokensIndex[lastToken] = tokenIndex;
     }
+
+    /**
+     * @dev Gets the list of token IDs of the requested owner
+     * @param owner address owning the tokens 
+     * @return uint256[] List of token IDs owned by the requested address
+     */
+    function _tokensOfOwner(address owner) internal view returns (uint256[] storage) {
+        return _ownedTokens[owner];
+    }
 }

+ 9 - 0
test/token/ERC721/ERC721Full.test.js

@@ -138,6 +138,15 @@ contract('ERC721Full', function ([
       });
     });
 
+    describe('tokensOfOwner', function () {
+      it('returns total tokens of owner', async function () {
+        const tokenIds = await this.token.tokensOfOwner(owner);
+        tokenIds.length.should.equal(2);
+        tokenIds[0].should.be.bignumber.equal(firstTokenId);
+        tokenIds[1].should.be.bignumber.equal(secondTokenId);
+      });
+    });
+
     describe('totalSupply', function () {
       it('returns total token supply', async function () {
         (await this.token.totalSupply()).should.be.bignumber.equal(2);