ERC721.behavior.js 34 KB

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