ERC721.behavior.js 37 KB

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