ERC721.behavior.js 36 KB

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