ERC721.behavior.js 19 KB

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