ERC721.behavior.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  2. const { assertRevert } = require('../../helpers/assertRevert');
  3. const { decodeLogs } = require('../../helpers/decodeLogs');
  4. const { sendTransaction } = require('../../helpers/sendTransaction');
  5. const ERC721Receiver = artifacts.require('ERC721ReceiverMock.sol');
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. function shouldBehaveLikeERC721 (
  11. creator,
  12. minter,
  13. [owner, approved, anotherApproved, operator, anyone]
  14. ) {
  15. const firstTokenId = 1;
  16. const secondTokenId = 2;
  17. const unknownTokenId = 3;
  18. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  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 assertRevert(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 assertRevert(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. logs.length.should.be.equal(1);
  75. logs[0].event.should.be.equal('Transfer');
  76. logs[0].args.from.should.be.equal(owner);
  77. logs[0].args.to.should.be.equal(this.toWhom);
  78. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  79. });
  80. } else {
  81. it('emits only a transfer event', async function () {
  82. logs.length.should.be.equal(1);
  83. logs[0].event.should.be.equal('Transfer');
  84. logs[0].args.from.should.be.equal(owner);
  85. logs[0].args.to.should.be.equal(this.toWhom);
  86. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  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. logs.length.should.be.equal(1);
  136. logs[0].event.should.be.equal('Transfer');
  137. logs[0].args.from.should.be.equal(owner);
  138. logs[0].args.to.should.be.equal(owner);
  139. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  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 assertRevert(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 assertRevert(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 assertRevert(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 assertRevert(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 ERC721Receiver.new(RECEIVER_MAGIC_VALUE, false);
  201. this.toWhom = this.receiver.address;
  202. });
  203. shouldTransferTokensByUsers(transferFun);
  204. it('should call onERC721Received', async function () {
  205. const result = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  206. result.receipt.logs.length.should.be.equal(2);
  207. const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address);
  208. log.event.should.be.equal('Received');
  209. log.args.operator.should.be.equal(owner);
  210. log.args.from.should.be.equal(owner);
  211. log.args.tokenId.toNumber().should.be.equal(tokenId);
  212. log.args.data.should.be.equal(data);
  213. });
  214. it('should call onERC721Received from approved', async function () {
  215. const result = await transferFun.call(this, owner, this.receiver.address, tokenId, {
  216. from: approved,
  217. });
  218. result.receipt.logs.length.should.be.equal(2);
  219. const [log] = decodeLogs(
  220. [result.receipt.logs[1]],
  221. ERC721Receiver,
  222. this.receiver.address
  223. );
  224. log.event.should.be.equal('Received');
  225. log.args.operator.should.be.equal(approved);
  226. log.args.from.should.be.equal(owner);
  227. log.args.tokenId.toNumber().should.be.equal(tokenId);
  228. log.args.data.should.be.equal(data);
  229. });
  230. describe('with an invalid token id', function () {
  231. it('reverts', async function () {
  232. await assertRevert(
  233. transferFun.call(
  234. this,
  235. owner,
  236. this.receiver.address,
  237. unknownTokenId,
  238. { from: owner },
  239. )
  240. );
  241. });
  242. });
  243. });
  244. };
  245. describe('with data', function () {
  246. shouldTransferSafely(safeTransferFromWithData, data);
  247. });
  248. describe('without data', function () {
  249. shouldTransferSafely(safeTransferFromWithoutData, '0x');
  250. });
  251. describe('to a receiver contract returning unexpected value', function () {
  252. it('reverts', async function () {
  253. const invalidReceiver = await ERC721Receiver.new('0x42', false);
  254. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  255. });
  256. });
  257. describe('to a receiver contract that throws', function () {
  258. it('reverts', async function () {
  259. const invalidReceiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, true);
  260. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  261. });
  262. });
  263. describe('to a contract that does not implement the required function', function () {
  264. it('reverts', async function () {
  265. const invalidReceiver = this.token;
  266. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  267. });
  268. });
  269. });
  270. });
  271. describe('approve', function () {
  272. const tokenId = firstTokenId;
  273. let logs = null;
  274. const itClearsApproval = function () {
  275. it('clears approval for the token', async function () {
  276. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  277. });
  278. };
  279. const itApproves = function (address) {
  280. it('sets the approval for the target address', async function () {
  281. (await this.token.getApproved(tokenId)).should.be.equal(address);
  282. });
  283. };
  284. const itEmitsApprovalEvent = function (address) {
  285. it('emits an approval event', async function () {
  286. logs.length.should.be.equal(1);
  287. logs[0].event.should.be.equal('Approval');
  288. logs[0].args.owner.should.be.equal(owner);
  289. logs[0].args.approved.should.be.equal(address);
  290. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  291. });
  292. };
  293. context('when clearing approval', function () {
  294. context('when there was no prior approval', function () {
  295. beforeEach(async function () {
  296. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  297. });
  298. itClearsApproval();
  299. itEmitsApprovalEvent(ZERO_ADDRESS);
  300. });
  301. context('when there was a prior approval', function () {
  302. beforeEach(async function () {
  303. await this.token.approve(approved, tokenId, { from: owner });
  304. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  305. });
  306. itClearsApproval();
  307. itEmitsApprovalEvent(ZERO_ADDRESS);
  308. });
  309. });
  310. context('when approving a non-zero address', function () {
  311. context('when there was no prior approval', function () {
  312. beforeEach(async function () {
  313. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  314. });
  315. itApproves(approved);
  316. itEmitsApprovalEvent(approved);
  317. });
  318. context('when there was a prior approval to the same address', function () {
  319. beforeEach(async function () {
  320. await this.token.approve(approved, tokenId, { from: owner });
  321. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  322. });
  323. itApproves(approved);
  324. itEmitsApprovalEvent(approved);
  325. });
  326. context('when there was a prior approval to a different address', function () {
  327. beforeEach(async function () {
  328. await this.token.approve(anotherApproved, tokenId, { from: owner });
  329. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  330. });
  331. itApproves(anotherApproved);
  332. itEmitsApprovalEvent(anotherApproved);
  333. });
  334. });
  335. context('when the address that receives the approval is the owner', function () {
  336. it('reverts', async function () {
  337. await assertRevert(
  338. this.token.approve(owner, tokenId, { from: owner })
  339. );
  340. });
  341. });
  342. context('when the sender does not own the given token ID', function () {
  343. it('reverts', async function () {
  344. await assertRevert(this.token.approve(approved, tokenId, { from: anyone }));
  345. });
  346. });
  347. context('when the sender is approved for the given token ID', function () {
  348. it('reverts', async function () {
  349. await this.token.approve(approved, tokenId, { from: owner });
  350. await assertRevert(this.token.approve(anotherApproved, tokenId, { from: approved }));
  351. });
  352. });
  353. context('when the sender is an operator', function () {
  354. beforeEach(async function () {
  355. await this.token.setApprovalForAll(operator, true, { from: owner });
  356. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  357. });
  358. itApproves(approved);
  359. itEmitsApprovalEvent(approved);
  360. });
  361. context('when the given token ID does not exist', function () {
  362. it('reverts', async function () {
  363. await assertRevert(this.token.approve(approved, unknownTokenId, { from: operator }));
  364. });
  365. });
  366. });
  367. describe('setApprovalForAll', function () {
  368. context('when the operator willing to approve is not the owner', function () {
  369. context('when there is no operator approval set by the sender', function () {
  370. it('approves the operator', async function () {
  371. await this.token.setApprovalForAll(operator, true, { from: owner });
  372. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  373. });
  374. it('emits an approval event', async function () {
  375. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  376. logs.length.should.be.equal(1);
  377. logs[0].event.should.be.equal('ApprovalForAll');
  378. logs[0].args.owner.should.be.equal(owner);
  379. logs[0].args.operator.should.be.equal(operator);
  380. logs[0].args.approved.should.equal(true);
  381. });
  382. });
  383. context('when the operator was set as not approved', function () {
  384. beforeEach(async function () {
  385. await this.token.setApprovalForAll(operator, false, { from: owner });
  386. });
  387. it('approves the operator', async function () {
  388. await this.token.setApprovalForAll(operator, true, { from: owner });
  389. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  390. });
  391. it('emits an approval event', async function () {
  392. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  393. logs.length.should.be.equal(1);
  394. logs[0].event.should.be.equal('ApprovalForAll');
  395. logs[0].args.owner.should.be.equal(owner);
  396. logs[0].args.operator.should.be.equal(operator);
  397. logs[0].args.approved.should.equal(true);
  398. });
  399. it('can unset the operator approval', async function () {
  400. await this.token.setApprovalForAll(operator, false, { from: owner });
  401. (await this.token.isApprovedForAll(owner, operator)).should.equal(false);
  402. });
  403. });
  404. context('when the operator was already approved', function () {
  405. beforeEach(async function () {
  406. await this.token.setApprovalForAll(operator, true, { from: owner });
  407. });
  408. it('keeps the approval to the given address', async function () {
  409. await this.token.setApprovalForAll(operator, true, { from: owner });
  410. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  411. });
  412. it('emits an approval event', async function () {
  413. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  414. logs.length.should.be.equal(1);
  415. logs[0].event.should.be.equal('ApprovalForAll');
  416. logs[0].args.owner.should.be.equal(owner);
  417. logs[0].args.operator.should.be.equal(operator);
  418. logs[0].args.approved.should.equal(true);
  419. });
  420. });
  421. });
  422. context('when the operator is the owner', function () {
  423. it('reverts', async function () {
  424. await assertRevert(this.token.setApprovalForAll(owner, true, { from: owner }));
  425. });
  426. });
  427. });
  428. shouldSupportInterfaces([
  429. 'ERC165',
  430. 'ERC721',
  431. ]);
  432. });
  433. }
  434. module.exports = {
  435. shouldBehaveLikeERC721,
  436. };