ERC721.behavior.js 37 KB

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