ERC721.behavior.js 38 KB

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