ERC721.behavior.js 34 KB

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