ERC721.test.js 33 KB

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