ERC721.behavior.js 20 KB

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