ERC721.behavior.js 34 KB

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