ERC721.behavior.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. const expectEvent = require('../../helpers/expectEvent');
  2. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  3. const shouldFail = require('../../helpers/shouldFail');
  4. const { ZERO_ADDRESS } = require('../../helpers/constants');
  5. const send = require('../../helpers/send');
  6. const ERC721ReceiverMock = artifacts.require('ERC721ReceiverMock.sol');
  7. require('../../helpers/setup');
  8. function shouldBehaveLikeERC721 (
  9. creator,
  10. minter,
  11. [owner, approved, anotherApproved, operator, anyone]
  12. ) {
  13. const firstTokenId = 1;
  14. const secondTokenId = 2;
  15. const unknownTokenId = 3;
  16. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  17. describe('like an ERC721', function () {
  18. beforeEach(async function () {
  19. await this.token.mint(owner, firstTokenId, { from: minter });
  20. await this.token.mint(owner, secondTokenId, { from: minter });
  21. this.toWhom = anyone; // default to anyone for toWhom in context-dependent tests
  22. });
  23. describe('balanceOf', function () {
  24. context('when the given address owns some tokens', function () {
  25. it('returns the amount of tokens owned by the given address', async function () {
  26. (await this.token.balanceOf(owner)).should.be.bignumber.equal(2);
  27. });
  28. });
  29. context('when the given address does not own any tokens', function () {
  30. it('returns 0', async function () {
  31. (await this.token.balanceOf(anyone)).should.be.bignumber.equal(0);
  32. });
  33. });
  34. context('when querying the zero address', function () {
  35. it('throws', async function () {
  36. await shouldFail.reverting(this.token.balanceOf(0));
  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. (await this.token.ownerOf(tokenId)).should.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 shouldFail.reverting(this.token.ownerOf(tokenId));
  51. });
  52. });
  53. });
  54. describe('transfers', function () {
  55. const tokenId = firstTokenId;
  56. const data = '0x42';
  57. let logs = null;
  58. beforeEach(async function () {
  59. await this.token.approve(approved, tokenId, { from: owner });
  60. await this.token.setApprovalForAll(operator, true, { from: owner });
  61. });
  62. const transferWasSuccessful = function ({ owner, tokenId, approved }) {
  63. it('transfers the ownership of the given token ID to the given address', async function () {
  64. (await this.token.ownerOf(tokenId)).should.be.equal(this.toWhom);
  65. });
  66. it('clears the approval for the token ID', async function () {
  67. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  68. });
  69. if (approved) {
  70. it('emit only a transfer event', async function () {
  71. expectEvent.inLogs(logs, 'Transfer', {
  72. from: owner,
  73. to: this.toWhom,
  74. tokenId: tokenId,
  75. });
  76. });
  77. } else {
  78. it('emits only a transfer event', async function () {
  79. expectEvent.inLogs(logs, 'Transfer', {
  80. from: owner,
  81. to: this.toWhom,
  82. tokenId: tokenId,
  83. });
  84. });
  85. }
  86. it('adjusts owners balances', async function () {
  87. (await this.token.balanceOf(owner)).should.be.bignumber.equal(1);
  88. });
  89. it('adjusts owners tokens by index', async function () {
  90. if (!this.token.tokenOfOwnerByIndex) return;
  91. (await this.token.tokenOfOwnerByIndex(this.toWhom, 0)).toNumber().should.be.equal(tokenId);
  92. (await this.token.tokenOfOwnerByIndex(owner, 0)).toNumber().should.not.be.equal(tokenId);
  93. });
  94. };
  95. const shouldTransferTokensByUsers = function (transferFunction) {
  96. context('when called by the owner', function () {
  97. beforeEach(async function () {
  98. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: owner }));
  99. });
  100. transferWasSuccessful({ owner, tokenId, approved });
  101. });
  102. context('when called by the approved individual', function () {
  103. beforeEach(async function () {
  104. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: approved }));
  105. });
  106. transferWasSuccessful({ owner, tokenId, approved });
  107. });
  108. context('when called by the operator', function () {
  109. beforeEach(async function () {
  110. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  111. });
  112. transferWasSuccessful({ owner, tokenId, approved });
  113. });
  114. context('when called by the owner without an approved user', function () {
  115. beforeEach(async function () {
  116. await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner });
  117. ({ logs } = await transferFunction.call(this, owner, this.toWhom, tokenId, { from: operator }));
  118. });
  119. transferWasSuccessful({ owner, tokenId, approved: null });
  120. });
  121. context('when sent to the owner', function () {
  122. beforeEach(async function () {
  123. ({ logs } = await transferFunction.call(this, owner, owner, tokenId, { from: owner }));
  124. });
  125. it('keeps ownership of the token', async function () {
  126. (await this.token.ownerOf(tokenId)).should.be.equal(owner);
  127. });
  128. it('clears the approval for the token ID', async function () {
  129. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  130. });
  131. it('emits only a transfer event', async function () {
  132. expectEvent.inLogs(logs, 'Transfer', {
  133. from: owner,
  134. to: owner,
  135. tokenId: tokenId,
  136. });
  137. });
  138. it('keeps the owner balance', async function () {
  139. (await this.token.balanceOf(owner)).should.be.bignumber.equal(2);
  140. });
  141. it('keeps same tokens by index', async function () {
  142. if (!this.token.tokenOfOwnerByIndex) return;
  143. const tokensListed = await Promise.all(
  144. [0, 1].map(i => this.token.tokenOfOwnerByIndex(owner, i))
  145. );
  146. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  147. });
  148. });
  149. context('when the address of the previous owner is incorrect', function () {
  150. it('reverts', async function () {
  151. await shouldFail.reverting(transferFunction.call(this, anyone, anyone, tokenId, { from: owner })
  152. );
  153. });
  154. });
  155. context('when the sender is not authorized for the token id', function () {
  156. it('reverts', async function () {
  157. await shouldFail.reverting(transferFunction.call(this, owner, anyone, tokenId, { from: anyone })
  158. );
  159. });
  160. });
  161. context('when the given token ID does not exist', function () {
  162. it('reverts', async function () {
  163. await shouldFail.reverting(transferFunction.call(this, owner, anyone, unknownTokenId, { from: owner })
  164. );
  165. });
  166. });
  167. context('when the address to transfer the token to is the zero address', function () {
  168. it('reverts', async function () {
  169. await shouldFail.reverting(transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }));
  170. });
  171. });
  172. };
  173. describe('via transferFrom', function () {
  174. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  175. return this.token.transferFrom(from, to, tokenId, opts);
  176. });
  177. });
  178. describe('via safeTransferFrom', function () {
  179. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  180. return send.transaction(
  181. this.token,
  182. 'safeTransferFrom',
  183. 'address,address,uint256,bytes',
  184. [from, to, tokenId, data],
  185. opts
  186. );
  187. };
  188. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  189. return this.token.safeTransferFrom(from, to, tokenId, opts);
  190. };
  191. const shouldTransferSafely = function (transferFun, data) {
  192. describe('to a user account', function () {
  193. shouldTransferTokensByUsers(transferFun);
  194. });
  195. describe('to a valid receiver contract', function () {
  196. beforeEach(async function () {
  197. this.receiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, false);
  198. this.toWhom = this.receiver.address;
  199. });
  200. shouldTransferTokensByUsers(transferFun);
  201. it('should call onERC721Received', async function () {
  202. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  203. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  204. operator: owner,
  205. from: owner,
  206. tokenId: tokenId,
  207. data: data,
  208. });
  209. });
  210. it('should call onERC721Received from approved', async function () {
  211. const receipt = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: approved });
  212. await expectEvent.inTransaction(receipt.tx, ERC721ReceiverMock, 'Received', {
  213. operator: approved,
  214. from: owner,
  215. tokenId: tokenId,
  216. data: data,
  217. });
  218. });
  219. describe('with an invalid token id', function () {
  220. it('reverts', async function () {
  221. await shouldFail.reverting(
  222. transferFun.call(
  223. this,
  224. owner,
  225. this.receiver.address,
  226. unknownTokenId,
  227. { from: owner },
  228. )
  229. );
  230. });
  231. });
  232. });
  233. };
  234. describe('with data', function () {
  235. shouldTransferSafely(safeTransferFromWithData, data);
  236. });
  237. describe('without data', function () {
  238. shouldTransferSafely(safeTransferFromWithoutData, '0x');
  239. });
  240. describe('to a receiver contract returning unexpected value', function () {
  241. it('reverts', async function () {
  242. const invalidReceiver = await ERC721ReceiverMock.new('0x42', false);
  243. await shouldFail.reverting(
  244. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner })
  245. );
  246. });
  247. });
  248. describe('to a receiver contract that throws', function () {
  249. it('reverts', async function () {
  250. const invalidReceiver = await ERC721ReceiverMock.new(RECEIVER_MAGIC_VALUE, true);
  251. await shouldFail.reverting(
  252. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner })
  253. );
  254. });
  255. });
  256. describe('to a contract that does not implement the required function', function () {
  257. it('reverts', async function () {
  258. const invalidReceiver = this.token;
  259. await shouldFail.reverting(
  260. this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner })
  261. );
  262. });
  263. });
  264. });
  265. });
  266. describe('approve', function () {
  267. const tokenId = firstTokenId;
  268. let logs = null;
  269. const itClearsApproval = function () {
  270. it('clears approval for the token', async function () {
  271. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  272. });
  273. };
  274. const itApproves = function (address) {
  275. it('sets the approval for the target address', async function () {
  276. (await this.token.getApproved(tokenId)).should.be.equal(address);
  277. });
  278. };
  279. const itEmitsApprovalEvent = function (address) {
  280. it('emits an approval event', async function () {
  281. expectEvent.inLogs(logs, 'Approval', {
  282. owner: owner,
  283. approved: address,
  284. tokenId: tokenId,
  285. });
  286. });
  287. };
  288. context('when clearing approval', function () {
  289. context('when there was no prior approval', function () {
  290. beforeEach(async function () {
  291. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  292. });
  293. itClearsApproval();
  294. itEmitsApprovalEvent(ZERO_ADDRESS);
  295. });
  296. context('when there was a prior approval', function () {
  297. beforeEach(async function () {
  298. await this.token.approve(approved, tokenId, { from: owner });
  299. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  300. });
  301. itClearsApproval();
  302. itEmitsApprovalEvent(ZERO_ADDRESS);
  303. });
  304. });
  305. context('when approving a non-zero address', function () {
  306. context('when there was no prior approval', function () {
  307. beforeEach(async function () {
  308. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  309. });
  310. itApproves(approved);
  311. itEmitsApprovalEvent(approved);
  312. });
  313. context('when there was a prior approval to the same address', function () {
  314. beforeEach(async function () {
  315. await this.token.approve(approved, tokenId, { from: owner });
  316. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  317. });
  318. itApproves(approved);
  319. itEmitsApprovalEvent(approved);
  320. });
  321. context('when there was a prior approval to a different address', function () {
  322. beforeEach(async function () {
  323. await this.token.approve(anotherApproved, tokenId, { from: owner });
  324. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  325. });
  326. itApproves(anotherApproved);
  327. itEmitsApprovalEvent(anotherApproved);
  328. });
  329. });
  330. context('when the address that receives the approval is the owner', function () {
  331. it('reverts', async function () {
  332. await shouldFail.reverting(
  333. this.token.approve(owner, tokenId, { from: owner })
  334. );
  335. });
  336. });
  337. context('when the sender does not own the given token ID', function () {
  338. it('reverts', async function () {
  339. await shouldFail.reverting(this.token.approve(approved, tokenId, { from: anyone }));
  340. });
  341. });
  342. context('when the sender is approved for the given token ID', function () {
  343. it('reverts', async function () {
  344. await this.token.approve(approved, tokenId, { from: owner });
  345. await shouldFail.reverting(this.token.approve(anotherApproved, tokenId, { from: approved }));
  346. });
  347. });
  348. context('when the sender is an operator', function () {
  349. beforeEach(async function () {
  350. await this.token.setApprovalForAll(operator, true, { from: owner });
  351. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  352. });
  353. itApproves(approved);
  354. itEmitsApprovalEvent(approved);
  355. });
  356. context('when the given token ID does not exist', function () {
  357. it('reverts', async function () {
  358. await shouldFail.reverting(this.token.approve(approved, unknownTokenId, { from: operator }));
  359. });
  360. });
  361. });
  362. describe('setApprovalForAll', function () {
  363. context('when the operator willing to approve is not the owner', function () {
  364. context('when there is no operator approval set by the sender', function () {
  365. it('approves the operator', async function () {
  366. await this.token.setApprovalForAll(operator, true, { from: owner });
  367. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  368. });
  369. it('emits an approval event', async function () {
  370. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  371. expectEvent.inLogs(logs, 'ApprovalForAll', {
  372. owner: owner,
  373. operator: operator,
  374. approved: true,
  375. });
  376. });
  377. });
  378. context('when the operator was set as not approved', function () {
  379. beforeEach(async function () {
  380. await this.token.setApprovalForAll(operator, false, { from: owner });
  381. });
  382. it('approves the operator', async function () {
  383. await this.token.setApprovalForAll(operator, true, { from: owner });
  384. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  385. });
  386. it('emits an approval event', async function () {
  387. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  388. expectEvent.inLogs(logs, 'ApprovalForAll', {
  389. owner: owner,
  390. operator: operator,
  391. approved: true,
  392. });
  393. });
  394. it('can unset the operator approval', async function () {
  395. await this.token.setApprovalForAll(operator, false, { from: owner });
  396. (await this.token.isApprovedForAll(owner, operator)).should.equal(false);
  397. });
  398. });
  399. context('when the operator was already approved', function () {
  400. beforeEach(async function () {
  401. await this.token.setApprovalForAll(operator, true, { from: owner });
  402. });
  403. it('keeps the approval to the given address', async function () {
  404. await this.token.setApprovalForAll(operator, true, { from: owner });
  405. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  406. });
  407. it('emits an approval event', async function () {
  408. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  409. expectEvent.inLogs(logs, 'ApprovalForAll', {
  410. owner: owner,
  411. operator: operator,
  412. approved: true,
  413. });
  414. });
  415. });
  416. });
  417. context('when the operator is the owner', function () {
  418. it('reverts', async function () {
  419. await shouldFail.reverting(this.token.setApprovalForAll(owner, true, { from: owner }));
  420. });
  421. });
  422. });
  423. shouldSupportInterfaces([
  424. 'ERC165',
  425. 'ERC721',
  426. ]);
  427. });
  428. }
  429. module.exports = {
  430. shouldBehaveLikeERC721,
  431. };