ERC721.test.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { ZERO_ADDRESS } = constants;
  4. const { expect } = require('chai');
  5. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  6. const ERC721Mock = contract.fromArtifact('ERC721Mock');
  7. const ERC721ReceiverMock = contract.fromArtifact('ERC721ReceiverMock');
  8. describe('ERC721', function () {
  9. const [owner, newOwner, approved, anotherApproved, operator, other] = accounts;
  10. const name = 'Non Fungible Token';
  11. const symbol = 'NFT';
  12. const firstTokenId = new BN('5042');
  13. const secondTokenId = new BN('79217');
  14. const nonExistentTokenId = new BN('13');
  15. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  16. beforeEach(async function () {
  17. this.token = await ERC721Mock.new(name, symbol);
  18. });
  19. shouldSupportInterfaces([
  20. 'ERC165',
  21. 'ERC721',
  22. 'ERC721Enumerable',
  23. 'ERC721Metadata',
  24. ]);
  25. describe('metadata', function () {
  26. it('has a name', async function () {
  27. expect(await this.token.name()).to.be.equal(name);
  28. });
  29. it('has a symbol', async function () {
  30. expect(await this.token.symbol()).to.be.equal(symbol);
  31. });
  32. describe('token URI', function () {
  33. beforeEach(async function () {
  34. await this.token.mint(owner, firstTokenId);
  35. });
  36. const baseURI = 'https://api.com/v1/';
  37. const sampleUri = 'mock://mytoken';
  38. it('it is empty by default', async function () {
  39. expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
  40. });
  41. it('reverts when queried for non existent token id', async function () {
  42. await expectRevert(
  43. this.token.tokenURI(nonExistentTokenId), 'ERC721Metadata: URI query for nonexistent token'
  44. );
  45. });
  46. it('can be set for a token id', async function () {
  47. await this.token.setTokenURI(firstTokenId, sampleUri);
  48. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
  49. });
  50. it('reverts when setting for non existent token id', async function () {
  51. await expectRevert(
  52. this.token.setTokenURI(nonExistentTokenId, sampleUri), 'ERC721Metadata: URI set of nonexistent token'
  53. );
  54. });
  55. it('base URI can be set', async function () {
  56. await this.token.setBaseURI(baseURI);
  57. expect(await this.token.baseURI()).to.equal(baseURI);
  58. });
  59. it('base URI is added as a prefix to the token URI', async function () {
  60. await this.token.setBaseURI(baseURI);
  61. await this.token.setTokenURI(firstTokenId, sampleUri);
  62. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
  63. });
  64. it('token URI can be changed by changing the base URI', async function () {
  65. await this.token.setBaseURI(baseURI);
  66. await this.token.setTokenURI(firstTokenId, sampleUri);
  67. const newBaseURI = 'https://api.com/v2/';
  68. await this.token.setBaseURI(newBaseURI);
  69. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
  70. });
  71. it('token URI is empty for tokens with no URI but with base URI', async function () {
  72. await this.token.setBaseURI(baseURI);
  73. expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
  74. });
  75. it('tokens with URI can be burnt ', async function () {
  76. await this.token.setTokenURI(firstTokenId, sampleUri);
  77. await this.token.burn(firstTokenId, { from: owner });
  78. expect(await this.token.exists(firstTokenId)).to.equal(false);
  79. await expectRevert(
  80. this.token.tokenURI(firstTokenId), 'ERC721Metadata: URI query for nonexistent token'
  81. );
  82. });
  83. });
  84. });
  85. context('with minted tokens', function () {
  86. beforeEach(async function () {
  87. await this.token.mint(owner, firstTokenId);
  88. await this.token.mint(owner, secondTokenId);
  89. this.toWhom = other; // default to other for toWhom in context-dependent tests
  90. });
  91. describe('balanceOf', function () {
  92. context('when the given address owns some tokens', function () {
  93. it('returns the amount of tokens owned by the given address', async function () {
  94. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  95. });
  96. });
  97. context('when the given address does not own any tokens', function () {
  98. it('returns 0', async function () {
  99. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('0');
  100. });
  101. });
  102. context('when querying the zero address', function () {
  103. it('throws', async function () {
  104. await expectRevert(
  105. this.token.balanceOf(ZERO_ADDRESS), 'ERC721: balance query for the zero address'
  106. );
  107. });
  108. });
  109. });
  110. describe('ownerOf', function () {
  111. context('when the given token ID was tracked by this token', function () {
  112. const tokenId = firstTokenId;
  113. it('returns the owner of the given token ID', async function () {
  114. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  115. });
  116. });
  117. context('when the given token ID was not tracked by this token', function () {
  118. const tokenId = nonExistentTokenId;
  119. it('reverts', async function () {
  120. await expectRevert(
  121. this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token'
  122. );
  123. });
  124. });
  125. });
  126. describe('transfers', function () {
  127. const tokenId = firstTokenId;
  128. const data = '0x42';
  129. let logs = null;
  130. beforeEach(async function () {
  131. await this.token.approve(approved, tokenId, { from: owner });
  132. await this.token.setApprovalForAll(operator, true, { from: owner });
  133. });
  134. const transferWasSuccessful = function ({ owner, tokenId, approved }) {
  135. it('transfers the ownership of the given token ID to the given address', async function () {
  136. expect(await this.token.ownerOf(tokenId)).to.be.equal(this.toWhom);
  137. });
  138. it('clears the approval for the token ID', async function () {
  139. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  140. });
  141. if (approved) {
  142. it('emit only a transfer event', async function () {
  143. expectEvent.inLogs(logs, 'Transfer', {
  144. from: owner,
  145. to: this.toWhom,
  146. tokenId: tokenId,
  147. });
  148. });
  149. } else {
  150. it('emits only a transfer event', async function () {
  151. expectEvent.inLogs(logs, 'Transfer', {
  152. from: owner,
  153. to: this.toWhom,
  154. tokenId: tokenId,
  155. });
  156. });
  157. }
  158. it('adjusts owners balances', async function () {
  159. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  160. });
  161. it('adjusts owners tokens by index', async function () {
  162. if (!this.token.tokenOfOwnerByIndex) return;
  163. expect(await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).to.be.bignumber.equal(tokenId);
  164. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.not.equal(tokenId);
  165. });
  166. };
  167. const shouldTransferTokensByUsers = function (transferFunction) {
  168. context('when called by the owner', function () {
  169. beforeEach(async function () {
  170. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner }));
  171. });
  172. transferWasSuccessful({ owner, tokenId, approved });
  173. });
  174. context('when called by the approved individual', function () {
  175. beforeEach(async function () {
  176. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved }));
  177. });
  178. transferWasSuccessful({ owner, tokenId, approved });
  179. });
  180. context('when called by the operator', function () {
  181. beforeEach(async function () {
  182. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  183. });
  184. transferWasSuccessful({ owner, tokenId, approved });
  185. });
  186. context('when called by the owner without an approved user', function () {
  187. beforeEach(async function () {
  188. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  189. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  190. });
  191. transferWasSuccessful({ owner, tokenId, approved: null });
  192. });
  193. context('when sent to the owner', function () {
  194. beforeEach(async function () {
  195. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  196. });
  197. it('keeps ownership of the token', async function () {
  198. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  199. });
  200. it('clears the approval for the token ID', async function () {
  201. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  202. });
  203. it('emits only a transfer event', async function () {
  204. expectEvent.inLogs(logs, 'Transfer', {
  205. from: owner,
  206. to: owner,
  207. tokenId: tokenId,
  208. });
  209. });
  210. it('keeps the owner balance', async function () {
  211. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  212. });
  213. it('keeps same tokens by index', async function () {
  214. if (!this.token.tokenOfOwnerByIndex) return;
  215. const tokensListed = await Promise.all(
  216. [0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i))
  217. );
  218. expect(tokensListed.map(t => t.toNumber())).to.have.members(
  219. [firstTokenId.toNumber(), secondTokenId.toNumber()]
  220. );
  221. });
  222. });
  223. context('when the address of the previous owner is incorrect', function () {
  224. it('reverts', async function () {
  225. await expectRevert(
  226. transferFunction.call(this, other, other, tokenId, { from: owner }),
  227. 'ERC721: transfer of token that is not own'
  228. );
  229. });
  230. });
  231. context('when the sender is not authorized for the token id', function () {
  232. it('reverts', async function () {
  233. await expectRevert(
  234. transferFunction.call(this, owner, other, tokenId, { from: other }),
  235. 'ERC721: transfer caller is not owner nor approved'
  236. );
  237. });
  238. });
  239. context('when the given token ID does not exist', function () {
  240. it('reverts', async function () {
  241. await expectRevert(
  242. transferFunction.call(this, owner, other, nonExistentTokenId, { from: owner }),
  243. 'ERC721: operator query for nonexistent token'
  244. );
  245. });
  246. });
  247. context('when the address to transfer the token to is the zero address', function () {
  248. it('reverts', async function () {
  249. await expectRevert(
  250. transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
  251. 'ERC721: transfer to the zero address'
  252. );
  253. });
  254. });
  255. };
  256. describe('via transferFrom', function () {
  257. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  258. return this.token.transferFrom(from, to, tokenId, opts);
  259. });
  260. });
  261. describe('via safeTransferFrom', function () {
  262. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  263. return this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
  264. };
  265. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  266. return this.token.methods['safeTransferFrom(address,address,uint256)'](from, to, tokenId, opts);
  267. };
  268. const shouldTransferSafely = function (transferFun, data) {
  269. describe('to a user account', function () {
  270. shouldTransferTokensByUsers(transferFun);
  271. });
  272. describe('to a valid receiver contract', function () {
  273. beforeEach(async function () {
  274. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  275. this.toWhom = this.receiver.address;
  276. });
  277. shouldTransferTokensByUsers(transferFun);
  278. it('should call onERC721Received', async function () {
  279. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  280. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  281. operator: owner,
  282. from: owner,
  283. tokenId: tokenId,
  284. data: data,
  285. });
  286. });
  287. it('should call onERC721Received from approved', async function () {
  288. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
  289. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  290. operator: approved,
  291. from: owner,
  292. tokenId: tokenId,
  293. data: data,
  294. });
  295. });
  296. describe('with an invalid token id', function () {
  297. it('reverts', async function () {
  298. await expectRevert(
  299. transferFun.call(
  300. this,
  301. owner,
  302. this.receiver.address,
  303. nonExistentTokenId,
  304. { from: owner },
  305. ),
  306. 'ERC721: operator query for nonexistent token'
  307. );
  308. });
  309. });
  310. });
  311. };
  312. describe('with data', function () {
  313. shouldTransferSafely(safeTransferFromWithData, data);
  314. });
  315. describe('without data', function () {
  316. shouldTransferSafely(safeTransferFromWithoutData, null);
  317. });
  318. describe('to a receiver contract returning unexpected value', function () {
  319. it('reverts', async function () {
  320. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  321. await expectRevert(
  322. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
  323. 'ERC721: transfer to non ERC721Receiver implementer'
  324. );
  325. });
  326. });
  327. describe('to a receiver contract that throws', function () {
  328. it('reverts', async function () {
  329. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  330. await expectRevert(
  331. this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
  332. 'ERC721ReceiverMock: reverting'
  333. );
  334. });
  335. });
  336. describe('to a contract that does not implement the required function', function () {
  337. it('reverts', async function () {
  338. const nonReceiver = this.token;
  339. await expectRevert(
  340. this.token.safeTransferFrom(owner, nonReceiver.address, tokenId, { from: owner }),
  341. 'ERC721: transfer to non ERC721Receiver implementer'
  342. );
  343. });
  344. });
  345. });
  346. });
  347. describe('safe mint', function () {
  348. const fourthTokenId = new BN(4);
  349. const tokenId = fourthTokenId;
  350. const data = '0x42';
  351. describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
  352. it('should call onERC721Received — with data', async function () {
  353. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  354. const receipt = await this.token.safeMint(this.receiver.address, tokenId, data);
  355. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  356. from: ZERO_ADDRESS,
  357. tokenId: tokenId,
  358. data: data,
  359. });
  360. });
  361. it('should call onERC721Received — without data', async function () {
  362. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  363. const receipt = await this.token.safeMint(this.receiver.address, tokenId);
  364. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  365. from: ZERO_ADDRESS,
  366. tokenId: tokenId,
  367. });
  368. });
  369. context('to a receiver contract returning unexpected value', function () {
  370. it('reverts', async function () {
  371. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  372. await expectRevert(
  373. this.token.safeMint(invalidReceiver.address, tokenId),
  374. 'ERC721: transfer to non ERC721Receiver implementer'
  375. );
  376. });
  377. });
  378. context('to a receiver contract that throws', function () {
  379. it('reverts', async function () {
  380. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  381. await expectRevert(
  382. this.token.safeMint(revertingReceiver.address, tokenId),
  383. 'ERC721ReceiverMock: reverting'
  384. );
  385. });
  386. });
  387. context('to a contract that does not implement the required function', function () {
  388. it('reverts', async function () {
  389. const nonReceiver = this.token;
  390. await expectRevert(
  391. this.token.safeMint(nonReceiver.address, tokenId),
  392. 'ERC721: transfer to non ERC721Receiver implementer'
  393. );
  394. });
  395. });
  396. });
  397. });
  398. describe('approve', function () {
  399. const tokenId = firstTokenId;
  400. let logs = null;
  401. const itClearsApproval = function () {
  402. it('clears approval for the token', async function () {
  403. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  404. });
  405. };
  406. const itApproves = function (address) {
  407. it('sets the approval for the target address', async function () {
  408. expect(await this.token.getApproved(tokenId)).to.be.equal(address);
  409. });
  410. };
  411. const itEmitsApprovalEvent = function (address) {
  412. it('emits an approval event', async function () {
  413. expectEvent.inLogs(logs, 'Approval', {
  414. owner: owner,
  415. approved: address,
  416. tokenId: tokenId,
  417. });
  418. });
  419. };
  420. context('when clearing approval', function () {
  421. context('when there was no prior approval', function () {
  422. beforeEach(async function () {
  423. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  424. });
  425. itClearsApproval();
  426. itEmitsApprovalEvent(ZERO_ADDRESS);
  427. });
  428. context('when there was a prior approval', function () {
  429. beforeEach(async function () {
  430. await this.token.approve(approved, tokenId, { from: owner });
  431. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  432. });
  433. itClearsApproval();
  434. itEmitsApprovalEvent(ZERO_ADDRESS);
  435. });
  436. });
  437. context('when approving a non-zero address', function () {
  438. context('when there was no prior approval', function () {
  439. beforeEach(async function () {
  440. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  441. });
  442. itApproves(approved);
  443. itEmitsApprovalEvent(approved);
  444. });
  445. context('when there was a prior approval to the same address', function () {
  446. beforeEach(async function () {
  447. await this.token.approve(approved, tokenId, { from: owner });
  448. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  449. });
  450. itApproves(approved);
  451. itEmitsApprovalEvent(approved);
  452. });
  453. context('when there was a prior approval to a different address', function () {
  454. beforeEach(async function () {
  455. await this.token.approve(anotherApproved, tokenId, { from: owner });
  456. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  457. });
  458. itApproves(anotherApproved);
  459. itEmitsApprovalEvent(anotherApproved);
  460. });
  461. });
  462. context('when the address that receives the approval is the owner', function () {
  463. it('reverts', async function () {
  464. await expectRevert(
  465. this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner'
  466. );
  467. });
  468. });
  469. context('when the sender does not own the given token ID', function () {
  470. it('reverts', async function () {
  471. await expectRevert(this.token.approve(approved, tokenId, { from: other }),
  472. 'ERC721: approve caller is not owner nor approved');
  473. });
  474. });
  475. context('when the sender is approved for the given token ID', function () {
  476. it('reverts', async function () {
  477. await this.token.approve(approved, tokenId, { from: owner });
  478. await expectRevert(this.token.approve(anotherApproved, tokenId, { from: approved }),
  479. 'ERC721: approve caller is not owner nor approved for all');
  480. });
  481. });
  482. context('when the sender is an operator', function () {
  483. beforeEach(async function () {
  484. await this.token.setApprovalForAll(operator, true, { from: owner });
  485. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  486. });
  487. itApproves(approved);
  488. itEmitsApprovalEvent(approved);
  489. });
  490. context('when the given token ID does not exist', function () {
  491. it('reverts', async function () {
  492. await expectRevert(this.token.approve(approved, nonExistentTokenId, { from: operator }),
  493. 'ERC721: owner query for nonexistent token');
  494. });
  495. });
  496. });
  497. describe('setApprovalForAll', function () {
  498. context('when the operator willing to approve is not the owner', function () {
  499. context('when there is no operator approval set by the sender', function () {
  500. it('approves the operator', async function () {
  501. await this.token.setApprovalForAll(operator, true, { from: owner });
  502. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  503. });
  504. it('emits an approval event', async function () {
  505. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  506. expectEvent.inLogs(logs, 'ApprovalForAll', {
  507. owner: owner,
  508. operator: operator,
  509. approved: true,
  510. });
  511. });
  512. });
  513. context('when the operator was set as not approved', function () {
  514. beforeEach(async function () {
  515. await this.token.setApprovalForAll(operator, false, { from: owner });
  516. });
  517. it('approves the operator', async function () {
  518. await this.token.setApprovalForAll(operator, true, { from: owner });
  519. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  520. });
  521. it('emits an approval event', async function () {
  522. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  523. expectEvent.inLogs(logs, 'ApprovalForAll', {
  524. owner: owner,
  525. operator: operator,
  526. approved: true,
  527. });
  528. });
  529. it('can unset the operator approval', async function () {
  530. await this.token.setApprovalForAll(operator, false, { from: owner });
  531. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
  532. });
  533. });
  534. context('when the operator was already approved', function () {
  535. beforeEach(async function () {
  536. await this.token.setApprovalForAll(operator, true, { from: owner });
  537. });
  538. it('keeps the approval to the given address', async function () {
  539. await this.token.setApprovalForAll(operator, true, { from: owner });
  540. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  541. });
  542. it('emits an approval event', async function () {
  543. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  544. expectEvent.inLogs(logs, 'ApprovalForAll', {
  545. owner: owner,
  546. operator: operator,
  547. approved: true,
  548. });
  549. });
  550. });
  551. });
  552. context('when the operator is the owner', function () {
  553. it('reverts', async function () {
  554. await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }),
  555. 'ERC721: approve to caller');
  556. });
  557. });
  558. });
  559. describe('getApproved', async function () {
  560. context('when token is not minted', async function () {
  561. it('reverts', async function () {
  562. await expectRevert(
  563. this.token.getApproved(nonExistentTokenId),
  564. 'ERC721: approved query for nonexistent token'
  565. );
  566. });
  567. });
  568. context('when token has been minted ', async function () {
  569. it('should return the zero address', async function () {
  570. expect(await this.token.getApproved(firstTokenId)).to.be.equal(
  571. ZERO_ADDRESS
  572. );
  573. });
  574. context('when account has been approved', async function () {
  575. beforeEach(async function () {
  576. await this.token.approve(approved, firstTokenId, { from: owner });
  577. });
  578. it('should return approved account', async function () {
  579. expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
  580. });
  581. });
  582. });
  583. });
  584. describe('tokensOfOwner', function () {
  585. it('returns total tokens of owner', async function () {
  586. const tokenIds = await this.token.tokensOfOwner(owner);
  587. expect(tokenIds.length).to.equal(2);
  588. expect(tokenIds[0]).to.be.bignumber.equal(firstTokenId);
  589. expect(tokenIds[1]).to.be.bignumber.equal(secondTokenId);
  590. });
  591. });
  592. describe('totalSupply', function () {
  593. it('returns total token supply', async function () {
  594. expect(await this.token.totalSupply()).to.be.bignumber.equal('2');
  595. });
  596. });
  597. describe('tokenOfOwnerByIndex', function () {
  598. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  599. it('returns the token ID placed at the given index', async function () {
  600. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  601. });
  602. });
  603. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  604. it('reverts', async function () {
  605. await expectRevert(
  606. this.token.tokenOfOwnerByIndex(owner, 2), 'ERC721Enumerable: owner index out of bounds'
  607. );
  608. });
  609. });
  610. describe('when the given address does not own any token', function () {
  611. it('reverts', async function () {
  612. await expectRevert(
  613. this.token.tokenOfOwnerByIndex(other, 0), 'ERC721Enumerable: owner index out of bounds'
  614. );
  615. });
  616. });
  617. describe('after transferring all tokens to another user', function () {
  618. beforeEach(async function () {
  619. await this.token.transferFrom(owner, other, firstTokenId, { from: owner });
  620. await this.token.transferFrom(owner, other, secondTokenId, { from: owner });
  621. });
  622. it('returns correct token IDs for target', async function () {
  623. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('2');
  624. const tokensListed = await Promise.all(
  625. [0, 1].map(i => this.token.tokenOfOwnerByIndex(other, i))
  626. );
  627. expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
  628. secondTokenId.toNumber()]);
  629. });
  630. it('returns empty collection for original owner', async function () {
  631. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
  632. await expectRevert(
  633. this.token.tokenOfOwnerByIndex(owner, 0), 'ERC721Enumerable: owner index out of bounds'
  634. );
  635. });
  636. });
  637. });
  638. describe('tokenByIndex', function () {
  639. it('should return all tokens', async function () {
  640. const tokensListed = await Promise.all(
  641. [0, 1].map(i => this.token.tokenByIndex(i))
  642. );
  643. expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
  644. secondTokenId.toNumber()]);
  645. });
  646. it('should revert if index is greater than supply', async function () {
  647. await expectRevert(
  648. this.token.tokenByIndex(2), 'ERC721Enumerable: global index out of bounds'
  649. );
  650. });
  651. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  652. it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  653. const newTokenId = new BN(300);
  654. const anotherNewTokenId = new BN(400);
  655. await this.token.burn(tokenId, { from: owner });
  656. await this.token.mint(newOwner, newTokenId);
  657. await this.token.mint(newOwner, anotherNewTokenId);
  658. expect(await this.token.totalSupply()).to.be.bignumber.equal('3');
  659. const tokensListed = await Promise.all(
  660. [0, 1, 2].map(i => this.token.tokenByIndex(i))
  661. );
  662. const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
  663. x => (x !== tokenId)
  664. );
  665. expect(tokensListed.map(t => t.toNumber())).to.have.members(expectedTokens.map(t => t.toNumber()));
  666. });
  667. });
  668. });
  669. });
  670. describe('_mint(address, uint256)', function () {
  671. it('reverts with a null destination address', async function () {
  672. await expectRevert(
  673. this.token.mint(ZERO_ADDRESS, firstTokenId), 'ERC721: mint to the zero address'
  674. );
  675. });
  676. context('with minted token', async function () {
  677. beforeEach(async function () {
  678. ({ logs: this.logs } = await this.token.mint(owner, firstTokenId));
  679. });
  680. it('emits a Transfer event', function () {
  681. expectEvent.inLogs(this.logs, 'Transfer', { from: ZERO_ADDRESS, to: owner, tokenId: firstTokenId });
  682. });
  683. it('creates the token', async function () {
  684. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  685. expect(await this.token.ownerOf(firstTokenId)).to.equal(owner);
  686. });
  687. it('adjusts owner tokens by index', async function () {
  688. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  689. });
  690. it('adjusts all tokens list', async function () {
  691. expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(firstTokenId);
  692. });
  693. it('reverts when adding a token id that already exists', async function () {
  694. await expectRevert(this.token.mint(owner, firstTokenId), 'ERC721: token already minted');
  695. });
  696. });
  697. });
  698. describe('_burn', function () {
  699. it('reverts when burning a non-existent token id', async function () {
  700. await expectRevert(
  701. this.token.burn(firstTokenId), 'ERC721: owner query for nonexistent token'
  702. );
  703. });
  704. context('with minted tokens', function () {
  705. beforeEach(async function () {
  706. await this.token.mint(owner, firstTokenId);
  707. await this.token.mint(owner, secondTokenId);
  708. });
  709. context('with burnt token', function () {
  710. beforeEach(async function () {
  711. ({ logs: this.logs } = await this.token.burn(firstTokenId));
  712. });
  713. it('emits a Transfer event', function () {
  714. expectEvent.inLogs(this.logs, 'Transfer', { from: owner, to: ZERO_ADDRESS, tokenId: firstTokenId });
  715. });
  716. it('deletes the token', async function () {
  717. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  718. await expectRevert(
  719. this.token.ownerOf(firstTokenId), 'ERC721: owner query for nonexistent token'
  720. );
  721. });
  722. it('removes that token from the token list of the owner', async function () {
  723. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(secondTokenId);
  724. });
  725. it('adjusts all tokens list', async function () {
  726. expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(secondTokenId);
  727. });
  728. it('burns all tokens', async function () {
  729. await this.token.burn(secondTokenId, { from: owner });
  730. expect(await this.token.totalSupply()).to.be.bignumber.equal('0');
  731. await expectRevert(
  732. this.token.tokenByIndex(0), 'ERC721Enumerable: global index out of bounds'
  733. );
  734. });
  735. it('reverts when burning a token id that has been deleted', async function () {
  736. await expectRevert(
  737. this.token.burn(firstTokenId), 'ERC721: owner query for nonexistent token'
  738. );
  739. });
  740. });
  741. });
  742. });
  743. });