ERC721.behavior.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  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 { expectRevertCustomError } = require('../../helpers/customError');
  6. const { Enum } = require('../../helpers/enums');
  7. const ERC721ReceiverMock = artifacts.require('ERC721ReceiverMock');
  8. const NonERC721ReceiverMock = artifacts.require('CallReceiverMock');
  9. const RevertType = Enum('None', 'RevertWithoutMessage', 'RevertWithMessage', 'RevertWithCustomError', 'Panic');
  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(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 expectRevertCustomError(this.token.balanceOf(ZERO_ADDRESS), 'ERC721InvalidOwner', [ZERO_ADDRESS]);
  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 expectRevertCustomError(this.token.ownerOf(tokenId), 'ERC721NonexistentToken', [tokenId]);
  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 with no event', async function () {
  71. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  72. expectEvent.notEmitted(receipt, 'Approval');
  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, opts = {}) {
  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 expectRevertCustomError(
  141. transferFunction.call(this, other, other, tokenId, { from: owner }),
  142. 'ERC721IncorrectOwner',
  143. [other, tokenId, owner],
  144. );
  145. });
  146. });
  147. context('when the sender is not authorized for the token id', function () {
  148. if (opts.unrestricted) {
  149. it('does not revert', async function () {
  150. await transferFunction.call(this, owner, other, tokenId, { from: other });
  151. });
  152. } else {
  153. it('reverts', async function () {
  154. await expectRevertCustomError(
  155. transferFunction.call(this, owner, other, tokenId, { from: other }),
  156. 'ERC721InsufficientApproval',
  157. [other, tokenId],
  158. );
  159. });
  160. }
  161. });
  162. context('when the given token ID does not exist', function () {
  163. it('reverts', async function () {
  164. await expectRevertCustomError(
  165. transferFunction.call(this, owner, other, nonExistentTokenId, { from: owner }),
  166. 'ERC721NonexistentToken',
  167. [nonExistentTokenId],
  168. );
  169. });
  170. });
  171. context('when the address to transfer the token to is the zero address', function () {
  172. it('reverts', async function () {
  173. await expectRevertCustomError(
  174. transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
  175. 'ERC721InvalidReceiver',
  176. [ZERO_ADDRESS],
  177. );
  178. });
  179. });
  180. };
  181. const shouldTransferSafely = function (transferFun, data, opts = {}) {
  182. describe('to a user account', function () {
  183. shouldTransferTokensByUsers(transferFun, opts);
  184. });
  185. describe('to a valid receiver contract', function () {
  186. beforeEach(async function () {
  187. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, RevertType.None);
  188. this.toWhom = this.receiver.address;
  189. });
  190. shouldTransferTokensByUsers(transferFun, opts);
  191. it('calls onERC721Received', async function () {
  192. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  193. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  194. operator: owner,
  195. from: owner,
  196. tokenId: tokenId,
  197. data: data,
  198. });
  199. });
  200. it('calls onERC721Received from approved', async function () {
  201. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
  202. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  203. operator: approved,
  204. from: owner,
  205. tokenId: tokenId,
  206. data: data,
  207. });
  208. });
  209. describe('with an invalid token id', function () {
  210. it('reverts', async function () {
  211. await expectRevertCustomError(
  212. transferFun.call(this, owner, this.receiver.address, nonExistentTokenId, { from: owner }),
  213. 'ERC721NonexistentToken',
  214. [nonExistentTokenId],
  215. );
  216. });
  217. });
  218. });
  219. };
  220. for (const { fnName, opts } of [
  221. { fnName: 'transferFrom', opts: {} },
  222. { fnName: '$_transfer', opts: { unrestricted: true } },
  223. ]) {
  224. describe(`via ${fnName}`, function () {
  225. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  226. return this.token[fnName](from, to, tokenId, opts);
  227. }, opts);
  228. });
  229. }
  230. for (const { fnName, opts } of [
  231. { fnName: 'safeTransferFrom', opts: {} },
  232. { fnName: '$_safeTransfer', opts: { unrestricted: true } },
  233. ]) {
  234. describe(`via ${fnName}`, function () {
  235. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  236. return this.token.methods[fnName + '(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
  237. };
  238. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  239. return this.token.methods[fnName + '(address,address,uint256)'](from, to, tokenId, opts);
  240. };
  241. describe('with data', function () {
  242. shouldTransferSafely(safeTransferFromWithData, data, opts);
  243. });
  244. describe('without data', function () {
  245. shouldTransferSafely(safeTransferFromWithoutData, null, opts);
  246. });
  247. describe('to a receiver contract returning unexpected value', function () {
  248. it('reverts', async function () {
  249. const invalidReceiver = await ERC721ReceiverMock.new('0x42', RevertType.None);
  250. await expectRevertCustomError(
  251. this.token.methods[fnName + '(address,address,uint256)'](owner, invalidReceiver.address, tokenId, {
  252. from: owner,
  253. }),
  254. 'ERC721InvalidReceiver',
  255. [invalidReceiver.address],
  256. );
  257. });
  258. });
  259. describe('to a receiver contract that reverts with message', function () {
  260. it('reverts', async function () {
  261. const revertingReceiver = await ERC721ReceiverMock.new(
  262. RECEIVER_MAGIC_VALUE,
  263. RevertType.RevertWithMessage,
  264. );
  265. await expectRevert(
  266. this.token.methods[fnName + '(address,address,uint256)'](owner, revertingReceiver.address, tokenId, {
  267. from: owner,
  268. }),
  269. 'ERC721ReceiverMock: reverting',
  270. );
  271. });
  272. });
  273. describe('to a receiver contract that reverts without message', function () {
  274. it('reverts', async function () {
  275. const revertingReceiver = await ERC721ReceiverMock.new(
  276. RECEIVER_MAGIC_VALUE,
  277. RevertType.RevertWithoutMessage,
  278. );
  279. await expectRevertCustomError(
  280. this.token.methods[fnName + '(address,address,uint256)'](owner, revertingReceiver.address, tokenId, {
  281. from: owner,
  282. }),
  283. 'ERC721InvalidReceiver',
  284. [revertingReceiver.address],
  285. );
  286. });
  287. });
  288. describe('to a receiver contract that reverts with custom error', function () {
  289. it('reverts', async function () {
  290. const revertingReceiver = await ERC721ReceiverMock.new(
  291. RECEIVER_MAGIC_VALUE,
  292. RevertType.RevertWithCustomError,
  293. );
  294. await expectRevertCustomError(
  295. this.token.methods[fnName + '(address,address,uint256)'](owner, revertingReceiver.address, tokenId, {
  296. from: owner,
  297. }),
  298. 'CustomError',
  299. [RECEIVER_MAGIC_VALUE],
  300. );
  301. });
  302. });
  303. describe('to a receiver contract that panics', function () {
  304. it('reverts', async function () {
  305. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, RevertType.Panic);
  306. await expectRevert.unspecified(
  307. this.token.methods[fnName + '(address,address,uint256)'](owner, revertingReceiver.address, tokenId, {
  308. from: owner,
  309. }),
  310. );
  311. });
  312. });
  313. describe('to a contract that does not implement the required function', function () {
  314. it('reverts', async function () {
  315. const nonReceiver = await NonERC721ReceiverMock.new();
  316. await expectRevertCustomError(
  317. this.token.methods[fnName + '(address,address,uint256)'](owner, nonReceiver.address, tokenId, {
  318. from: owner,
  319. }),
  320. 'ERC721InvalidReceiver',
  321. [nonReceiver.address],
  322. );
  323. });
  324. });
  325. });
  326. }
  327. });
  328. describe('safe mint', function () {
  329. const tokenId = fourthTokenId;
  330. const data = '0x42';
  331. describe('via safeMint', function () {
  332. // regular minting is tested in ERC721Mintable.test.js and others
  333. it('calls onERC721Received — with data', async function () {
  334. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, RevertType.None);
  335. const receipt = await this.token.$_safeMint(this.receiver.address, tokenId, data);
  336. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  337. from: ZERO_ADDRESS,
  338. tokenId: tokenId,
  339. data: data,
  340. });
  341. });
  342. it('calls onERC721Received — without data', async function () {
  343. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, RevertType.None);
  344. const receipt = await this.token.$_safeMint(this.receiver.address, tokenId);
  345. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  346. from: ZERO_ADDRESS,
  347. tokenId: tokenId,
  348. });
  349. });
  350. context('to a receiver contract returning unexpected value', function () {
  351. it('reverts', async function () {
  352. const invalidReceiver = await ERC721ReceiverMock.new('0x42', RevertType.None);
  353. await expectRevertCustomError(
  354. this.token.$_safeMint(invalidReceiver.address, tokenId),
  355. 'ERC721InvalidReceiver',
  356. [invalidReceiver.address],
  357. );
  358. });
  359. });
  360. context('to a receiver contract that reverts with message', function () {
  361. it('reverts', async function () {
  362. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, RevertType.RevertWithMessage);
  363. await expectRevert(
  364. this.token.$_safeMint(revertingReceiver.address, tokenId),
  365. 'ERC721ReceiverMock: reverting',
  366. );
  367. });
  368. });
  369. context('to a receiver contract that reverts without message', function () {
  370. it('reverts', async function () {
  371. const revertingReceiver = await ERC721ReceiverMock.new(
  372. RECEIVER_MAGIC_VALUE,
  373. RevertType.RevertWithoutMessage,
  374. );
  375. await expectRevertCustomError(
  376. this.token.$_safeMint(revertingReceiver.address, tokenId),
  377. 'ERC721InvalidReceiver',
  378. [revertingReceiver.address],
  379. );
  380. });
  381. });
  382. context('to a receiver contract that reverts with custom error', function () {
  383. it('reverts', async function () {
  384. const revertingReceiver = await ERC721ReceiverMock.new(
  385. RECEIVER_MAGIC_VALUE,
  386. RevertType.RevertWithCustomError,
  387. );
  388. await expectRevertCustomError(this.token.$_safeMint(revertingReceiver.address, tokenId), 'CustomError', [
  389. RECEIVER_MAGIC_VALUE,
  390. ]);
  391. });
  392. });
  393. context('to a receiver contract that panics', function () {
  394. it('reverts', async function () {
  395. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, RevertType.Panic);
  396. await expectRevert.unspecified(this.token.$_safeMint(revertingReceiver.address, tokenId));
  397. });
  398. });
  399. context('to a contract that does not implement the required function', function () {
  400. it('reverts', async function () {
  401. const nonReceiver = await NonERC721ReceiverMock.new();
  402. await expectRevertCustomError(
  403. this.token.$_safeMint(nonReceiver.address, tokenId),
  404. 'ERC721InvalidReceiver',
  405. [nonReceiver.address],
  406. );
  407. });
  408. });
  409. });
  410. });
  411. describe('approve', function () {
  412. const tokenId = firstTokenId;
  413. let receipt = null;
  414. const itClearsApproval = function () {
  415. it('clears approval for the token', async function () {
  416. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  417. });
  418. };
  419. const itApproves = function (address) {
  420. it('sets the approval for the target address', async function () {
  421. expect(await this.token.getApproved(tokenId)).to.be.equal(address);
  422. });
  423. };
  424. const itEmitsApprovalEvent = function (address) {
  425. it('emits an approval event', async function () {
  426. expectEvent(receipt, 'Approval', {
  427. owner: owner,
  428. approved: address,
  429. tokenId: tokenId,
  430. });
  431. });
  432. };
  433. context('when clearing approval', function () {
  434. context('when there was no prior approval', function () {
  435. beforeEach(async function () {
  436. receipt = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  437. });
  438. itClearsApproval();
  439. itEmitsApprovalEvent(ZERO_ADDRESS);
  440. });
  441. context('when there was a prior approval', function () {
  442. beforeEach(async function () {
  443. await this.token.approve(approved, tokenId, { from: owner });
  444. receipt = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  445. });
  446. itClearsApproval();
  447. itEmitsApprovalEvent(ZERO_ADDRESS);
  448. });
  449. });
  450. context('when approving a non-zero address', function () {
  451. context('when there was no prior approval', function () {
  452. beforeEach(async function () {
  453. receipt = await this.token.approve(approved, tokenId, { from: owner });
  454. });
  455. itApproves(approved);
  456. itEmitsApprovalEvent(approved);
  457. });
  458. context('when there was a prior approval to the same address', function () {
  459. beforeEach(async function () {
  460. await this.token.approve(approved, tokenId, { from: owner });
  461. receipt = await this.token.approve(approved, tokenId, { from: owner });
  462. });
  463. itApproves(approved);
  464. itEmitsApprovalEvent(approved);
  465. });
  466. context('when there was a prior approval to a different address', function () {
  467. beforeEach(async function () {
  468. await this.token.approve(anotherApproved, tokenId, { from: owner });
  469. receipt = await this.token.approve(anotherApproved, tokenId, { from: owner });
  470. });
  471. itApproves(anotherApproved);
  472. itEmitsApprovalEvent(anotherApproved);
  473. });
  474. });
  475. context('when the sender does not own the given token ID', function () {
  476. it('reverts', async function () {
  477. await expectRevertCustomError(
  478. this.token.approve(approved, tokenId, { from: other }),
  479. 'ERC721InvalidApprover',
  480. [other],
  481. );
  482. });
  483. });
  484. context('when the sender is approved for the given token ID', function () {
  485. it('reverts', async function () {
  486. await this.token.approve(approved, tokenId, { from: owner });
  487. await expectRevertCustomError(
  488. this.token.approve(anotherApproved, tokenId, { from: approved }),
  489. 'ERC721InvalidApprover',
  490. [approved],
  491. );
  492. });
  493. });
  494. context('when the sender is an operator', function () {
  495. beforeEach(async function () {
  496. await this.token.setApprovalForAll(operator, true, { from: owner });
  497. receipt = await this.token.approve(approved, tokenId, { from: operator });
  498. });
  499. itApproves(approved);
  500. itEmitsApprovalEvent(approved);
  501. });
  502. context('when the given token ID does not exist', function () {
  503. it('reverts', async function () {
  504. await expectRevertCustomError(
  505. this.token.approve(approved, nonExistentTokenId, { from: operator }),
  506. 'ERC721NonexistentToken',
  507. [nonExistentTokenId],
  508. );
  509. });
  510. });
  511. });
  512. describe('setApprovalForAll', function () {
  513. context('when the operator willing to approve is not the owner', function () {
  514. context('when there is no operator approval set by the sender', function () {
  515. it('approves the operator', async function () {
  516. await this.token.setApprovalForAll(operator, true, { from: owner });
  517. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  518. });
  519. it('emits an approval event', async function () {
  520. const receipt = await this.token.setApprovalForAll(operator, true, { from: owner });
  521. expectEvent(receipt, 'ApprovalForAll', {
  522. owner: owner,
  523. operator: operator,
  524. approved: true,
  525. });
  526. });
  527. });
  528. context('when the operator was set as not approved', function () {
  529. beforeEach(async function () {
  530. await this.token.setApprovalForAll(operator, false, { from: owner });
  531. });
  532. it('approves the operator', async function () {
  533. await this.token.setApprovalForAll(operator, true, { from: owner });
  534. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  535. });
  536. it('emits an approval event', async function () {
  537. const receipt = await this.token.setApprovalForAll(operator, true, { from: owner });
  538. expectEvent(receipt, 'ApprovalForAll', {
  539. owner: owner,
  540. operator: operator,
  541. approved: true,
  542. });
  543. });
  544. it('can unset the operator approval', async function () {
  545. await this.token.setApprovalForAll(operator, false, { from: owner });
  546. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
  547. });
  548. });
  549. context('when the operator was already approved', function () {
  550. beforeEach(async function () {
  551. await this.token.setApprovalForAll(operator, true, { from: owner });
  552. });
  553. it('keeps the approval to the given address', async function () {
  554. await this.token.setApprovalForAll(operator, true, { from: owner });
  555. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  556. });
  557. it('emits an approval event', async function () {
  558. const receipt = await this.token.setApprovalForAll(operator, true, { from: owner });
  559. expectEvent(receipt, 'ApprovalForAll', {
  560. owner: owner,
  561. operator: operator,
  562. approved: true,
  563. });
  564. });
  565. });
  566. });
  567. context('when the operator is address zero', function () {
  568. it('reverts', async function () {
  569. await expectRevertCustomError(
  570. this.token.setApprovalForAll(constants.ZERO_ADDRESS, true, { from: owner }),
  571. 'ERC721InvalidOperator',
  572. [constants.ZERO_ADDRESS],
  573. );
  574. });
  575. });
  576. });
  577. describe('getApproved', async function () {
  578. context('when token is not minted', async function () {
  579. it('reverts', async function () {
  580. await expectRevertCustomError(this.token.getApproved(nonExistentTokenId), 'ERC721NonexistentToken', [
  581. nonExistentTokenId,
  582. ]);
  583. });
  584. });
  585. context('when token has been minted ', async function () {
  586. it('should return the zero address', async function () {
  587. expect(await this.token.getApproved(firstTokenId)).to.be.equal(ZERO_ADDRESS);
  588. });
  589. context('when account has been approved', async function () {
  590. beforeEach(async function () {
  591. await this.token.approve(approved, firstTokenId, { from: owner });
  592. });
  593. it('returns approved account', async function () {
  594. expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
  595. });
  596. });
  597. });
  598. });
  599. });
  600. describe('_mint(address, uint256)', function () {
  601. it('reverts with a null destination address', async function () {
  602. await expectRevertCustomError(this.token.$_mint(ZERO_ADDRESS, firstTokenId), 'ERC721InvalidReceiver', [
  603. ZERO_ADDRESS,
  604. ]);
  605. });
  606. context('with minted token', async function () {
  607. beforeEach(async function () {
  608. this.receipt = await this.token.$_mint(owner, firstTokenId);
  609. });
  610. it('emits a Transfer event', function () {
  611. expectEvent(this.receipt, 'Transfer', { from: ZERO_ADDRESS, to: owner, tokenId: firstTokenId });
  612. });
  613. it('creates the token', async function () {
  614. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  615. expect(await this.token.ownerOf(firstTokenId)).to.equal(owner);
  616. });
  617. it('reverts when adding a token id that already exists', async function () {
  618. await expectRevertCustomError(this.token.$_mint(owner, firstTokenId), 'ERC721InvalidSender', [ZERO_ADDRESS]);
  619. });
  620. });
  621. });
  622. describe('_burn', function () {
  623. it('reverts when burning a non-existent token id', async function () {
  624. await expectRevertCustomError(this.token.$_burn(nonExistentTokenId), 'ERC721NonexistentToken', [
  625. nonExistentTokenId,
  626. ]);
  627. });
  628. context('with minted tokens', function () {
  629. beforeEach(async function () {
  630. await this.token.$_mint(owner, firstTokenId);
  631. await this.token.$_mint(owner, secondTokenId);
  632. });
  633. context('with burnt token', function () {
  634. beforeEach(async function () {
  635. this.receipt = await this.token.$_burn(firstTokenId);
  636. });
  637. it('emits a Transfer event', function () {
  638. expectEvent(this.receipt, 'Transfer', { from: owner, to: ZERO_ADDRESS, tokenId: firstTokenId });
  639. });
  640. it('deletes the token', async function () {
  641. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  642. await expectRevertCustomError(this.token.ownerOf(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  643. });
  644. it('reverts when burning a token id that has been deleted', async function () {
  645. await expectRevertCustomError(this.token.$_burn(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  646. });
  647. });
  648. });
  649. });
  650. }
  651. function shouldBehaveLikeERC721Enumerable(owner, newOwner, approved, anotherApproved, operator, other) {
  652. shouldSupportInterfaces(['ERC721Enumerable']);
  653. context('with minted tokens', function () {
  654. beforeEach(async function () {
  655. await this.token.$_mint(owner, firstTokenId);
  656. await this.token.$_mint(owner, secondTokenId);
  657. this.toWhom = other; // default to other for toWhom in context-dependent tests
  658. });
  659. describe('totalSupply', function () {
  660. it('returns total token supply', async function () {
  661. expect(await this.token.totalSupply()).to.be.bignumber.equal('2');
  662. });
  663. });
  664. describe('tokenOfOwnerByIndex', function () {
  665. describe('when the given index is lower than the amount of tokens owned by the given address', function () {
  666. it('returns the token ID placed at the given index', async function () {
  667. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  668. });
  669. });
  670. describe('when the index is greater than or equal to the total tokens owned by the given address', function () {
  671. it('reverts', async function () {
  672. await expectRevertCustomError(this.token.tokenOfOwnerByIndex(owner, 2), 'ERC721OutOfBoundsIndex', [owner, 2]);
  673. });
  674. });
  675. describe('when the given address does not own any token', function () {
  676. it('reverts', async function () {
  677. await expectRevertCustomError(this.token.tokenOfOwnerByIndex(other, 0), 'ERC721OutOfBoundsIndex', [other, 0]);
  678. });
  679. });
  680. describe('after transferring all tokens to another user', function () {
  681. beforeEach(async function () {
  682. await this.token.transferFrom(owner, other, firstTokenId, { from: owner });
  683. await this.token.transferFrom(owner, other, secondTokenId, { from: owner });
  684. });
  685. it('returns correct token IDs for target', async function () {
  686. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('2');
  687. const tokensListed = await Promise.all([0, 1].map(i => this.token.tokenOfOwnerByIndex(other, i)));
  688. expect(tokensListed.map(t => t.toNumber())).to.have.members([
  689. firstTokenId.toNumber(),
  690. secondTokenId.toNumber(),
  691. ]);
  692. });
  693. it('returns empty collection for original owner', async function () {
  694. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('0');
  695. await expectRevertCustomError(this.token.tokenOfOwnerByIndex(owner, 0), 'ERC721OutOfBoundsIndex', [owner, 0]);
  696. });
  697. });
  698. });
  699. describe('tokenByIndex', function () {
  700. it('returns all tokens', async function () {
  701. const tokensListed = await Promise.all([0, 1].map(i => this.token.tokenByIndex(i)));
  702. expect(tokensListed.map(t => t.toNumber())).to.have.members([
  703. firstTokenId.toNumber(),
  704. secondTokenId.toNumber(),
  705. ]);
  706. });
  707. it('reverts if index is greater than supply', async function () {
  708. await expectRevertCustomError(this.token.tokenByIndex(2), 'ERC721OutOfBoundsIndex', [ZERO_ADDRESS, 2]);
  709. });
  710. [firstTokenId, secondTokenId].forEach(function (tokenId) {
  711. it(`returns all tokens after burning token ${tokenId} and minting new tokens`, async function () {
  712. const newTokenId = new BN(300);
  713. const anotherNewTokenId = new BN(400);
  714. await this.token.$_burn(tokenId);
  715. await this.token.$_mint(newOwner, newTokenId);
  716. await this.token.$_mint(newOwner, anotherNewTokenId);
  717. expect(await this.token.totalSupply()).to.be.bignumber.equal('3');
  718. const tokensListed = await Promise.all([0, 1, 2].map(i => this.token.tokenByIndex(i)));
  719. const expectedTokens = [firstTokenId, secondTokenId, newTokenId, anotherNewTokenId].filter(
  720. x => x !== tokenId,
  721. );
  722. expect(tokensListed.map(t => t.toNumber())).to.have.members(expectedTokens.map(t => t.toNumber()));
  723. });
  724. });
  725. });
  726. });
  727. describe('_mint(address, uint256)', function () {
  728. it('reverts with a null destination address', async function () {
  729. await expectRevertCustomError(this.token.$_mint(ZERO_ADDRESS, firstTokenId), 'ERC721InvalidReceiver', [
  730. ZERO_ADDRESS,
  731. ]);
  732. });
  733. context('with minted token', async function () {
  734. beforeEach(async function () {
  735. this.receipt = await this.token.$_mint(owner, firstTokenId);
  736. });
  737. it('adjusts owner tokens by index', async function () {
  738. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(firstTokenId);
  739. });
  740. it('adjusts all tokens list', async function () {
  741. expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(firstTokenId);
  742. });
  743. });
  744. });
  745. describe('_burn', function () {
  746. it('reverts when burning a non-existent token id', async function () {
  747. await expectRevertCustomError(this.token.$_burn(firstTokenId), 'ERC721NonexistentToken', [firstTokenId]);
  748. });
  749. context('with minted tokens', function () {
  750. beforeEach(async function () {
  751. await this.token.$_mint(owner, firstTokenId);
  752. await this.token.$_mint(owner, secondTokenId);
  753. });
  754. context('with burnt token', function () {
  755. beforeEach(async function () {
  756. this.receipt = await this.token.$_burn(firstTokenId);
  757. });
  758. it('removes that token from the token list of the owner', async function () {
  759. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.equal(secondTokenId);
  760. });
  761. it('adjusts all tokens list', async function () {
  762. expect(await this.token.tokenByIndex(0)).to.be.bignumber.equal(secondTokenId);
  763. });
  764. it('burns all tokens', async function () {
  765. await this.token.$_burn(secondTokenId, { from: owner });
  766. expect(await this.token.totalSupply()).to.be.bignumber.equal('0');
  767. await expectRevertCustomError(this.token.tokenByIndex(0), 'ERC721OutOfBoundsIndex', [ZERO_ADDRESS, 0]);
  768. });
  769. });
  770. });
  771. });
  772. }
  773. function shouldBehaveLikeERC721Metadata(name, symbol, owner) {
  774. shouldSupportInterfaces(['ERC721Metadata']);
  775. describe('metadata', function () {
  776. it('has a name', async function () {
  777. expect(await this.token.name()).to.be.equal(name);
  778. });
  779. it('has a symbol', async function () {
  780. expect(await this.token.symbol()).to.be.equal(symbol);
  781. });
  782. describe('token URI', function () {
  783. beforeEach(async function () {
  784. await this.token.$_mint(owner, firstTokenId);
  785. });
  786. it('return empty string by default', async function () {
  787. expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
  788. });
  789. it('reverts when queried for non existent token id', async function () {
  790. await expectRevertCustomError(this.token.tokenURI(nonExistentTokenId), 'ERC721NonexistentToken', [
  791. nonExistentTokenId,
  792. ]);
  793. });
  794. describe('base URI', function () {
  795. beforeEach(function () {
  796. if (this.token.setBaseURI === undefined) {
  797. this.skip();
  798. }
  799. });
  800. it('base URI can be set', async function () {
  801. await this.token.setBaseURI(baseURI);
  802. expect(await this.token.baseURI()).to.equal(baseURI);
  803. });
  804. it('base URI is added as a prefix to the token URI', async function () {
  805. await this.token.setBaseURI(baseURI);
  806. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId.toString());
  807. });
  808. it('token URI can be changed by changing the base URI', async function () {
  809. await this.token.setBaseURI(baseURI);
  810. const newBaseURI = 'https://api.example.com/v2/';
  811. await this.token.setBaseURI(newBaseURI);
  812. expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + firstTokenId.toString());
  813. });
  814. });
  815. });
  816. });
  817. }
  818. module.exports = {
  819. shouldBehaveLikeERC721,
  820. shouldBehaveLikeERC721Enumerable,
  821. shouldBehaveLikeERC721Metadata,
  822. };