ERC721.behavior.js 36 KB

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