ERC721.behavior.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. const { contract } = require('@openzeppelin/test-environment');
  2. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const { ZERO_ADDRESS } = constants;
  5. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  6. const ERC721Mock = contract.fromArtifact('ERC721Mock');
  7. const ERC721ReceiverMock = contract.fromArtifact('ERC721ReceiverMock');
  8. function shouldBehaveLikeERC721 (
  9. creator,
  10. minter,
  11. [owner, approved, anotherApproved, operator, other]
  12. ) {
  13. const firstTokenId = new BN(1);
  14. const secondTokenId = new BN(2);
  15. const unknownTokenId = new BN(3);
  16. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  17. describe('like an ERC721', function () {
  18. beforeEach(async function () {
  19. await this.token.mint(owner, firstTokenId, { from: minter });
  20. await this.token.mint(owner, secondTokenId, { from: minter });
  21. this.toWhom = other; // default to anyone for toWhom in context-dependent tests
  22. });
  23. describe('balanceOf', function () {
  24. context('when the given address owns some tokens', function () {
  25. it('returns the amount of tokens owned by the given address', async function () {
  26. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  27. });
  28. });
  29. context('when the given address does not own any tokens', function () {
  30. it('returns 0', async function () {
  31. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('0');
  32. });
  33. });
  34. context('when querying the zero address', function () {
  35. it('throws', async function () {
  36. await expectRevert(
  37. this.token.balanceOf(ZERO_ADDRESS), 'ERC721: balance query for the zero address'
  38. );
  39. });
  40. });
  41. });
  42. describe('ownerOf', function () {
  43. context('when the given token ID was tracked by this token', function () {
  44. const tokenId = firstTokenId;
  45. it('returns the owner of the given token ID', async function () {
  46. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  47. });
  48. });
  49. context('when the given token ID was not tracked by this token', function () {
  50. const tokenId = unknownTokenId;
  51. it('reverts', async function () {
  52. await expectRevert(
  53. this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token'
  54. );
  55. });
  56. });
  57. });
  58. describe('transfers', function () {
  59. const tokenId = firstTokenId;
  60. const data = '0x42';
  61. let logs = null;
  62. beforeEach(async function () {
  63. await this.token.approve(approved, tokenId, { from: owner });
  64. await this.token.setApprovalForAll(operator, true, { from: owner });
  65. });
  66. const transferWasSuccessful = function ({ owner, tokenId, approved }) {
  67. it('transfers the ownership of the given token ID to the given address', async function () {
  68. expect(await this.token.ownerOf(tokenId)).to.be.equal(this.toWhom);
  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. if (approved) {
  74. it('emit only a transfer event', async function () {
  75. expectEvent.inLogs(logs, 'Transfer', {
  76. from: owner,
  77. to: this.toWhom,
  78. tokenId: tokenId,
  79. });
  80. });
  81. } else {
  82. it('emits only a transfer event', async function () {
  83. expectEvent.inLogs(logs, 'Transfer', {
  84. from: owner,
  85. to: this.toWhom,
  86. tokenId: tokenId,
  87. });
  88. });
  89. }
  90. it('adjusts owners balances', async function () {
  91. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  92. });
  93. it('adjusts owners tokens by index', async function () {
  94. if (!this.token.tokenOfOwnerByIndex) return;
  95. expect(await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).to.be.bignumber.equal(tokenId);
  96. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.not.equal(tokenId);
  97. });
  98. };
  99. const shouldTransferTokensByUsers = function (transferFunction) {
  100. context('when called by the owner', function () {
  101. beforeEach(async function () {
  102. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner }));
  103. });
  104. transferWasSuccessful({ owner, tokenId, approved });
  105. });
  106. context('when called by the approved individual', function () {
  107. beforeEach(async function () {
  108. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved }));
  109. });
  110. transferWasSuccessful({ owner, tokenId, approved });
  111. });
  112. context('when called by the operator', function () {
  113. beforeEach(async function () {
  114. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  115. });
  116. transferWasSuccessful({ owner, tokenId, approved });
  117. });
  118. context('when called by the owner without an approved user', function () {
  119. beforeEach(async function () {
  120. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  121. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  122. });
  123. transferWasSuccessful({ owner, tokenId, approved: null });
  124. });
  125. context('when sent to the owner', function () {
  126. beforeEach(async function () {
  127. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  128. });
  129. it('keeps ownership of the token', async function () {
  130. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  131. });
  132. it('clears the approval for the token ID', async function () {
  133. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  134. });
  135. it('emits only a transfer event', async function () {
  136. expectEvent.inLogs(logs, 'Transfer', {
  137. from: owner,
  138. to: owner,
  139. tokenId: tokenId,
  140. });
  141. });
  142. it('keeps the owner balance', async function () {
  143. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  144. });
  145. it('keeps same tokens by index', async function () {
  146. if (!this.token.tokenOfOwnerByIndex) return;
  147. const tokensListed = await Promise.all(
  148. [0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i))
  149. );
  150. expect(tokensListed.map(t => t.toNumber())).to.have.members(
  151. [firstTokenId.toNumber(), secondTokenId.toNumber()]
  152. );
  153. });
  154. });
  155. context('when the address of the previous owner is incorrect', function () {
  156. it('reverts', async function () {
  157. await expectRevert(
  158. transferFunction.call(this, other, other, tokenId, { from: owner }),
  159. 'ERC721: transfer of token that is not own'
  160. );
  161. });
  162. });
  163. context('when the sender is not authorized for the token id', function () {
  164. it('reverts', async function () {
  165. await expectRevert(
  166. transferFunction.call(this, owner, other, tokenId, { from: other }),
  167. 'ERC721: transfer caller is not owner nor approved'
  168. );
  169. });
  170. });
  171. context('when the given token ID does not exist', function () {
  172. it('reverts', async function () {
  173. await expectRevert(
  174. transferFunction.call(this, owner, other, unknownTokenId, { from: owner }),
  175. 'ERC721: operator query for nonexistent token'
  176. );
  177. });
  178. });
  179. context('when the address to transfer the token to is the zero address', function () {
  180. it('reverts', async function () {
  181. await expectRevert(
  182. transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
  183. 'ERC721: transfer to the zero address'
  184. );
  185. });
  186. });
  187. };
  188. describe('via transferFrom', function () {
  189. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  190. return this.token.transferFrom(from, to, tokenId, opts);
  191. });
  192. });
  193. describe('via safeTransferFrom', function () {
  194. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  195. return this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
  196. };
  197. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  198. return this.token.methods['safeTransferFrom(address,address,uint256)'](from, to, tokenId, opts);
  199. };
  200. const shouldTransferSafely = function (transferFun, data) {
  201. describe('to a user account', function () {
  202. shouldTransferTokensByUsers(transferFun);
  203. });
  204. describe('to a valid receiver contract', function () {
  205. beforeEach(async function () {
  206. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  207. this.toWhom = this.receiver.address;
  208. });
  209. shouldTransferTokensByUsers(transferFun);
  210. it('should call onERC721Received', async function () {
  211. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  212. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  213. operator: owner,
  214. from: owner,
  215. tokenId: tokenId,
  216. data: data,
  217. });
  218. });
  219. it('should call onERC721Received from approved', async function () {
  220. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
  221. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  222. operator: approved,
  223. from: owner,
  224. tokenId: tokenId,
  225. data: data,
  226. });
  227. });
  228. describe('with an invalid token id', function () {
  229. it('reverts', async function () {
  230. await expectRevert(
  231. transferFun.call(
  232. this,
  233. owner,
  234. this.receiver.address,
  235. unknownTokenId,
  236. { from: owner },
  237. ),
  238. 'ERC721: operator query for nonexistent token'
  239. );
  240. });
  241. });
  242. });
  243. };
  244. describe('with data', function () {
  245. shouldTransferSafely(safeTransferFromWithData, data);
  246. });
  247. describe('without data', function () {
  248. shouldTransferSafely(safeTransferFromWithoutData, null);
  249. });
  250. describe('to a receiver contract returning unexpected value', function () {
  251. it('reverts', async function () {
  252. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  253. await expectRevert(
  254. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
  255. 'ERC721: transfer to non ERC721Receiver implementer'
  256. );
  257. });
  258. });
  259. describe('to a receiver contract that throws', function () {
  260. it('reverts', async function () {
  261. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  262. await expectRevert(
  263. this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
  264. 'ERC721ReceiverMock: reverting'
  265. );
  266. });
  267. });
  268. describe('to a contract that does not implement the required function', function () {
  269. it('reverts', async function () {
  270. const nonReceiver = this.token;
  271. await expectRevert(
  272. this.token.safeTransferFrom(owner, nonReceiver.address, tokenId, { from: owner }),
  273. 'ERC721: transfer to non ERC721Receiver implementer'
  274. );
  275. });
  276. });
  277. });
  278. });
  279. describe('safe mint', function () {
  280. const fourthTokenId = new BN(4);
  281. const tokenId = fourthTokenId;
  282. const data = '0x42';
  283. beforeEach(async function () {
  284. this.ERC721Mock = await ERC721Mock.new();
  285. });
  286. describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
  287. it('should call onERC721Received — with data', async function () {
  288. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  289. const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId, data);
  290. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  291. from: ZERO_ADDRESS,
  292. tokenId: tokenId,
  293. data: data,
  294. });
  295. });
  296. it('should call onERC721Received — without data', async function () {
  297. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  298. const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId);
  299. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  300. from: ZERO_ADDRESS,
  301. tokenId: tokenId,
  302. });
  303. });
  304. context('to a receiver contract returning unexpected value', function () {
  305. it('reverts', async function () {
  306. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  307. await expectRevert(
  308. this.ERC721Mock.safeMint(invalidReceiver.address, tokenId),
  309. 'ERC721: transfer to non ERC721Receiver implementer'
  310. );
  311. });
  312. });
  313. context('to a receiver contract that throws', function () {
  314. it('reverts', async function () {
  315. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  316. await expectRevert(
  317. this.ERC721Mock.safeMint(revertingReceiver.address, tokenId),
  318. 'ERC721ReceiverMock: reverting'
  319. );
  320. });
  321. });
  322. context('to a contract that does not implement the required function', function () {
  323. it('reverts', async function () {
  324. const nonReceiver = this.token;
  325. await expectRevert(
  326. this.ERC721Mock.safeMint(nonReceiver.address, tokenId),
  327. 'ERC721: transfer to non ERC721Receiver implementer'
  328. );
  329. });
  330. });
  331. });
  332. });
  333. describe('approve', function () {
  334. const tokenId = firstTokenId;
  335. let logs = null;
  336. const itClearsApproval = function () {
  337. it('clears approval for the token', async function () {
  338. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  339. });
  340. };
  341. const itApproves = function (address) {
  342. it('sets the approval for the target address', async function () {
  343. expect(await this.token.getApproved(tokenId)).to.be.equal(address);
  344. });
  345. };
  346. const itEmitsApprovalEvent = function (address) {
  347. it('emits an approval event', async function () {
  348. expectEvent.inLogs(logs, 'Approval', {
  349. owner: owner,
  350. approved: address,
  351. tokenId: tokenId,
  352. });
  353. });
  354. };
  355. context('when clearing approval', function () {
  356. context('when there was no prior approval', function () {
  357. beforeEach(async function () {
  358. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  359. });
  360. itClearsApproval();
  361. itEmitsApprovalEvent(ZERO_ADDRESS);
  362. });
  363. context('when there was a prior approval', function () {
  364. beforeEach(async function () {
  365. await this.token.approve(approved, tokenId, { from: owner });
  366. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  367. });
  368. itClearsApproval();
  369. itEmitsApprovalEvent(ZERO_ADDRESS);
  370. });
  371. });
  372. context('when approving a non-zero address', function () {
  373. context('when there was no prior approval', function () {
  374. beforeEach(async function () {
  375. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  376. });
  377. itApproves(approved);
  378. itEmitsApprovalEvent(approved);
  379. });
  380. context('when there was a prior approval to the same address', function () {
  381. beforeEach(async function () {
  382. await this.token.approve(approved, tokenId, { from: owner });
  383. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  384. });
  385. itApproves(approved);
  386. itEmitsApprovalEvent(approved);
  387. });
  388. context('when there was a prior approval to a different address', function () {
  389. beforeEach(async function () {
  390. await this.token.approve(anotherApproved, tokenId, { from: owner });
  391. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  392. });
  393. itApproves(anotherApproved);
  394. itEmitsApprovalEvent(anotherApproved);
  395. });
  396. });
  397. context('when the address that receives the approval is the owner', function () {
  398. it('reverts', async function () {
  399. await expectRevert(
  400. this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner'
  401. );
  402. });
  403. });
  404. context('when the sender does not own the given token ID', function () {
  405. it('reverts', async function () {
  406. await expectRevert(this.token.approve(approved, tokenId, { from: other }),
  407. 'ERC721: approve caller is not owner nor approved');
  408. });
  409. });
  410. context('when the sender is approved for the given token ID', function () {
  411. it('reverts', async function () {
  412. await this.token.approve(approved, tokenId, { from: owner });
  413. await expectRevert(this.token.approve(anotherApproved, tokenId, { from: approved }),
  414. 'ERC721: approve caller is not owner nor approved for all');
  415. });
  416. });
  417. context('when the sender is an operator', function () {
  418. beforeEach(async function () {
  419. await this.token.setApprovalForAll(operator, true, { from: owner });
  420. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  421. });
  422. itApproves(approved);
  423. itEmitsApprovalEvent(approved);
  424. });
  425. context('when the given token ID does not exist', function () {
  426. it('reverts', async function () {
  427. await expectRevert(this.token.approve(approved, unknownTokenId, { from: operator }),
  428. 'ERC721: owner query for nonexistent token');
  429. });
  430. });
  431. });
  432. describe('setApprovalForAll', function () {
  433. context('when the operator willing to approve is not the owner', function () {
  434. context('when there is no operator approval set by the sender', function () {
  435. it('approves the operator', async function () {
  436. await this.token.setApprovalForAll(operator, true, { from: owner });
  437. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  438. });
  439. it('emits an approval event', async function () {
  440. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  441. expectEvent.inLogs(logs, 'ApprovalForAll', {
  442. owner: owner,
  443. operator: operator,
  444. approved: true,
  445. });
  446. });
  447. });
  448. context('when the operator was set as not approved', function () {
  449. beforeEach(async function () {
  450. await this.token.setApprovalForAll(operator, false, { from: owner });
  451. });
  452. it('approves the operator', async function () {
  453. await this.token.setApprovalForAll(operator, true, { from: owner });
  454. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  455. });
  456. it('emits an approval event', async function () {
  457. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  458. expectEvent.inLogs(logs, 'ApprovalForAll', {
  459. owner: owner,
  460. operator: operator,
  461. approved: true,
  462. });
  463. });
  464. it('can unset the operator approval', async function () {
  465. await this.token.setApprovalForAll(operator, false, { from: owner });
  466. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
  467. });
  468. });
  469. context('when the operator was already approved', function () {
  470. beforeEach(async function () {
  471. await this.token.setApprovalForAll(operator, true, { from: owner });
  472. });
  473. it('keeps the approval to the given address', async function () {
  474. await this.token.setApprovalForAll(operator, true, { from: owner });
  475. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  476. });
  477. it('emits an approval event', async function () {
  478. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  479. expectEvent.inLogs(logs, 'ApprovalForAll', {
  480. owner: owner,
  481. operator: operator,
  482. approved: true,
  483. });
  484. });
  485. });
  486. });
  487. context('when the operator is the owner', function () {
  488. it('reverts', async function () {
  489. await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }),
  490. 'ERC721: approve to caller');
  491. });
  492. });
  493. });
  494. describe('getApproved', async function () {
  495. context('when token is not minted', async function () {
  496. it('reverts', async function () {
  497. await expectRevert(
  498. this.token.getApproved(unknownTokenId, { from: minter }),
  499. 'ERC721: approved query for nonexistent token'
  500. );
  501. });
  502. });
  503. context('when token has been minted ', async function () {
  504. it('should return the zero address', async function () {
  505. expect(await this.token.getApproved(firstTokenId)).to.be.equal(
  506. ZERO_ADDRESS
  507. );
  508. });
  509. context('when account has been approved', async function () {
  510. beforeEach(async function () {
  511. await this.token.approve(approved, firstTokenId, { from: owner });
  512. });
  513. it('should return approved account', async function () {
  514. expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
  515. });
  516. });
  517. });
  518. });
  519. shouldSupportInterfaces([
  520. 'ERC165',
  521. 'ERC721',
  522. ]);
  523. });
  524. }
  525. module.exports = {
  526. shouldBehaveLikeERC721,
  527. };