ERC721.behavior.js 19 KB

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