From 5caecf548c04c97955b8f0487ceb804fab0e2ca1 Mon Sep 17 00:00:00 2001 From: Aniket <30843294+Aniket-Engg@users.noreply.github.com> Date: Thu, 6 Dec 2018 02:15:27 +0530 Subject: [PATCH] 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> --- contracts/mocks/ERC721FullMock.sol | 8 ++++++-- contracts/token/ERC721/ERC721Enumerable.sol | 9 +++++++++ test/token/ERC721/ERC721Full.test.js | 9 +++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/contracts/mocks/ERC721FullMock.sol b/contracts/mocks/ERC721FullMock.sol index 5199ef41753..66999755759 100644 --- a/contracts/mocks/ERC721FullMock.sol +++ b/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); } diff --git a/contracts/token/ERC721/ERC721Enumerable.sol b/contracts/token/ERC721/ERC721Enumerable.sol index ff8c1b9eecc..95622b75917 100644 --- a/contracts/token/ERC721/ERC721Enumerable.sol +++ b/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]; + } } diff --git a/test/token/ERC721/ERC721Full.test.js b/test/token/ERC721/ERC721Full.test.js index 6789ca28aea..933f624eeb0 100644 --- a/test/token/ERC721/ERC721Full.test.js +++ b/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);