ERC721.behavior.js 38 KB

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