ERC721.behavior.js 20 KB

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