ERC721Basic.behavior.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 _ = require('lodash');
  6. const ERC721Receiver = artifacts.require('ERC721ReceiverMock.sol');
  7. const BigNumber = web3.BigNumber;
  8. require('chai')
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. function shouldBehaveLikeERC721Basic (
  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 ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  20. const RECEIVER_MAGIC_VALUE = '0x150b7a02';
  21. describe('like an ERC721Basic', 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. logs.length.should.be.equal(1);
  76. logs[0].event.should.be.equal('Transfer');
  77. logs[0].args.from.should.be.equal(owner);
  78. logs[0].args.to.should.be.equal(this.toWhom);
  79. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  80. });
  81. } else {
  82. it('emits only a transfer event', async function () {
  83. logs.length.should.be.equal(1);
  84. logs[0].event.should.be.equal('Transfer');
  85. logs[0].args.from.should.be.equal(owner);
  86. logs[0].args.to.should.be.equal(this.toWhom);
  87. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  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. logs.length.should.be.equal(1);
  137. logs[0].event.should.be.equal('Transfer');
  138. logs[0].args.from.should.be.equal(owner);
  139. logs[0].args.to.should.be.equal(owner);
  140. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  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(_.range(2).map(i => this.token.tokenOfOwnerByIndex(owner, i)));
  148. tokensListed.map(t => t.toNumber()).should.have.members([firstTokenId, secondTokenId]);
  149. });
  150. });
  151. context('when the address of the previous owner is incorrect', function () {
  152. it('reverts', async function () {
  153. await assertRevert(transferFunction.call(this, anyone, anyone, tokenId, { from: owner })
  154. );
  155. });
  156. });
  157. context('when the sender is not authorized for the token id', function () {
  158. it('reverts', async function () {
  159. await assertRevert(transferFunction.call(this, owner, anyone, tokenId, { from: anyone })
  160. );
  161. });
  162. });
  163. context('when the given token ID does not exist', function () {
  164. it('reverts', async function () {
  165. await assertRevert(transferFunction.call(this, owner, anyone, unknownTokenId, { from: owner })
  166. );
  167. });
  168. });
  169. context('when the address to transfer the token to is the zero address', function () {
  170. it('reverts', async function () {
  171. await assertRevert(transferFunction.call(this, owner, ZERO_ADDRESS, tokenId, { from: owner }));
  172. });
  173. });
  174. };
  175. describe('via transferFrom', function () {
  176. shouldTransferTokensByUsers(function (from, to, tokenId, opts) {
  177. return this.token.transferFrom(from, to, tokenId, opts);
  178. });
  179. });
  180. describe('via safeTransferFrom', function () {
  181. const safeTransferFromWithData = function (from, to, tokenId, opts) {
  182. return sendTransaction(
  183. this.token,
  184. 'safeTransferFrom',
  185. 'address,address,uint256,bytes',
  186. [from, to, tokenId, data],
  187. opts
  188. );
  189. };
  190. const safeTransferFromWithoutData = function (from, to, tokenId, opts) {
  191. return this.token.safeTransferFrom(from, to, tokenId, opts);
  192. };
  193. const shouldTransferSafely = function (transferFun, data) {
  194. describe('to a user account', function () {
  195. shouldTransferTokensByUsers(transferFun);
  196. });
  197. describe('to a valid receiver contract', function () {
  198. beforeEach(async function () {
  199. this.receiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, false);
  200. this.toWhom = this.receiver.address;
  201. });
  202. shouldTransferTokensByUsers(transferFun);
  203. it('should call onERC721Received', async function () {
  204. const result = await transferFun.call(this, owner, this.receiver.address, tokenId, { from: owner });
  205. result.receipt.logs.length.should.be.equal(2);
  206. const [log] = decodeLogs([result.receipt.logs[1]], ERC721Receiver, this.receiver.address);
  207. log.event.should.be.equal('Received');
  208. log.args.operator.should.be.equal(owner);
  209. log.args.from.should.be.equal(owner);
  210. log.args.tokenId.toNumber().should.be.equal(tokenId);
  211. log.args.data.should.be.equal(data);
  212. });
  213. it('should call onERC721Received from approved', async function () {
  214. const result = await transferFun.call(this, owner, this.receiver.address, tokenId, {
  215. from: approved,
  216. });
  217. result.receipt.logs.length.should.be.equal(2);
  218. const [log] = decodeLogs(
  219. [result.receipt.logs[1]],
  220. ERC721Receiver,
  221. this.receiver.address
  222. );
  223. log.event.should.be.equal('Received');
  224. log.args.operator.should.be.equal(approved);
  225. log.args.from.should.be.equal(owner);
  226. log.args.tokenId.toNumber().should.be.equal(tokenId);
  227. log.args.data.should.be.equal(data);
  228. });
  229. describe('with an invalid token id', function () {
  230. it('reverts', async function () {
  231. await assertRevert(
  232. transferFun.call(
  233. this,
  234. owner,
  235. this.receiver.address,
  236. unknownTokenId,
  237. { from: owner },
  238. )
  239. );
  240. });
  241. });
  242. });
  243. };
  244. describe('with data', function () {
  245. shouldTransferSafely(safeTransferFromWithData, data);
  246. });
  247. describe('without data', function () {
  248. shouldTransferSafely(safeTransferFromWithoutData, '0x');
  249. });
  250. describe('to a receiver contract returning unexpected value', function () {
  251. it('reverts', async function () {
  252. const invalidReceiver = await ERC721Receiver.new('0x42', false);
  253. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  254. });
  255. });
  256. describe('to a receiver contract that throws', function () {
  257. it('reverts', async function () {
  258. const invalidReceiver = await ERC721Receiver.new(RECEIVER_MAGIC_VALUE, true);
  259. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  260. });
  261. });
  262. describe('to a contract that does not implement the required function', function () {
  263. it('reverts', async function () {
  264. const invalidReceiver = this.token;
  265. await assertRevert(this.token.safeTransferFrom(owner, invalidReceiver.address, tokenId, { from: owner }));
  266. });
  267. });
  268. });
  269. });
  270. describe('approve', function () {
  271. const tokenId = firstTokenId;
  272. let logs = null;
  273. const itClearsApproval = function () {
  274. it('clears approval for the token', async function () {
  275. (await this.token.getApproved(tokenId)).should.be.equal(ZERO_ADDRESS);
  276. });
  277. };
  278. const itApproves = function (address) {
  279. it('sets the approval for the target address', async function () {
  280. (await this.token.getApproved(tokenId)).should.be.equal(address);
  281. });
  282. };
  283. const itEmitsApprovalEvent = function (address) {
  284. it('emits an approval event', async function () {
  285. logs.length.should.be.equal(1);
  286. logs[0].event.should.be.equal('Approval');
  287. logs[0].args.owner.should.be.equal(owner);
  288. logs[0].args.approved.should.be.equal(address);
  289. logs[0].args.tokenId.should.be.bignumber.equal(tokenId);
  290. });
  291. };
  292. context('when clearing approval', function () {
  293. context('when there was no prior approval', function () {
  294. beforeEach(async function () {
  295. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  296. });
  297. itClearsApproval();
  298. itEmitsApprovalEvent(ZERO_ADDRESS);
  299. });
  300. context('when there was a prior approval', function () {
  301. beforeEach(async function () {
  302. await this.token.approve(approved, tokenId, { from: owner });
  303. ({ logs } = await this.token.approve(ZERO_ADDRESS, tokenId, { from: owner }));
  304. });
  305. itClearsApproval();
  306. itEmitsApprovalEvent(ZERO_ADDRESS);
  307. });
  308. });
  309. context('when approving a non-zero address', function () {
  310. context('when there was no prior approval', function () {
  311. beforeEach(async function () {
  312. ({ logs } = await this.token.approve(approved, tokenId, { from: owner }));
  313. });
  314. itApproves(approved);
  315. itEmitsApprovalEvent(approved);
  316. });
  317. context('when there was a prior approval to the same address', function () {
  318. beforeEach(async function () {
  319. await this.token.approve(approved, tokenId, { from: owner });
  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 a different address', function () {
  326. beforeEach(async function () {
  327. await this.token.approve(anotherApproved, tokenId, { from: owner });
  328. ({ logs } = await this.token.approve(anotherApproved, tokenId, { from: owner }));
  329. });
  330. itApproves(anotherApproved);
  331. itEmitsApprovalEvent(anotherApproved);
  332. });
  333. });
  334. context('when the address that receives the approval is the owner', function () {
  335. it('reverts', async function () {
  336. await assertRevert(
  337. this.token.approve(owner, tokenId, { from: owner })
  338. );
  339. });
  340. });
  341. context('when the sender does not own the given token ID', function () {
  342. it('reverts', async function () {
  343. await assertRevert(this.token.approve(approved, tokenId, { from: anyone }));
  344. });
  345. });
  346. context('when the sender is approved for the given token ID', function () {
  347. it('reverts', async function () {
  348. await this.token.approve(approved, tokenId, { from: owner });
  349. await assertRevert(this.token.approve(anotherApproved, tokenId, { from: approved }));
  350. });
  351. });
  352. context('when the sender is an operator', function () {
  353. beforeEach(async function () {
  354. await this.token.setApprovalForAll(operator, true, { from: owner });
  355. ({ logs } = await this.token.approve(approved, tokenId, { from: operator }));
  356. });
  357. itApproves(approved);
  358. itEmitsApprovalEvent(approved);
  359. });
  360. context('when the given token ID does not exist', function () {
  361. it('reverts', async function () {
  362. await assertRevert(this.token.approve(approved, unknownTokenId, { from: operator }));
  363. });
  364. });
  365. });
  366. describe('setApprovalForAll', function () {
  367. context('when the operator willing to approve is not the owner', function () {
  368. context('when there is no operator approval set by the sender', function () {
  369. it('approves the operator', async function () {
  370. await this.token.setApprovalForAll(operator, true, { from: owner });
  371. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  372. });
  373. it('emits an approval event', async function () {
  374. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  375. logs.length.should.be.equal(1);
  376. logs[0].event.should.be.equal('ApprovalForAll');
  377. logs[0].args.owner.should.be.equal(owner);
  378. logs[0].args.operator.should.be.equal(operator);
  379. logs[0].args.approved.should.equal(true);
  380. });
  381. });
  382. context('when the operator was set as not approved', function () {
  383. beforeEach(async function () {
  384. await this.token.setApprovalForAll(operator, false, { from: owner });
  385. });
  386. it('approves the operator', async function () {
  387. await this.token.setApprovalForAll(operator, true, { from: owner });
  388. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  389. });
  390. it('emits an approval event', async function () {
  391. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  392. logs.length.should.be.equal(1);
  393. logs[0].event.should.be.equal('ApprovalForAll');
  394. logs[0].args.owner.should.be.equal(owner);
  395. logs[0].args.operator.should.be.equal(operator);
  396. logs[0].args.approved.should.equal(true);
  397. });
  398. it('can unset the operator approval', async function () {
  399. await this.token.setApprovalForAll(operator, false, { from: owner });
  400. (await this.token.isApprovedForAll(owner, operator)).should.equal(false);
  401. });
  402. });
  403. context('when the operator was already approved', function () {
  404. beforeEach(async function () {
  405. await this.token.setApprovalForAll(operator, true, { from: owner });
  406. });
  407. it('keeps the approval to the given address', async function () {
  408. await this.token.setApprovalForAll(operator, true, { from: owner });
  409. (await this.token.isApprovedForAll(owner, operator)).should.equal(true);
  410. });
  411. it('emits an approval event', async function () {
  412. const { logs } = await this.token.setApprovalForAll(operator, true, { from: owner });
  413. logs.length.should.be.equal(1);
  414. logs[0].event.should.be.equal('ApprovalForAll');
  415. logs[0].args.owner.should.be.equal(owner);
  416. logs[0].args.operator.should.be.equal(operator);
  417. logs[0].args.approved.should.equal(true);
  418. });
  419. });
  420. });
  421. context('when the operator is the owner', function () {
  422. it('reverts', async function () {
  423. await assertRevert(this.token.setApprovalForAll(owner, true, { from: owner }));
  424. });
  425. });
  426. });
  427. shouldSupportInterfaces([
  428. 'ERC165',
  429. 'ERC721',
  430. ]);
  431. });
  432. }
  433. module.exports = {
  434. shouldBehaveLikeERC721Basic,
  435. };