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 ERC721Mock = contract.fromArtifact('ERC721Mock');
  7. const ERC721ReceiverMock = contract.fromArtifact('ERC721ReceiverMock');
  8. function shouldBehaveLikeERC721 (
  9. [owner, approved, anotherApproved, operator, other]
  10. ) {
  11. const firstTokenId = new BN(1);
  12. const secondTokenId = new BN(2);
  13. const unknownTokenId = new BN(3);
  14. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  15. describe('like an ERC721', function () {
  16. beforeEach(async function () {
  17. await this.token.mint(owner, firstTokenId);
  18. await this.token.mint(owner, secondTokenId);
  19. this.toWhom = other; // default to anyone for toWhom in context-dependent tests
  20. });
  21. describe('balanceOf', function () {
  22. context('when the given address owns some tokens', function () {
  23. it('returns the amount of tokens owned by the given address', async function () {
  24. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  25. });
  26. });
  27. context('when the given address does not own any tokens', function () {
  28. it('returns 0', async function () {
  29. expect(await this.token.balanceOf(other)).to.be.bignumber.equal('0');
  30. });
  31. });
  32. context('when querying the zero address', function () {
  33. it('throws', async function () {
  34. await expectRevert(
  35. this.token.balanceOf(ZERO_ADDRESS), 'ERC721: balance query for the zero address'
  36. );
  37. });
  38. });
  39. });
  40. describe('ownerOf', function () {
  41. context('when the given token ID was tracked by this token', function () {
  42. const tokenId = firstTokenId;
  43. it('returns the owner of the given token ID', async function () {
  44. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  45. });
  46. });
  47. context('when the given token ID was not tracked by this token', function () {
  48. const tokenId = unknownTokenId;
  49. it('reverts', async function () {
  50. await expectRevert(
  51. this.token.ownerOf(tokenId), 'ERC721: owner query for nonexistent token'
  52. );
  53. });
  54. });
  55. });
  56. describe('transfers', function () {
  57. const tokenId = firstTokenId;
  58. const data = '0x42';
  59. let logs = null;
  60. beforeEach(async function () {
  61. await this.token.approve(approved, tokenId, { from: owner });
  62. await this.token.setApprovalForAll(operator, true, { from: owner });
  63. });
  64. const transferWasSuccessful = function ({ owner, tokenId, approved }) {
  65. it('transfers the ownership of the given token ID to the given address', async function () {
  66. expect(await this.token.ownerOf(tokenId)).to.be.equal(this.toWhom);
  67. });
  68. it('clears the approval for the token ID', async function () {
  69. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  70. });
  71. if (approved) {
  72. it('emit only a transfer event', async function () {
  73. expectEvent.inLogs(logs, 'Transfer', {
  74. from: owner,
  75. to: this.toWhom,
  76. tokenId: tokenId,
  77. });
  78. });
  79. } else {
  80. it('emits only a transfer event', async function () {
  81. expectEvent.inLogs(logs, 'Transfer', {
  82. from: owner,
  83. to: this.toWhom,
  84. tokenId: tokenId,
  85. });
  86. });
  87. }
  88. it('adjusts owners balances', async function () {
  89. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('1');
  90. });
  91. it('adjusts owners tokens by index', async function () {
  92. if (!this.token.tokenOfOwnerByIndex) return;
  93. expect(await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).to.be.bignumber.equal(tokenId);
  94. expect(await this.token.tokenOfOwnerByIndex(owner, 0)).to.be.bignumber.not.equal(tokenId);
  95. });
  96. };
  97. const shouldTransferTokensByUsers = function (transferFunction) {
  98. context('when called by the owner', function () {
  99. beforeEach(async function () {
  100. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner }));
  101. });
  102. transferWasSuccessful({ owner, tokenId, approved });
  103. });
  104. context('when called by the approved individual', function () {
  105. beforeEach(async function () {
  106. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved }));
  107. });
  108. transferWasSuccessful({ owner, tokenId, approved });
  109. });
  110. context('when called by the operator', function () {
  111. beforeEach(async function () {
  112. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  113. });
  114. transferWasSuccessful({ owner, tokenId, approved });
  115. });
  116. context('when called by the owner without an approved user', function () {
  117. beforeEach(async function () {
  118. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  119. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  120. });
  121. transferWasSuccessful({ owner, tokenId, approved: null });
  122. });
  123. context('when sent to the owner', function () {
  124. beforeEach(async function () {
  125. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  126. });
  127. it('keeps ownership of the token', async function () {
  128. expect(await this.token.ownerOf(tokenId)).to.be.equal(owner);
  129. });
  130. it('clears the approval for the token ID', async function () {
  131. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  132. });
  133. it('emits only a transfer event', async function () {
  134. expectEvent.inLogs(logs, 'Transfer', {
  135. from: owner,
  136. to: owner,
  137. tokenId: tokenId,
  138. });
  139. });
  140. it('keeps the owner balance', async function () {
  141. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal('2');
  142. });
  143. it('keeps same tokens by index', async function () {
  144. if (!this.token.tokenOfOwnerByIndex) return;
  145. const tokensListed = await Promise.all(
  146. [0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i))
  147. );
  148. expect(tokensListed.map(t => t.toNumber())).to.have.members(
  149. [firstTokenId.toNumber(), secondTokenId.toNumber()]
  150. );
  151. });
  152. });
  153. context('when the address of the previous owner is incorrect', function () {
  154. it('reverts', async function () {
  155. await expectRevert(
  156. transferFunction.call(this, other, other, tokenId, { from: owner }),
  157. 'ERC721: transfer of token that is not own'
  158. );
  159. });
  160. });
  161. context('when the sender is not authorized for the token id', function () {
  162. it('reverts', async function () {
  163. await expectRevert(
  164. transferFunction.call(this, owner, other, tokenId, { from: other }),
  165. 'ERC721: transfer caller is not owner nor approved'
  166. );
  167. });
  168. });
  169. context('when the given token ID does not exist', function () {
  170. it('reverts', async function () {
  171. await expectRevert(
  172. transferFunction.call(this, owner, other, unknownTokenId, { from: owner }),
  173. 'ERC721: operator query for nonexistent token'
  174. );
  175. });
  176. });
  177. context('when the address to transfer the token to is the zero address', function () {
  178. it('reverts', async function () {
  179. await expectRevert(
  180. transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }),
  181. 'ERC721: transfer to the zero address'
  182. );
  183. });
  184. });
  185. };
  186. describe('via transferFrom', function () {
  187. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  188. return this.token.transferFrom(from, to, tokenId, opts);
  189. });
  190. });
  191. describe('via safeTransferFrom', function () {
  192. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  193. return this.token.methods['safeTransferFrom(address,address,uint256,bytes)'](from, to, tokenId, data, opts);
  194. };
  195. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  196. return this.token.methods['safeTransferFrom(address,address,uint256)'](from, to, tokenId, opts);
  197. };
  198. const shouldTransferSafely = function (transferFun, data) {
  199. describe('to a user account', function () {
  200. shouldTransferTokensByUsers(transferFun);
  201. });
  202. describe('to a valid receiver contract', function () {
  203. beforeEach(async function () {
  204. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  205. this.toWhom = this.receiver.address;
  206. });
  207. shouldTransferTokensByUsers(transferFun);
  208. it('should call onERC721Received', async function () {
  209. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  210. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  211. operator: owner,
  212. from: owner,
  213. tokenId: tokenId,
  214. data: data,
  215. });
  216. });
  217. it('should call onERC721Received from approved', async function () {
  218. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
  219. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  220. operator: approved,
  221. from: owner,
  222. tokenId: tokenId,
  223. data: data,
  224. });
  225. });
  226. describe('with an invalid token id', function () {
  227. it('reverts', async function () {
  228. await expectRevert(
  229. transferFun.call(
  230. this,
  231. owner,
  232. this.receiver.address,
  233. unknownTokenId,
  234. { from: owner },
  235. ),
  236. 'ERC721: operator query for nonexistent token'
  237. );
  238. });
  239. });
  240. });
  241. };
  242. describe('with data', function () {
  243. shouldTransferSafely(safeTransferFromWithData, data);
  244. });
  245. describe('without data', function () {
  246. shouldTransferSafely(safeTransferFromWithoutData, null);
  247. });
  248. describe('to a receiver contract returning unexpected value', function () {
  249. it('reverts', async function () {
  250. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  251. await expectRevert(
  252. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }),
  253. 'ERC721: transfer to non ERC721Receiver implementer'
  254. );
  255. });
  256. });
  257. describe('to a receiver contract that throws', function () {
  258. it('reverts', async function () {
  259. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  260. await expectRevert(
  261. this.token.safeTransferFrom(owner, revertingReceiver.address, tokenId, { from: owner }),
  262. 'ERC721ReceiverMock: reverting'
  263. );
  264. });
  265. });
  266. describe('to a contract that does not implement the required function', function () {
  267. it('reverts', async function () {
  268. const nonReceiver = this.token;
  269. await expectRevert(
  270. this.token.safeTransferFrom(owner, nonReceiver.address, tokenId, { from: owner }),
  271. 'ERC721: transfer to non ERC721Receiver implementer'
  272. );
  273. });
  274. });
  275. });
  276. });
  277. describe('safe mint', function () {
  278. const fourthTokenId = new BN(4);
  279. const tokenId = fourthTokenId;
  280. const data = '0x42';
  281. beforeEach(async function () {
  282. this.ERC721Mock = await ERC721Mock.new();
  283. });
  284. describe('via safeMint', function () { // regular minting is tested in ERC721Mintable.test.js and others
  285. it('should call onERC721Received — with data', async function () {
  286. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  287. const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId, data);
  288. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  289. from: ZERO_ADDRESS,
  290. tokenId: tokenId,
  291. data: data,
  292. });
  293. });
  294. it('should call onERC721Received — without data', async function () {
  295. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  296. const receipt = await this.ERC721Mock.safeMint(this.receiver.address, tokenId);
  297. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  298. from: ZERO_ADDRESS,
  299. tokenId: tokenId,
  300. });
  301. });
  302. context('to a receiver contract returning unexpected value', function () {
  303. it('reverts', async function () {
  304. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  305. await expectRevert(
  306. this.ERC721Mock.safeMint(invalidReceiver.address, tokenId),
  307. 'ERC721: transfer to non ERC721Receiver implementer'
  308. );
  309. });
  310. });
  311. context('to a receiver contract that throws', function () {
  312. it('reverts', async function () {
  313. const revertingReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  314. await expectRevert(
  315. this.ERC721Mock.safeMint(revertingReceiver.address, tokenId),
  316. 'ERC721ReceiverMock: reverting'
  317. );
  318. });
  319. });
  320. context('to a contract that does not implement the required function', function () {
  321. it('reverts', async function () {
  322. const nonReceiver = this.token;
  323. await expectRevert(
  324. this.ERC721Mock.safeMint(nonReceiver.address, tokenId),
  325. 'ERC721: transfer to non ERC721Receiver implementer'
  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),
  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. };