ERC721.behavior.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 ERC721ReceiverMock = contract.fromArtifact('ERC721ReceiverMock');
  7. const ERC721Mock = contract.fromArtifact('ERC721Mock');
  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 invalidReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  262. await expectRevert(
  263. this.token.safeTransferFrom(owner, invalidReceiver.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 invalidReceiver = this.token;
  271. await expectRevert.unspecified(
  272. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner })
  273. );
  274. });
  275. });
  276. });
  277. });
  278. describe('safe mint', function () {
  279. const fourthTokenId = new BN(4);
  280. const tokenId = fourthTokenId;
  281. const data = '0x42';
  282. beforeEach(async function () {
  283. this.ERC721Mock = await ERC721Mock.new();
  284. });
  285. describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
  286. it('should call onERC721Received — with data', async function () {
  287. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  288. const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId, data);
  289. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  290. from: ZERO_ADDRESS,
  291. tokenId: tokenId,
  292. data: data,
  293. });
  294. });
  295. it('should call onERC721Received — without data', async function () {
  296. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  297. const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId);
  298. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  299. from: ZERO_ADDRESS,
  300. tokenId: tokenId,
  301. });
  302. });
  303. context('to a receiver contract returning unexpected value', function () {
  304. it('reverts', async function () {
  305. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  306. await expectRevert(
  307. this.ERC721Mock.safeMint(invalidReceiver.address, tokenId),
  308. 'ERC721: transfer to non ERC721Receiver implementer'
  309. );
  310. });
  311. });
  312. context('to a receiver contract that throws', function () {
  313. it('reverts', async function () {
  314. const invalidReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  315. await expectRevert(
  316. this.ERC721Mock.safeMint(invalidReceiver.address, tokenId),
  317. 'ERC721ReceiverMock: reverting'
  318. );
  319. });
  320. });
  321. context('to a contract that does not implement the required function', function () {
  322. it('reverts', async function () {
  323. const invalidReceiver = this.token;
  324. await expectRevert.unspecified(
  325. this.ERC721Mock.safeMint(invalidReceiver.address, tokenId)
  326. );
  327. });
  328. });
  329. });
  330. });
  331. describe('approve', function () {
  332. const tokenId = firstTokenId;
  333. let logs = null;
  334. const itClearsApproval = function () {
  335. it('clears approval for the token', async function () {
  336. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  337. });
  338. };
  339. const itApproves = function (address) {
  340. it('sets the approval for the target address', async function () {
  341. expect(await this.token.getApproved(tokenId)).to.be.equal(address);
  342. });
  343. };
  344. const itEmitsApprovalEvent = function (address) {
  345. it('emits an approval event', async function () {
  346. expectEvent.inLogs(logs, 'Approval', {
  347. owner: owner,
  348. approved: address,
  349. tokenId: tokenId,
  350. });
  351. });
  352. };
  353. context('when clearing approval', function () {
  354. context('when there was no prior approval', function () {
  355. beforeEach(async function () {
  356. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  357. });
  358. itClearsApproval();
  359. itEmitsApprovalEvent(ZERO_ADDRESS);
  360. });
  361. context('when there was a prior approval', function () {
  362. beforeEach(async function () {
  363. await this.token.approve(approved, tokenId, { from: owner });
  364. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  365. });
  366. itClearsApproval();
  367. itEmitsApprovalEvent(ZERO_ADDRESS);
  368. });
  369. });
  370. context('when approving a non-zero address', function () {
  371. context('when there was no prior approval', function () {
  372. beforeEach(async function () {
  373. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  374. });
  375. itApproves(approved);
  376. itEmitsApprovalEvent(approved);
  377. });
  378. context('when there was a prior approval to the same address', function () {
  379. beforeEach(async function () {
  380. await this.token.approve(approved, tokenId, { from: owner });
  381. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  382. });
  383. itApproves(approved);
  384. itEmitsApprovalEvent(approved);
  385. });
  386. context('when there was a prior approval to a different address', function () {
  387. beforeEach(async function () {
  388. await this.token.approve(anotherApproved, tokenId, { from: owner });
  389. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  390. });
  391. itApproves(anotherApproved);
  392. itEmitsApprovalEvent(anotherApproved);
  393. });
  394. });
  395. context('when the address that receives the approval is the owner', function () {
  396. it('reverts', async function () {
  397. await expectRevert(
  398. this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner'
  399. );
  400. });
  401. });
  402. context('when the sender does not own the given token ID', function () {
  403. it('reverts', async function () {
  404. await expectRevert(this.token.approve(approved, tokenId, { from: other }),
  405. 'ERC721: approve caller is not owner nor approved');
  406. });
  407. });
  408. context('when the sender is approved for the given token ID', function () {
  409. it('reverts', async function () {
  410. await this.token.approve(approved, tokenId, { from: owner });
  411. await expectRevert(this.token.approve(anotherApproved, tokenId, { from: approved }),
  412. 'ERC721: approve caller is not owner nor approved for all');
  413. });
  414. });
  415. context('when the sender is an operator', function () {
  416. beforeEach(async function () {
  417. await this.token.setApprovalForAll(operator, true, { from: owner });
  418. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  419. });
  420. itApproves(approved);
  421. itEmitsApprovalEvent(approved);
  422. });
  423. context('when the given token ID does not exist', function () {
  424. it('reverts', async function () {
  425. await expectRevert(this.token.approve(approved, unknownTokenId, { from: operator }),
  426. 'ERC721: owner query for nonexistent token');
  427. });
  428. });
  429. });
  430. describe('setApprovalForAll', function () {
  431. context('when the operator willing to approve is not the owner', function () {
  432. context('when there is no operator approval set by the sender', function () {
  433. it('approves the operator', async function () {
  434. await this.token.setApprovalForAll(operator, true, { from: owner });
  435. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  436. });
  437. it('emits an approval event', async function () {
  438. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  439. expectEvent.inLogs(logs, 'ApprovalForAll', {
  440. owner: owner,
  441. operator: operator,
  442. approved: true,
  443. });
  444. });
  445. });
  446. context('when the operator was set as not approved', function () {
  447. beforeEach(async function () {
  448. await this.token.setApprovalForAll(operator, false, { from: owner });
  449. });
  450. it('approves the operator', async function () {
  451. await this.token.setApprovalForAll(operator, true, { from: owner });
  452. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  453. });
  454. it('emits an approval event', async function () {
  455. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  456. expectEvent.inLogs(logs, 'ApprovalForAll', {
  457. owner: owner,
  458. operator: operator,
  459. approved: true,
  460. });
  461. });
  462. it('can unset the operator approval', async function () {
  463. await this.token.setApprovalForAll(operator, false, { from: owner });
  464. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
  465. });
  466. });
  467. context('when the operator was already approved', function () {
  468. beforeEach(async function () {
  469. await this.token.setApprovalForAll(operator, true, { from: owner });
  470. });
  471. it('keeps the approval to the given address', async function () {
  472. await this.token.setApprovalForAll(operator, true, { from: owner });
  473. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  474. });
  475. it('emits an approval event', async function () {
  476. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  477. expectEvent.inLogs(logs, 'ApprovalForAll', {
  478. owner: owner,
  479. operator: operator,
  480. approved: true,
  481. });
  482. });
  483. });
  484. });
  485. context('when the operator is the owner', function () {
  486. it('reverts', async function () {
  487. await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }),
  488. 'ERC721: approve to caller');
  489. });
  490. });
  491. });
  492. describe('getApproved', async function () {
  493. context('when token is not minted', async function () {
  494. it('reverts', async function () {
  495. await expectRevert(
  496. this.token.getApproved(unknownTokenId, { from: minter }),
  497. 'ERC721: approved query for nonexistent token'
  498. );
  499. });
  500. });
  501. context('when token has been minted ', async function () {
  502. it('should return the zero address', async function () {
  503. expect(await this.token.getApproved(firstTokenId)).to.be.equal(
  504. ZERO_ADDRESS
  505. );
  506. });
  507. context('when account has been approved', async function () {
  508. beforeEach(async function () {
  509. await this.token.approve(approved, firstTokenId, { from: owner });
  510. });
  511. it('should return approved account', async function () {
  512. expect(await this.token.getApproved(firstTokenId)).to.be.equal(approved);
  513. });
  514. });
  515. });
  516. });
  517. shouldSupportInterfaces([
  518. 'ERC165',
  519. 'ERC721',
  520. ]);
  521. });
  522. }
  523. module.exports = {
  524. shouldBehaveLikeERC721,
  525. };