ERC721.behavior.js 23 KB

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