diff --git a/contracts/mocks/DetailedERC721TokenMock.sol b/contracts/mocks/DetailedERC721TokenMock.sol new file mode 100644 index 00000000000..9fa5b40f8e3 --- /dev/null +++ b/contracts/mocks/DetailedERC721TokenMock.sol @@ -0,0 +1,12 @@ +pragma solidity ^0.4.18; + +import "./ERC721TokenMock.sol"; +import "../token/DetailedERC721Token.sol"; + +/** + * @title DetailedERC721TokenMock + * This mock just provides a public mint and burn functions for testing purposes. + */ +contract DetailedERC721TokenMock is ERC721TokenMock, DetailedERC721Token { + function DetailedERC721TokenMock(string name, string symbol) DetailedERC721Token(name, symbol) public { } +} diff --git a/contracts/mocks/OperatableERC721TokenMock.sol b/contracts/mocks/OperatableERC721TokenMock.sol new file mode 100644 index 00000000000..f635f7c9b82 --- /dev/null +++ b/contracts/mocks/OperatableERC721TokenMock.sol @@ -0,0 +1,18 @@ +pragma solidity ^0.4.18; + +import "./DetailedERC721TokenMock.sol"; +import "../token/OperatableERC721Token.sol"; + +/** + * @title OperatableERC721TokenMock + * We needed to implement this mock contract just to provide a new interface renaming the overloaded functions of + * the OperatableERC721Token contract. This was needed since Truffle does not support calling overloaded functions. + * Please see https://github.com/trufflesuite/truffle/issues/737 + */ +contract OperatableERC721TokenMock is OperatableERC721Token, DetailedERC721TokenMock { + function OperatableERC721TokenMock(string name, string symbol) DetailedERC721TokenMock(name, symbol) public { } + + function transferAndCall(address _to, uint _tokenId, bytes _data) public { + super.transfer(_to, _tokenId, _data); + } +} diff --git a/contracts/token/DetailedERC721.sol b/contracts/token/DetailedERC721.sol new file mode 100644 index 00000000000..029eac7af45 --- /dev/null +++ b/contracts/token/DetailedERC721.sol @@ -0,0 +1,16 @@ +pragma solidity ^0.4.18; + +import "./ERC721.sol"; + +/** + * @title Detailed ERC721 interface + * @dev see https://github.com/ethereum/eips/issues/721 + */ +contract DetailedERC721 is ERC721 { + function name() public view returns (string _name); + function symbol() public view returns (string _symbol); + function implementsERC721() public pure returns (bool); + function setTokenMetadata(uint256 _tokenId, string _metadata) public; + function tokenMetadata(uint256 _tokenId) public view returns (string infoUrl); + function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256 _tokenId); +} diff --git a/contracts/token/DetailedERC721Token.sol b/contracts/token/DetailedERC721Token.sol new file mode 100644 index 00000000000..14ae2749882 --- /dev/null +++ b/contracts/token/DetailedERC721Token.sol @@ -0,0 +1,85 @@ +pragma solidity ^0.4.18; + +import "./ERC721Token.sol"; +import "./DetailedERC721.sol"; + +/** + * @title Detailed ERC721 Token + * This implementation includes all the required and optional functionality of the ERC721 standard + * @dev see https://github.com/ethereum/eips/issues/721 + */ +contract DetailedERC721Token is DetailedERC721, ERC721Token { + // Token name + string private _name; + + // Token symbol + string private _symbol; + + // Token metadata + mapping (uint256 => string) private _metadata; + + // Event triggered every time a token metadata gets updated + event MetadataUpdated(address _owner, uint256 _tokenId, string _newMetadata); + + /** + * @dev Constructor function + */ + function DetailedERC721Token(string name, string symbol) public { + _name = name; + _symbol = symbol; + } + + /** + * @dev Gets the token name + * @return string representing the token name + */ + function name() public view returns (string) { + return _name; + } + + /** + * @dev Gets the token symbol + * @return string representing the token symbol + */ + function symbol() public view returns (string) { + return _symbol; + } + + /** + * @dev Ensures this contract is an ERC721 implementation + * @return true to ensure this contract implements ERC721 functionality + */ + function implementsERC721() public pure returns (bool) { + return true; + } + + /** + * @dev Gets the token ID at a given index of the tokens list of the requested owner + * @param _owner address owning the tokens list to be accessed + * @param _index uint256 representing the index to be accessed of the requested tokens list + * @return uint256 token ID at the given index of the tokens list owned by the requested address + */ + function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256) { + require(_index < balanceOf(_owner)); + return tokensOf(_owner)[_index]; + } + + /** + * @dev Gets the metadata of the given token ID + * @param _tokenId uint256 ID of the token to query the metadata of + * @return string representing the metadata of the given token ID + */ + function tokenMetadata(uint256 _tokenId) public view returns (string) { + return _metadata[_tokenId]; + } + + /** + * @dev Sets the metadata of the given token ID + * @param _tokenId uint256 ID of the token to set the metadata of + * @param _newMetadata string representing the new metadata to be set + */ + function setTokenMetadata(uint256 _tokenId, string _newMetadata) public onlyOwnerOf(_tokenId) { + _metadata[_tokenId] = _newMetadata; + MetadataUpdated(msg.sender, _tokenId, _newMetadata); + } +} diff --git a/contracts/token/OperatableERC721.sol b/contracts/token/OperatableERC721.sol new file mode 100644 index 00000000000..4ac7deb7783 --- /dev/null +++ b/contracts/token/OperatableERC721.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.4.18; + +import "./DetailedERC721.sol"; + +/** + * @title ERC721 interface + * @dev see https://github.com/ethereum/eips/issues/721 + */ +contract OperatableERC721 is DetailedERC721 { + function setOperatorApproval(address _to, bool _approved) public; + function isOperatorApprovedFor(address _owner, address _operator) public view returns (bool); + function transfer(address _to, uint _tokenId, bytes _data) public; + function takeOwnershipFor(address _to, uint256 _tokenId) public; +} diff --git a/contracts/token/OperatableERC721Token.sol b/contracts/token/OperatableERC721Token.sol new file mode 100644 index 00000000000..9a9d5f7493c --- /dev/null +++ b/contracts/token/OperatableERC721Token.sol @@ -0,0 +1,66 @@ +pragma solidity ^0.4.18; + +import "./OperatableERC721.sol"; +import "./DetailedERC721Token.sol"; + +/** + * @title Operatable ERC721 Token + * Extended implementation of the ERC721 standard including approveAll and transfer&call functionalities + * Inspired by Decentraland's and Dharma's implementation + */ +contract OperatableERC721Token is OperatableERC721, DetailedERC721Token { + + // Mapping from owner to operator approvals + mapping (address => mapping (address => bool)) private operatorApprovals; + + /** + * @dev Sets the approval of a given operator + * @param _to operator address to set the approval + * @param _approved representing the status of the approval to be set + */ + function setOperatorApproval(address _to, bool _approved) public { + require(_to != msg.sender); + operatorApprovals[msg.sender][_to] = _approved; + } + + /** + * @dev Tells whether an operator is approved by a given owner + * @param _owner owner address which you want to query the approval of + * @param _operator operator address which you want to query the approval of + * @return bool whether the given operator is approved by the given owner + */ + function isOperatorApprovedFor(address _owner, address _operator) public view returns (bool) { + return operatorApprovals[_owner][_operator]; + } + + /** + * @dev Transfer the ownership of a given token ID to another contract address and calls it with given data + * @param _to address to receive the ownership of the given token ID + * @param _tokenId uint256 ID of the token to be transferred + * @param _data bytes of data to be sent when the receiving address gets called + */ + function transfer(address _to, uint _tokenId, bytes _data) public onlyOwnerOf(_tokenId) { + clearApprovalAndTransfer(msg.sender, _to, _tokenId); + require(_to.call(_data)); + } + + /** + * @dev Claims the ownership of a given token ID for a given recipient + * @param _to address which you want to transfer the token to + * @param _tokenId uint256 ID of the token being claimed by the msg.sender + */ + function takeOwnershipFor(address _to, uint256 _tokenId) public { + require(isApprovedFor(_tokenId)); + clearApprovalAndTransfer(ownerOf(_tokenId), _to, _tokenId); + } + + /** + * @dev Tells whether the msg.sender is approved for the given token ID or + * if he is an operator approved by the owner of the token + * @param _tokenId uint256 ID of the token to query the approval of + * @return bool whether the msg.sender is approved for the given token ID or not + */ + function isApprovedFor(uint256 _tokenId) internal view returns (bool) { + return super.isApprovedFor(_tokenId) || isOperatorApprovedFor(ownerOf(_tokenId), msg.sender); + } +} diff --git a/package-lock.json b/package-lock.json index 293b8a79598..2edfc005a89 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2548,6 +2548,31 @@ "integrity": "sha1-mh4Wnq00q3XgifUMpRK/0PvRJlU=", "dev": true }, + "ethereumjs-abi": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/ethereumjs-abi/-/ethereumjs-abi-0.6.5.tgz", + "integrity": "sha1-WmN+8Wq0NHP6cqKa2QhxQFs/UkE=", + "dev": true, + "requires": { + "bn.js": "4.11.8", + "ethereumjs-util": "4.5.0" + }, + "dependencies": { + "ethereumjs-util": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-4.5.0.tgz", + "integrity": "sha1-PpQosxfuvaPXJg2FT93alUsfG8Y=", + "dev": true, + "requires": { + "bn.js": "4.11.8", + "create-hash": "1.1.3", + "keccakjs": "0.2.1", + "rlp": "2.0.0", + "secp256k1": "3.3.0" + } + } + } + }, "ethereumjs-account": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/ethereumjs-account/-/ethereumjs-account-2.0.4.tgz", @@ -7308,7 +7333,7 @@ "truffle": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/truffle/-/truffle-4.0.1.tgz", - "integrity": "sha512-PybO+GMq3AvsfCWfEx4sbuaJlDL19iR8Ff20cO0TtP599N5JbMLlhwlffvVInPgFjP+F11vjSOYj3hT8fONs5A==", + "integrity": "sha1-2GYaStemyglLdRfSmxmcYObd5mU=", "dev": true, "requires": { "mocha": "3.5.3", @@ -7348,7 +7373,7 @@ "mocha": { "version": "3.5.3", "resolved": "https://registry.npmjs.org/mocha/-/mocha-3.5.3.tgz", - "integrity": "sha512-/6na001MJWEtYxHOV1WLfsmR4YIynkUEhBwzsb+fk2qmQ3iqsi258l/Q2MWHJMImAcNpZ8DEdYAK72NHoIQ9Eg==", + "integrity": "sha1-HgSA/jbS2lhY0etqzDhBiybqog0=", "dev": true, "requires": { "browser-stdout": "1.3.0", diff --git a/package.json b/package.json index f15f6397b2b..c947a6887c7 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "eslint-plugin-node": "^5.2.1", "eslint-plugin-promise": "^3.6.0", "eslint-plugin-standard": "^3.0.1", + "ethereumjs-abi": "^0.6.5", "ethereumjs-testrpc": "^6.0.1", "ethereumjs-util": "^5.1.2", "mocha-lcov-reporter": "^1.3.0", diff --git a/test/token/DetailedERC721Token.test.js b/test/token/DetailedERC721Token.test.js new file mode 100644 index 00000000000..dc3cf52ba4d --- /dev/null +++ b/test/token/DetailedERC721Token.test.js @@ -0,0 +1,133 @@ +import assertRevert from '../helpers/assertRevert'; +const BigNumber = web3.BigNumber; +const DetailedERC721Token = artifacts.require('DetailedERC721TokenMock.sol'); + +require('chai') + .use(require('chai-as-promised')) + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('DetailedERC721Token', accounts => { + let token = null; + const _name = 'Non Fungible Token'; + const _symbol = 'NFT'; + const _firstTokenId = 1; + const _secondTokenId = 2; + const _unknownTokenId = 3; + const _creator = accounts[0]; + + beforeEach(async function () { + token = await DetailedERC721Token.new(_name, _symbol, { from: _creator }); + await token.publicMint(_creator, _firstTokenId, { from: _creator }); + await token.publicMint(_creator, _secondTokenId, { from: _creator }); + }); + + describe('name', function () { + it('has a name', async function () { + const name = await token.name(); + name.should.be.equal(_name); + }); + }); + + describe('symbol', function () { + it('has a symbol', async function () { + const symbol = await token.symbol(); + symbol.should.be.equal(_symbol); + }); + }); + + describe('tokenOfOwnerByIndex', function () { + describe('when the given address owns some tokens', function () { + const owner = _creator; + + describe('when the given index is lower than the amount of tokens owned by the given address', function () { + const index = 0; + + it('returns the token ID placed at the given index', async function () { + const tokenId = await token.tokenOfOwnerByIndex(owner, index); + tokenId.should.be.bignumber.equal(_firstTokenId); + }); + }); + + describe('when the index is greater than or equal to the total tokens owned by the given address', function () { + const index = 2; + + it('reverts', async function () { + await assertRevert(token.tokenOfOwnerByIndex(owner, index)); + }); + }); + }); + + describe('when the given address does not own any token', function () { + const owner = accounts[1]; + + it('reverts', async function () { + await assertRevert(token.tokenOfOwnerByIndex(owner, 0)); + }); + }); + }); + + describe('tokenMetadata', function () { + describe('when no metadata was set', function () { + it('the given token has no metadata', async function () { + const metadata = await token.tokenMetadata(_firstTokenId); + + metadata.should.be.equal(''); + }); + }); + + describe('when some metadata was set', function () { + it('returns the metadata of the given token', async function () { + await token.setTokenMetadata(_firstTokenId, 'dummy metadata', { from: _creator }); + + const metadata = await token.tokenMetadata(_firstTokenId); + + metadata.should.be.equal('dummy metadata'); + }); + }); + }); + + describe('setTokenMetadata', function () { + describe('when the sender is not the token owner', function () { + const sender = accounts[1]; + + it('reverts', async function () { + await assertRevert(token.setTokenMetadata(_firstTokenId, 'new metadata', { from: sender })); + }); + }); + + describe('when the sender is the owner of the token', function () { + const sender = _creator; + + describe('when the given token ID was tracked by this contract before', function () { + const tokenId = _firstTokenId; + + it('updates the metadata of the given token ID', async function () { + await token.setTokenMetadata(_firstTokenId, 'new metadata', { from: sender }); + + const metadata = await token.tokenMetadata(tokenId); + + metadata.should.be.equal('new metadata'); + }); + + it('emits a metadata updated event', async function () { + const { logs } = await token.setTokenMetadata(tokenId, 'new metadata', { from: sender }); + + logs.length.should.be.equal(1); + logs[0].event.should.be.eq('MetadataUpdated'); + logs[0].args._owner.should.be.equal(sender); + logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + logs[0].args._newMetadata.should.be.equal('new metadata'); + }); + }); + + describe('when the given token ID was not tracked by this contract before', function () { + const tokenId = _unknownTokenId; + + it('reverts', async function () { + await assertRevert(token.setTokenMetadata(tokenId, 'new metadata'), { from: sender }); + }); + }); + }); + }); +}); diff --git a/test/token/OperatableERC721Token.test.js b/test/token/OperatableERC721Token.test.js new file mode 100644 index 00000000000..5bb24c71916 --- /dev/null +++ b/test/token/OperatableERC721Token.test.js @@ -0,0 +1,497 @@ +import abi from 'ethereumjs-abi'; +import assertRevert from '../helpers/assertRevert'; +const BigNumber = web3.BigNumber; +const SimpleToken = artifacts.require('SimpleToken.sol'); +const OperatableERC721Token = artifacts.require('OperatableERC721TokenMock.sol'); + +require('chai') + .use(require('chai-as-promised')) + .use(require('chai-bignumber')(BigNumber)) + .should(); + +contract('OperatableERC721Token', accounts => { + let token = null; + const _name = 'Non Fungible Token'; + const _symbol = 'NFT'; + const _firstTokenId = 1; + const _secondTokenId = 2; + const _unknownTokenId = 3; + const _creator = accounts[0]; + const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'; + + beforeEach(async function () { + token = await OperatableERC721Token.new(_name, _symbol, { from: _creator }); + await token.publicMint(_creator, _firstTokenId, { from: _creator }); + await token.publicMint(_creator, _secondTokenId, { from: _creator }); + }); + + describe('transfer and call', async function () { + let data; + let erc20; // we are using an erc20 token to test transfer and call + + beforeEach(async function () { + erc20 = await SimpleToken.new({ from: _creator }); + const balance = web3.toHex(await erc20.balanceOf(_creator)); + await erc20.approve(token.address, balance); + const args = ['address', 'address', 'uint256']; + const methodId = '0x' + abi.methodID('transferFrom', args).toString('hex'); + const params = abi.rawEncode(args, [_creator, token.address, balance]).toString('hex'); + data = methodId + params; + }); + + describe('when the given token ID was tracked by this token', function () { + const tokenId = _firstTokenId; + + describe('when the msg.sender is the owner of the given token ID', function () { + const sender = _creator; + + describe('when the address to transfer the token to is not the zero address', function () { + it('transfers the ownership of the given token ID to the given address', async function () { + const to = erc20.address; + await token.transferAndCall(to, tokenId, data, { from: sender }); + + const newOwner = await token.ownerOf(tokenId); + newOwner.should.be.equal(to); + }); + + it('clears the approval for the token ID', async function () { + const to = erc20.address; + await token.approve(accounts[2], tokenId, { from: sender }); + + await token.transferAndCall(to, tokenId, data, { from: sender }); + + const approvedAccount = await token.approvedFor(tokenId); + approvedAccount.should.be.equal(ZERO_ADDRESS); + }); + + it('emits an approval and transfer events', async function () { + const to = erc20.address; + const { logs } = await token.transferAndCall(to, tokenId, data, { from: sender }); + + logs.length.should.be.equal(3); + + logs[0].event.should.be.eq('Approval'); + logs[0].args._owner.should.be.equal(sender); + logs[0].args._approved.should.be.equal(ZERO_ADDRESS); + logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + + logs[1].event.should.be.eq('Transfer'); + logs[1].args._from.should.be.equal(sender); + logs[1].args._to.should.be.equal(to); + logs[1].args._tokenId.should.be.bignumber.equal(tokenId); + + logs[2].event.should.be.eq('Transfer'); + }); + + it('adjusts owners balances', async function () { + const to = erc20.address; + const previousBalance = await token.balanceOf(sender); + await token.transferAndCall(to, tokenId, data, { from: sender }); + + const newOwnerBalance = await token.balanceOf(to); + newOwnerBalance.should.be.bignumber.equal(1); + + const previousOwnerBalance = await token.balanceOf(_creator); + previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1); + }); + + it('places the last token of the sender in the position of the transferred token', async function () { + const to = erc20.address; + const firstTokenIndex = 0; + const lastTokenIndex = await token.balanceOf(_creator) - 1; + const lastToken = await token.tokenOfOwnerByIndex(_creator, lastTokenIndex); + + await token.transferAndCall(to, tokenId, data, { from: sender }); + + const swappedToken = await token.tokenOfOwnerByIndex(_creator, firstTokenIndex); + swappedToken.should.be.bignumber.equal(lastToken); + await assertRevert(token.tokenOfOwnerByIndex(_creator, lastTokenIndex)); + }); + + it('adds the token to the tokens list of the new owner', async function () { + const to = erc20.address; + await token.transferAndCall(to, tokenId, data, { from: sender }); + + const tokenIDs = await token.tokensOf(to); + tokenIDs.length.should.be.equal(1); + tokenIDs[0].should.be.bignumber.equal(tokenId); + }); + + it('calls the receiver with the given data', async function () { + const to = erc20.address; + const previousBalance = await erc20.balanceOf(_creator); + await token.transferAndCall(to, tokenId, data, { from: sender }); + + const contractBalance = await erc20.balanceOf(token.address); + contractBalance.should.be.bignumber.equal(previousBalance); + }); + }); + + describe('when the address to transfer the token to is the zero address', function () { + const to = ZERO_ADDRESS; + + it('reverts', async function () { + await assertRevert(token.transferAndCall(to, 0, data, { from: _creator })); + }); + }); + }); + + describe('when the msg.sender is not the owner of the given token ID', function () { + const sender = accounts[2]; + + it('reverts', async function () { + await assertRevert(token.transferAndCall(accounts[1], tokenId, data, { from: sender })); + }); + }); + }); + + describe('when the given token ID was not tracked by this token', function () { + let tokenId = _unknownTokenId; + + it('reverts', async function () { + await assertRevert(token.transferAndCall(accounts[1], tokenId, data, { from: _creator })); + }); + }); + }); + + describe('setOperatorApproval', function () { + const sender = _creator; + + describe('when the operator willing to approve is not the owner', function () { + const operator = accounts[1]; + + describe('when there is no operator approval set by the sender', function () { + it('approves the operator and does not emit an approval event', async function () { + const { logs } = await token.setOperatorApproval(operator, true, { from: sender }); + + const isApproved = await token.isOperatorApprovedFor(sender, operator); + isApproved.should.be.true; + logs.length.should.be.equal(0); + }); + }); + + describe('when the operator was set as not approved', function () { + it('approves the operator and does not emit an approval event', async function () { + const { logs } = await token.setOperatorApproval(operator, true, { from: sender }); + + const isApproved = await token.isOperatorApprovedFor(sender, operator); + isApproved.should.be.true; + logs.length.should.be.equal(0); + }); + + it('can unset the operator approval', async function () { + await token.setOperatorApproval(operator, false, { from: sender }); + + const isApproved = await token.isOperatorApprovedFor(sender, operator); + isApproved.should.be.false; + }); + }); + + describe('when the operator was already approved', function () { + beforeEach(async function () { + await token.setOperatorApproval(operator, true, { from: sender }); + }); + + it('keeps the approval to the given address and does not emit an approval event', async function () { + const { logs } = await token.setOperatorApproval(operator, true, { from: sender }); + + const isApproved = await token.isOperatorApprovedFor(sender, operator); + isApproved.should.be.true; + logs.length.should.be.equal(0); + }); + }); + }); + + describe('when the operator is the owner', function () { + const operator = _creator; + + it('reverts', async function () { + await assertRevert(token.setOperatorApproval(operator, true, { from: sender })); + }); + }); + }); + + describe('takeOwnership', function () { + const tokenId = _firstTokenId; + + describe('when the sender was approved by the owner of the token ID', function () { + const sender = accounts[1]; + + beforeEach(async function () { + await token.setOperatorApproval(sender, true, { from: _creator }); + }); + + it('transfers the ownership of the given token ID to the given address', async function () { + await token.takeOwnership(tokenId, { from: sender }); + + const newOwner = await token.ownerOf(tokenId); + newOwner.should.be.equal(sender); + }); + + it('clears the approval for the token ID', async function () { + await token.takeOwnership(tokenId, { from: sender }); + + const approvedAccount = await token.approvedFor(tokenId); + approvedAccount.should.be.equal(ZERO_ADDRESS); + }); + + it('emits an approval and transfer events', async function () { + const { logs } = await token.takeOwnership(tokenId, { from: sender }); + + logs.length.should.be.equal(2); + + logs[0].event.should.be.eq('Approval'); + logs[0].args._owner.should.be.equal(_creator); + logs[0].args._approved.should.be.equal(ZERO_ADDRESS); + logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + + logs[1].event.should.be.eq('Transfer'); + logs[1].args._from.should.be.equal(_creator); + logs[1].args._to.should.be.equal(sender); + logs[1].args._tokenId.should.be.bignumber.equal(tokenId); + }); + + it('adjusts owners balances', async function () { + const previousBalance = await token.balanceOf(_creator); + + await token.takeOwnership(tokenId, { from: sender }); + + const newOwnerBalance = await token.balanceOf(sender); + newOwnerBalance.should.be.bignumber.equal(1); + + const previousOwnerBalance = await token.balanceOf(_creator); + previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1); + }); + + it('places the last token of the sender in the position of the transferred token', async function () { + const firstTokenIndex = 0; + const lastTokenIndex = await token.balanceOf(_creator) - 1; + const lastToken = await token.tokenOfOwnerByIndex(_creator, lastTokenIndex); + + await token.takeOwnership(tokenId, { from: sender }); + + const swappedToken = await token.tokenOfOwnerByIndex(_creator, firstTokenIndex); + swappedToken.should.be.bignumber.equal(lastToken); + await assertRevert(token.tokenOfOwnerByIndex(_creator, lastTokenIndex)); + }); + + it('adds the token to the tokens list of the new owner', async function () { + await token.takeOwnership(tokenId, { from: sender }); + + const tokenIDs = await token.tokensOf(sender); + tokenIDs.length.should.be.equal(1); + tokenIDs[0].should.be.bignumber.equal(tokenId); + }); + }); + }); + + describe('takeOwnershipFor', function () { + describe('when the given token ID was already tracked by this contract', function () { + const tokenId = _firstTokenId; + + describe('when the sender has the approval for the token ID', function () { + const sender = accounts[1]; + + beforeEach(async function () { + await token.approve(sender, tokenId, { from: _creator }); + }); + + describe('when the recipient is the zero address', function () { + const to = ZERO_ADDRESS; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(to, tokenId, { from: sender })); + }); + }); + + describe('when the recipient is the owner', function () { + const to = _creator; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(to, tokenId, { from: sender })); + }); + }); + + describe('when the recipient is not the zero address neither the owner', function () { + const to = accounts[2]; + + it('transfers the ownership of the given token ID to the given address', async function () { + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const newOwner = await token.ownerOf(tokenId); + newOwner.should.be.equal(to); + }); + + it('clears the approval for the token ID', async function () { + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const approvedAccount = await token.approvedFor(tokenId); + approvedAccount.should.be.equal(ZERO_ADDRESS); + }); + + it('emits an approval and transfer events', async function () { + const { logs } = await token.takeOwnershipFor(to, tokenId, { from: sender }); + + logs.length.should.be.equal(2); + + logs[0].event.should.be.eq('Approval'); + logs[0].args._owner.should.be.equal(_creator); + logs[0].args._approved.should.be.equal(ZERO_ADDRESS); + logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + + logs[1].event.should.be.eq('Transfer'); + logs[1].args._from.should.be.equal(_creator); + logs[1].args._to.should.be.equal(to); + logs[1].args._tokenId.should.be.bignumber.equal(tokenId); + }); + + it('adjusts owners balances', async function () { + const previousBalance = await token.balanceOf(_creator); + + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const newOwnerBalance = await token.balanceOf(to); + newOwnerBalance.should.be.bignumber.equal(1); + + const previousOwnerBalance = await token.balanceOf(_creator); + previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1); + }); + + it('places the last token of the sender in the position of the transferred token', async function () { + const firstTokenIndex = 0; + const lastTokenIndex = await token.balanceOf(_creator) - 1; + const lastToken = await token.tokenOfOwnerByIndex(_creator, lastTokenIndex); + + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const swappedToken = await token.tokenOfOwnerByIndex(_creator, firstTokenIndex); + swappedToken.should.be.bignumber.equal(lastToken); + await assertRevert(token.tokenOfOwnerByIndex(_creator, lastTokenIndex)); + }); + + it('adds the token to the tokens list of the new owner', async function () { + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const tokenIDs = await token.tokensOf(to); + tokenIDs.length.should.be.equal(1); + tokenIDs[0].should.be.bignumber.equal(tokenId); + }); + }); + }); + + describe('when the sender was approved by the owner of the token ID', function () { + const sender = accounts[1]; + + beforeEach(async function () { + await token.setOperatorApproval(sender, true, { from: _creator }); + }); + + describe('when the recipient is the zero address', function () { + const to = ZERO_ADDRESS; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(to, tokenId, { from: sender })); + }); + }); + + describe('when the recipient is the owner', function () { + const to = _creator; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(to, tokenId, { from: sender })); + }); + }); + + describe('when the recipient is not the zero address neither the owner', function () { + const to = accounts[2]; + + it('transfers the ownership of the given token ID to the given address', async function () { + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const newOwner = await token.ownerOf(tokenId); + newOwner.should.be.equal(to); + }); + + it('clears the approval for the token ID', async function () { + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const approvedAccount = await token.approvedFor(tokenId); + approvedAccount.should.be.equal(ZERO_ADDRESS); + }); + + it('emits an approval and transfer events', async function () { + const { logs } = await token.takeOwnershipFor(to, tokenId, { from: sender }); + + logs.length.should.be.equal(2); + + logs[0].event.should.be.eq('Approval'); + logs[0].args._owner.should.be.equal(_creator); + logs[0].args._approved.should.be.equal(ZERO_ADDRESS); + logs[0].args._tokenId.should.be.bignumber.equal(tokenId); + + logs[1].event.should.be.eq('Transfer'); + logs[1].args._from.should.be.equal(_creator); + logs[1].args._to.should.be.equal(to); + logs[1].args._tokenId.should.be.bignumber.equal(tokenId); + }); + + it('adjusts owners balances', async function () { + const previousBalance = await token.balanceOf(_creator); + + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const newOwnerBalance = await token.balanceOf(to); + newOwnerBalance.should.be.bignumber.equal(1); + + const previousOwnerBalance = await token.balanceOf(_creator); + previousOwnerBalance.should.be.bignumber.equal(previousBalance - 1); + }); + + it('places the last token of the sender in the position of the transferred token', async function () { + const firstTokenIndex = 0; + const lastTokenIndex = await token.balanceOf(_creator) - 1; + const lastToken = await token.tokenOfOwnerByIndex(_creator, lastTokenIndex); + + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const swappedToken = await token.tokenOfOwnerByIndex(_creator, firstTokenIndex); + swappedToken.should.be.bignumber.equal(lastToken); + await assertRevert(token.tokenOfOwnerByIndex(_creator, lastTokenIndex)); + }); + + it('adds the token to the tokens list of the new owner', async function () { + await token.takeOwnershipFor(to, tokenId, { from: sender }); + + const tokenIDs = await token.tokensOf(to); + tokenIDs.length.should.be.equal(1); + tokenIDs[0].should.be.bignumber.equal(tokenId); + }); + }); + }); + + describe('when the sender does not have an approval for the token ID', function () { + const sender = accounts[1]; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(accounts[2], tokenId, { from: sender })); + }); + }); + + describe('when the sender is already the owner of the token', function () { + const sender = _creator; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(accounts[2], tokenId, { from: sender })); + }); + }); + }); + + describe('when the given token ID was not tracked by the contract before', function () { + const tokenId = _unknownTokenId; + + it('reverts', async function () { + await assertRevert(token.takeOwnershipFor(accounts[2], tokenId, { from: _creator })); + }); + }); + }); +});