ERC721.behavior.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. function shouldBehaveLikeERC721 (
  7. creator,
  8. minter,
  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, { from: minter });
  18. await this.token.mint(owner, secondTokenId, { from: minter });
  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 invalidReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  260. await expectRevert(
  261. this.token.safeTransferFrom(owner, invalidReceiver.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 invalidReceiver = this.token;
  269. await expectRevert.unspecified(
  270. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner })
  271. );
  272. });
  273. });
  274. });
  275. });
  276. describe('approve', function () {
  277. const tokenId = firstTokenId;
  278. let logs = null;
  279. const itClearsApproval = function () {
  280. it('clears approval for the token', async function () {
  281. expect(await this.token.getApproved(tokenId)).to.be.equal(ZERO_ADDRESS);
  282. });
  283. };
  284. const itApproves = function (address) {
  285. it('sets the approval for the target address', async function () {
  286. expect(await this.token.getApproved(tokenId)).to.be.equal(address);
  287. });
  288. };
  289. const itEmitsApprovalEvent = function (address) {
  290. it('emits an approval event', async function () {
  291. expectEvent.inLogs(logs, 'Approval', {
  292. owner: owner,
  293. approved: address,
  294. tokenId: tokenId,
  295. });
  296. });
  297. };
  298. context('when clearing approval', function () {
  299. context('when there was no prior approval', function () {
  300. beforeEach(async function () {
  301. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  302. });
  303. itClearsApproval();
  304. itEmitsApprovalEvent(ZERO_ADDRESS);
  305. });
  306. context('when there was a prior approval', function () {
  307. beforeEach(async function () {
  308. await this.token.approve(approved, tokenId, { from: owner });
  309. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  310. });
  311. itClearsApproval();
  312. itEmitsApprovalEvent(ZERO_ADDRESS);
  313. });
  314. });
  315. context('when approving a non-zero address', function () {
  316. context('when there was no prior approval', function () {
  317. beforeEach(async function () {
  318. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  319. });
  320. itApproves(approved);
  321. itEmitsApprovalEvent(approved);
  322. });
  323. context('when there was a prior approval to the same address', function () {
  324. beforeEach(async function () {
  325. await this.token.approve(approved, tokenId, { from: owner });
  326. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  327. });
  328. itApproves(approved);
  329. itEmitsApprovalEvent(approved);
  330. });
  331. context('when there was a prior approval to a different address', function () {
  332. beforeEach(async function () {
  333. await this.token.approve(anotherApproved, tokenId, { from: owner });
  334. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  335. });
  336. itApproves(anotherApproved);
  337. itEmitsApprovalEvent(anotherApproved);
  338. });
  339. });
  340. context('when the address that receives the approval is the owner', function () {
  341. it('reverts', async function () {
  342. await expectRevert(
  343. this.token.approve(owner, tokenId, { from: owner }), 'ERC721: approval to current owner'
  344. );
  345. });
  346. });
  347. context('when the sender does not own the given token ID', function () {
  348. it('reverts', async function () {
  349. await expectRevert(this.token.approve(approved, tokenId, { from: other }),
  350. 'ERC721: approve caller is not owner nor approved');
  351. });
  352. });
  353. context('when the sender is approved for the given token ID', function () {
  354. it('reverts', async function () {
  355. await this.token.approve(approved, tokenId, { from: owner });
  356. await expectRevert(this.token.approve(anotherApproved, tokenId, { from: approved }),
  357. 'ERC721: approve caller is not owner nor approved for all');
  358. });
  359. });
  360. context('when the sender is an operator', function () {
  361. beforeEach(async function () {
  362. await this.token.setApprovalForAll(operator, true, { from: owner });
  363. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  364. });
  365. itApproves(approved);
  366. itEmitsApprovalEvent(approved);
  367. });
  368. context('when the given token ID does not exist', function () {
  369. it('reverts', async function () {
  370. await expectRevert(this.token.approve(approved, unknownTokenId, { from: operator }),
  371. 'ERC721: owner query for nonexistent token');
  372. });
  373. });
  374. });
  375. describe('setApprovalForAll', function () {
  376. context('when the operator willing to approve is not the owner', function () {
  377. context('when there is no operator approval set by the sender', function () {
  378. it('approves the operator', async function () {
  379. await this.token.setApprovalForAll(operator, true, { from: owner });
  380. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  381. });
  382. it('emits an approval event', async function () {
  383. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  384. expectEvent.inLogs(logs, 'ApprovalForAll', {
  385. owner: owner,
  386. operator: operator,
  387. approved: true,
  388. });
  389. });
  390. });
  391. context('when the operator was set as not approved', function () {
  392. beforeEach(async function () {
  393. await this.token.setApprovalForAll(operator, false, { from: owner });
  394. });
  395. it('approves the operator', async function () {
  396. await this.token.setApprovalForAll(operator, true, { from: owner });
  397. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  398. });
  399. it('emits an approval event', async function () {
  400. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  401. expectEvent.inLogs(logs, 'ApprovalForAll', {
  402. owner: owner,
  403. operator: operator,
  404. approved: true,
  405. });
  406. });
  407. it('can unset the operator approval', async function () {
  408. await this.token.setApprovalForAll(operator, false, { from: owner });
  409. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(false);
  410. });
  411. });
  412. context('when the operator was already approved', function () {
  413. beforeEach(async function () {
  414. await this.token.setApprovalForAll(operator, true, { from: owner });
  415. });
  416. it('keeps the approval to the given address', async function () {
  417. await this.token.setApprovalForAll(operator, true, { from: owner });
  418. expect(await this.token.isApprovedForAll(owner, operator)).to.equal(true);
  419. });
  420. it('emits an approval event', async function () {
  421. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  422. expectEvent.inLogs(logs, 'ApprovalForAll', {
  423. owner: owner,
  424. operator: operator,
  425. approved: true,
  426. });
  427. });
  428. });
  429. });
  430. context('when the operator is the owner', function () {
  431. it('reverts', async function () {
  432. await expectRevert(this.token.setApprovalForAll(owner, true, { from: owner }),
  433. 'ERC721: approve to caller');
  434. });
  435. });
  436. });
  437. shouldSupportInterfaces([
  438. 'ERC165',
  439. 'ERC721',
  440. ]);
  441. });
  442. }
  443. module.exports = {
  444. shouldBehaveLikeERC721,
  445. };