ERC721.behavior.js 19 KB

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