ERC721.test.js 34 KB

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