ERC721.test.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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('emits a Transfer event', async function () {
  139. expectEvent.inLogs(logs, 'Transfer', { from: owner, to: this.toWhom, tokenId: tokenId });
  140. });
  141. it('clears the approval for the token ID', async function () {
  142. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  143. });
  144. it('emits an Approval event', async function () {
  145. expectEvent.inLogs(logs, 'Approval', { owner, approved: ZERO_ADDRESS, tokenId: tokenId });
  146. });
  147. it('adjusts owners balances', async function () {
  148. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  149. });
  150. it('adjusts owners tokens by index', async function () {
  151. if (!this.token.tokenOfOwnerByIndex) return;
  152. expect(await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).to.be.bignumber.equal(tokenId);
  153. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.not.equal(tokenId);
  154. });
  155. };
  156. const shouldTransferTokensByUsers = function (transferFunction) {
  157. context('when called by the owner', function () {
  158. beforeEach(async function () {
  159. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner }));
  160. });
  161. transferWasSuccessful({ owner, tokenId, approved });
  162. });
  163. context('when called by the approved individual', function () {
  164. beforeEach(async function () {
  165. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved }));
  166. });
  167. transferWasSuccessful({ owner, tokenId, approved });
  168. });
  169. context('when called by the operator', function () {
  170. beforeEach(async function () {
  171. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  172. });
  173. transferWasSuccessful({ owner, tokenId, approved });
  174. });
  175. context('when called by the owner without an approved user', function () {
  176. beforeEach(async function () {
  177. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  178. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  179. });
  180. transferWasSuccessful({ owner, tokenId, approved: null });
  181. });
  182. context('when sent to the owner', function () {
  183. beforeEach(async function () {
  184. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  185. });
  186. it('keeps ownership of the token', async function () {
  187. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  188. });
  189. it('clears the approval for the token ID', async function () {
  190. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  191. });
  192. it('emits only a transfer event', async function () {
  193. expectEvent.inLogs(logs, 'Transfer', {
  194. from: owner,
  195. to: owner,
  196. tokenId: tokenId,
  197. });
  198. });
  199. it('keeps the owner balance', async function () {
  200. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  201. });
  202. it('keeps same tokens by index', async function () {
  203. if (!this.token.tokenOfOwnerByIndex) return;
  204. const tokensListed = await Promise.all(
  205. [0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i))
  206. );
  207. expect(tokensListed.map(t => t.toNumber())).to.have.members(
  208. [firstTokenId.toNumber(), secondTokenId.toNumber()]
  209. );
  210. });
  211. });
  212. context('when the address of the previous owner is incorrect', function () {
  213. it('reverts', async function () {
  214. await expectRevert(
  215. transferFunction.call(this, other, other, tokenId, { from: owner }),
  216. 'ERC721: transfer of token that is not own'
  217. );
  218. });
  219. });
  220. context('when the sender is not authorized for the token id', function () {
  221. it('reverts', async function () {
  222. await expectRevert(
  223. transferFunction.call(this, owner, other, tokenId, { from: other }),
  224. 'ERC721: transfer caller is not owner nor approved'
  225. );
  226. });
  227. });
  228. context('when the given token ID does not exist', function () {
  229. it('reverts', async function () {
  230. await expectRevert(
  231. transferFunction.call(this, owner, other, nonExistentTokenId, { from: owner }),
  232. 'ERC721: operator query for nonexistent token'
  233. );
  234. });
  235. });
  236. context('when the address to transfer the token to is the zero address', function () {
  237. it('reverts', async function () {
  238. await expectRevert(
  239. transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
  240. 'ERC721: transfer to the zero address'
  241. );
  242. });
  243. });
  244. };
  245. describe('via transferFrom', function () {
  246. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  247. return this.token.transferFrom(from, to, tokenId, opts);
  248. });
  249. });
  250. describe('via safeTransferFrom', function () {
  251. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  252. return this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
  253. };
  254. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  255. return this.token.methods['safeTransferFrom(address,address,uint256)'](from, to, tokenId, opts);
  256. };
  257. const shouldTransferSafely = function (transferFun, data) {
  258. describe('to a user account', function () {
  259. shouldTransferTokensByUsers(transferFun);
  260. });
  261. describe('to a valid receiver contract', function () {
  262. beforeEach(async function () {
  263. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  264. this.toWhom = this.receiver.address;
  265. });
  266. shouldTransferTokensByUsers(transferFun);
  267. it('should call onERC721Received', async function () {
  268. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  269. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  270. operator: owner,
  271. from: owner,
  272. tokenId: tokenId,
  273. data: data,
  274. });
  275. });
  276. it('should call onERC721Received from approved', async function () {
  277. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
  278. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  279. operator: approved,
  280. from: owner,
  281. tokenId: tokenId,
  282. data: data,
  283. });
  284. });
  285. describe('with an invalid token id', function () {
  286. it('reverts', async function () {
  287. await expectRevert(
  288. transferFun.call(
  289. this,
  290. owner,
  291. this.receiver.address,
  292. nonExistentTokenId,
  293. { from: owner },
  294. ),
  295. 'ERC721: operator query for nonexistent token'
  296. );
  297. });
  298. });
  299. });
  300. };
  301. describe('with data', function () {
  302. shouldTransferSafely(safeTransferFromWithData, data);
  303. });
  304. describe('without data', function () {
  305. shouldTransferSafely(safeTransferFromWithoutData, null);
  306. });
  307. describe('to a receiver contract returning unexpected value', function () {
  308. it('reverts', async function () {
  309. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  310. await expectRevert(
  311. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
  312. 'ERC721: transfer to non ERC721Receiver implementer'
  313. );
  314. });
  315. });
  316. describe('to a receiver contract that throws', function () {
  317. it('reverts', async function () {
  318. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  319. await expectRevert(
  320. this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
  321. 'ERC721ReceiverMock: reverting'
  322. );
  323. });
  324. });
  325. describe('to a contract that does not implement the required function', function () {
  326. it('reverts', async function () {
  327. const nonReceiver = this.token;
  328. await expectRevert(
  329. this.token.safeTransferFrom(owner, nonReceiver.address, tokenId, { from: owner }),
  330. 'ERC721: transfer to non ERC721Receiver implementer'
  331. );
  332. });
  333. });
  334. });
  335. });
  336. describe('safe mint', function () {
  337. const fourthTokenId = new BN(4);
  338. const tokenId = fourthTokenId;
  339. const data = '0x42';
  340. describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
  341. it('should call onERC721Received — with data', async function () {
  342. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  343. const receipt = await this.token.safeMint(this.receiver.address, tokenId, data);
  344. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  345. from: ZERO_ADDRESS,
  346. tokenId: tokenId,
  347. data: data,
  348. });
  349. });
  350. it('should call onERC721Received — without data', async function () {
  351. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  352. const receipt = await this.token.safeMint(this.receiver.address, tokenId);
  353. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  354. from: ZERO_ADDRESS,
  355. tokenId: tokenId,
  356. });
  357. });
  358. context('to a receiver contract returning unexpected value', function () {
  359. it('reverts', async function () {
  360. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  361. await expectRevert(
  362. this.token.safeMint(invalidReceiver.address, tokenId),
  363. 'ERC721: transfer to non ERC721Receiver implementer'
  364. );
  365. });
  366. });
  367. context('to a receiver contract that throws', function () {
  368. it('reverts', async function () {
  369. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  370. await expectRevert(
  371. this.token.safeMint(revertingReceiver.address, tokenId),
  372. 'ERC721ReceiverMock: reverting'
  373. );
  374. });
  375. });
  376. context('to a contract that does not implement the required function', function () {
  377. it('reverts', async function () {
  378. const nonReceiver = this.token;
  379. await expectRevert(
  380. this.token.safeMint(nonReceiver.address, tokenId),
  381. 'ERC721: transfer to non ERC721Receiver implementer'
  382. );
  383. });
  384. });
  385. });
  386. });
  387. describe('approve', function () {
  388. const tokenId = firstTokenId;
  389. let logs = null;
  390. const itClearsApproval = function () {
  391. it('clears approval for the token', async function () {
  392. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  393. });
  394. };
  395. const itApproves = function (address) {
  396. it('sets the approval for the target address', async function () {
  397. expect(await this.token.getApproved(tokenId)).to.be.equal(address);
  398. });
  399. };
  400. const itEmitsApprovalEvent = function (address) {
  401. it('emits an approval event', async function () {
  402. expectEvent.inLogs(logs, 'Approval', {
  403. owner: owner,
  404. approved: address,
  405. tokenId: tokenId,
  406. });
  407. });
  408. };
  409. context('when clearing approval', function () {
  410. context('when there was no prior approval', function () {
  411. beforeEach(async function () {
  412. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  413. });
  414. itClearsApproval();
  415. itEmitsApprovalEvent(ZERO_ADDRESS);
  416. });
  417. context('when there was a prior approval', function () {
  418. beforeEach(async function () {
  419. await this.token.approve(approved, tokenId, { from: owner });
  420. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  421. });
  422. itClearsApproval();
  423. itEmitsApprovalEvent(ZERO_ADDRESS);
  424. });
  425. });
  426. context('when approving a non-zero address', function () {
  427. context('when there was no prior approval', function () {
  428. beforeEach(async function () {
  429. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  430. });
  431. itApproves(approved);
  432. itEmitsApprovalEvent(approved);
  433. });
  434. context('when there was a prior approval to the same address', function () {
  435. beforeEach(async function () {
  436. await this.token.approve(approved, tokenId, { from: owner });
  437. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  438. });
  439. itApproves(approved);
  440. itEmitsApprovalEvent(approved);
  441. });
  442. context('when there was a prior approval to a different address', function () {
  443. beforeEach(async function () {
  444. await this.token.approve(anotherApproved, tokenId, { from: owner });
  445. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  446. });
  447. itApproves(anotherApproved);
  448. itEmitsApprovalEvent(anotherApproved);
  449. });
  450. });
  451. context('when the address that receives the approval is the owner', function () {
  452. it('reverts', async function () {
  453. await expectRevert(
  454. this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner'
  455. );
  456. });
  457. });
  458. context('when the sender does not own the given token ID', function () {
  459. it('reverts', async function () {
  460. await expectRevert(this.token.approve(approved, tokenId, { from: other }),
  461. 'ERC721: approve caller is not owner nor approved');
  462. });
  463. });
  464. context('when the sender is approved for the given token ID', function () {
  465. it('reverts', async function () {
  466. await this.token.approve(approved, tokenId, { from: owner });
  467. await expectRevert(this.token.approve(anotherApproved, tokenId, { from: approved }),
  468. 'ERC721: approve caller is not owner nor approved for all');
  469. });
  470. });
  471. context('when the sender is an operator', function () {
  472. beforeEach(async function () {
  473. await this.token.setApprovalForAll(operator, true, { from: owner });
  474. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  475. });
  476. itApproves(approved);
  477. itEmitsApprovalEvent(approved);
  478. });
  479. context('when the given token ID does not exist', function () {
  480. it('reverts', async function () {
  481. await expectRevert(this.token.approve(approved, nonExistentTokenId, { from: operator }),
  482. 'ERC721: owner query for nonexistent token');
  483. });
  484. });
  485. });
  486. describe('setApprovalForAll', function () {
  487. context('when the operator willing to approve is not the owner', function () {
  488. context('when there is no operator approval set by the sender', function () {
  489. it('approves the operator', async function () {
  490. await this.token.setApprovalForAll(operator, true, { from: owner });
  491. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  492. });
  493. it('emits an approval event', async function () {
  494. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  495. expectEvent.inLogs(logs, 'ApprovalForAll', {
  496. owner: owner,
  497. operator: operator,
  498. approved: true,
  499. });
  500. });
  501. });
  502. context('when the operator was set as not approved', function () {
  503. beforeEach(async function () {
  504. await this.token.setApprovalForAll(operator, false, { from: owner });
  505. });
  506. it('approves the operator', async function () {
  507. await this.token.setApprovalForAll(operator, true, { from: owner });
  508. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  509. });
  510. it('emits an approval event', async function () {
  511. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  512. expectEvent.inLogs(logs, 'ApprovalForAll', {
  513. owner: owner,
  514. operator: operator,
  515. approved: true,
  516. });
  517. });
  518. it('can unset the operator approval', async function () {
  519. await this.token.setApprovalForAll(operator, false, { from: owner });
  520. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
  521. });
  522. });
  523. context('when the operator was already approved', function () {
  524. beforeEach(async function () {
  525. await this.token.setApprovalForAll(operator, true, { from: owner });
  526. });
  527. it('keeps the approval to the given address', async function () {
  528. await this.token.setApprovalForAll(operator, true, { from: owner });
  529. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  530. });
  531. it('emits an approval event', async function () {
  532. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  533. expectEvent.inLogs(logs, 'ApprovalForAll', {
  534. owner: owner,
  535. operator: operator,
  536. approved: true,
  537. });
  538. });
  539. });
  540. });
  541. context('when the operator is the owner', function () {
  542. it('reverts', async function () {
  543. await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }),
  544. 'ERC721: approve to caller');
  545. });
  546. });
  547. });
  548. describe('getApproved', async function () {
  549. context('when token is not minted', async function () {
  550. it('reverts', async function () {
  551. await expectRevert(
  552. this.token.getApproved(nonExistentTokenId),
  553. 'ERC721: approved query for nonexistent token'
  554. );
  555. });
  556. });
  557. context('when token has been minted ', async function () {
  558. it('should return the zero address', async function () {
  559. expect(await this.token.getApproved(firstTokenId)).to.be.equal(
  560. ZERO_ADDRESS
  561. );
  562. });
  563. context('when account has been approved', async function () {
  564. beforeEach(async function () {
  565. await this.token.approve(approved, firstTokenId, { from: owner });
  566. });
  567. it('should return approved account', async function () {
  568. expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
  569. });
  570. });
  571. });
  572. });
  573. describe('totalSupply', function () {
  574. it('returns total token supply', async function () {
  575. expect(await this.token.totalSupply()).to.be.bignumber.equal('2');
  576. });
  577. });
  578. describe('tokenOfOwnerByIndex', function () {
  579. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  580. it('returns the token ID placed at the given index', async function () {
  581. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  582. });
  583. });
  584. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  585. it('reverts', async function () {
  586. await expectRevert(
  587. this.token.tokenOfOwnerByIndex(owner, 2), 'EnumerableSet: index out of bounds'
  588. );
  589. });
  590. });
  591. describe('when the given address does not own any token', function () {
  592. it('reverts', async function () {
  593. await expectRevert(
  594. this.token.tokenOfOwnerByIndex(other, 0), 'EnumerableSet: index out of bounds'
  595. );
  596. });
  597. });
  598. describe('after transferring all tokens to another user', function () {
  599. beforeEach(async function () {
  600. await this.token.transferFrom(owner, other, firstTokenId, { from: owner });
  601. await this.token.transferFrom(owner, other, secondTokenId, { from: owner });
  602. });
  603. it('returns correct token IDs for target', async function () {
  604. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('2');
  605. const tokensListed = await Promise.all(
  606. [0, 1].map(i => this.token.tokenOfOwnerByIndex(other, i))
  607. );
  608. expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
  609. secondTokenId.toNumber()]);
  610. });
  611. it('returns empty collection for original owner', async function () {
  612. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
  613. await expectRevert(
  614. this.token.tokenOfOwnerByIndex(owner, 0), 'EnumerableSet: index out of bounds'
  615. );
  616. });
  617. });
  618. });
  619. describe('tokenByIndex', function () {
  620. it('should return all tokens', async function () {
  621. const tokensListed = await Promise.all(
  622. [0, 1].map(i => this.token.tokenByIndex(i))
  623. );
  624. expect(tokensListed.map(t => t.toNumber())).to.have.members([firstTokenId.toNumber(),
  625. secondTokenId.toNumber()]);
  626. });
  627. it('should revert if index is greater than supply', async function () {
  628. await expectRevert(
  629. this.token.tokenByIndex(2), 'EnumerableMap: index out of bounds'
  630. );
  631. });
  632. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  633. it(`should return all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  634. const newTokenId = new BN(300);
  635. const anotherNewTokenId = new BN(400);
  636. await this.token.burn(tokenId);
  637. await this.token.mint(newOwner, newTokenId);
  638. await this.token.mint(newOwner, anotherNewTokenId);
  639. expect(await this.token.totalSupply()).to.be.bignumber.equal('3');
  640. const tokensListed = await Promise.all(
  641. [0, 1, 2].map(i => this.token.tokenByIndex(i))
  642. );
  643. const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
  644. x => (x !== tokenId)
  645. );
  646. expect(tokensListed.map(t => t.toNumber())).to.have.members(expectedTokens.map(t => t.toNumber()));
  647. });
  648. });
  649. });
  650. });
  651. describe('_mint(address, uint256)', function () {
  652. it('reverts with a null destination address', async function () {
  653. await expectRevert(
  654. this.token.mint(ZERO_ADDRESS, firstTokenId), 'ERC721: mint to the zero address'
  655. );
  656. });
  657. context('with minted token', async function () {
  658. beforeEach(async function () {
  659. ({ logs: this.logs } = await this.token.mint(owner, firstTokenId));
  660. });
  661. it('emits a Transfer event', function () {
  662. expectEvent.inLogs(this.logs, 'Transfer', { from: ZERO_ADDRESS, to: owner, tokenId: firstTokenId });
  663. });
  664. it('creates the token', async function () {
  665. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  666. expect(await this.token.ownerOf(firstTokenId)).to.equal(owner);
  667. });
  668. it('adjusts owner tokens by index', async function () {
  669. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  670. });
  671. it('adjusts all tokens list', async function () {
  672. expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(firstTokenId);
  673. });
  674. it('reverts when adding a token id that already exists', async function () {
  675. await expectRevert(this.token.mint(owner, firstTokenId), 'ERC721: token already minted');
  676. });
  677. });
  678. });
  679. describe('_burn', function () {
  680. it('reverts when burning a non-existent token id', async function () {
  681. await expectRevert(
  682. this.token.burn(firstTokenId), 'ERC721: owner query for nonexistent token'
  683. );
  684. });
  685. context('with minted tokens', function () {
  686. beforeEach(async function () {
  687. await this.token.mint(owner, firstTokenId);
  688. await this.token.mint(owner, secondTokenId);
  689. });
  690. context('with burnt token', function () {
  691. beforeEach(async function () {
  692. ({ logs: this.logs } = await this.token.burn(firstTokenId));
  693. });
  694. it('emits a Transfer event', function () {
  695. expectEvent.inLogs(this.logs, 'Transfer', { from: owner, to: ZERO_ADDRESS, tokenId: firstTokenId });
  696. });
  697. it('emits an Approval event', function () {
  698. expectEvent.inLogs(this.logs, 'Approval', { owner, approved: ZERO_ADDRESS, tokenId: firstTokenId });
  699. });
  700. it('deletes the token', async function () {
  701. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  702. await expectRevert(
  703. this.token.ownerOf(firstTokenId), 'ERC721: owner query for nonexistent token'
  704. );
  705. });
  706. it('removes that token from the token list of the owner', async function () {
  707. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(secondTokenId);
  708. });
  709. it('adjusts all tokens list', async function () {
  710. expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(secondTokenId);
  711. });
  712. it('burns all tokens', async function () {
  713. await this.token.burn(secondTokenId, { from: owner });
  714. expect(await this.token.totalSupply()).to.be.bignumber.equal('0');
  715. await expectRevert(
  716. this.token.tokenByIndex(0), 'EnumerableMap: index out of bounds'
  717. );
  718. });
  719. it('reverts when burning a token id that has been deleted', async function () {
  720. await expectRevert(
  721. this.token.burn(firstTokenId), 'ERC721: owner query for nonexistent token'
  722. );
  723. });
  724. });
  725. });
  726. });
  727. });