ERC721.behavior.js 19 KB

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