ERC1155.behavior.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const { shouldSupportInterfaces } = require('../../introspection/SupportsInterface.behavior');
  5. const ERC1155ReceiverMock = artifacts.require('ERC1155ReceiverMock');
  6. function shouldBehaveLikeERC1155 ([minter, firstTokenHolder, secondTokenHolder, multiTokenHolder, recipient, proxy]) {
  7. const firstTokenId = new BN(1);
  8. const secondTokenId = new BN(2);
  9. const unknownTokenId = new BN(3);
  10. const firstAmount = new BN(1000);
  11. const secondAmount = new BN(2000);
  12. const RECEIVER_SINGLE_MAGIC_VALUE = '0xf23a6e61';
  13. const RECEIVER_BATCH_MAGIC_VALUE = '0xbc197c81';
  14. describe('like an ERC1155', function () {
  15. describe('balanceOf', function () {
  16. it('reverts when queried about the zero address', async function () {
  17. await expectRevert(
  18. this.token.balanceOf(ZERO_ADDRESS, firstTokenId),
  19. 'ERC1155: balance query for the zero address',
  20. );
  21. });
  22. context('when accounts don\'t own tokens', function () {
  23. it('returns zero for given addresses', async function () {
  24. expect(await this.token.balanceOf(
  25. firstTokenHolder,
  26. firstTokenId,
  27. )).to.be.bignumber.equal('0');
  28. expect(await this.token.balanceOf(
  29. secondTokenHolder,
  30. secondTokenId,
  31. )).to.be.bignumber.equal('0');
  32. expect(await this.token.balanceOf(
  33. firstTokenHolder,
  34. unknownTokenId,
  35. )).to.be.bignumber.equal('0');
  36. });
  37. });
  38. context('when accounts own some tokens', function () {
  39. beforeEach(async function () {
  40. await this.token.mint(firstTokenHolder, firstTokenId, firstAmount, '0x', {
  41. from: minter,
  42. });
  43. await this.token.mint(
  44. secondTokenHolder,
  45. secondTokenId,
  46. secondAmount,
  47. '0x',
  48. {
  49. from: minter,
  50. },
  51. );
  52. });
  53. it('returns the amount of tokens owned by the given addresses', async function () {
  54. expect(await this.token.balanceOf(
  55. firstTokenHolder,
  56. firstTokenId,
  57. )).to.be.bignumber.equal(firstAmount);
  58. expect(await this.token.balanceOf(
  59. secondTokenHolder,
  60. secondTokenId,
  61. )).to.be.bignumber.equal(secondAmount);
  62. expect(await this.token.balanceOf(
  63. firstTokenHolder,
  64. unknownTokenId,
  65. )).to.be.bignumber.equal('0');
  66. });
  67. });
  68. });
  69. describe('balanceOfBatch', function () {
  70. it('reverts when input arrays don\'t match up', async function () {
  71. await expectRevert(
  72. this.token.balanceOfBatch(
  73. [firstTokenHolder, secondTokenHolder, firstTokenHolder, secondTokenHolder],
  74. [firstTokenId, secondTokenId, unknownTokenId],
  75. ),
  76. 'ERC1155: accounts and ids length mismatch',
  77. );
  78. await expectRevert(
  79. this.token.balanceOfBatch(
  80. [firstTokenHolder, secondTokenHolder],
  81. [firstTokenId, secondTokenId, unknownTokenId],
  82. ),
  83. 'ERC1155: accounts and ids length mismatch',
  84. );
  85. });
  86. it('reverts when one of the addresses is the zero address', async function () {
  87. await expectRevert(
  88. this.token.balanceOfBatch(
  89. [firstTokenHolder, secondTokenHolder, ZERO_ADDRESS],
  90. [firstTokenId, secondTokenId, unknownTokenId],
  91. ),
  92. 'ERC1155: balance query for the zero address',
  93. );
  94. });
  95. context('when accounts don\'t own tokens', function () {
  96. it('returns zeros for each account', async function () {
  97. const result = await this.token.balanceOfBatch(
  98. [firstTokenHolder, secondTokenHolder, firstTokenHolder],
  99. [firstTokenId, secondTokenId, unknownTokenId],
  100. );
  101. expect(result).to.be.an('array');
  102. expect(result[0]).to.be.a.bignumber.equal('0');
  103. expect(result[1]).to.be.a.bignumber.equal('0');
  104. expect(result[2]).to.be.a.bignumber.equal('0');
  105. });
  106. });
  107. context('when accounts own some tokens', function () {
  108. beforeEach(async function () {
  109. await this.token.mint(firstTokenHolder, firstTokenId, firstAmount, '0x', {
  110. from: minter,
  111. });
  112. await this.token.mint(
  113. secondTokenHolder,
  114. secondTokenId,
  115. secondAmount,
  116. '0x',
  117. {
  118. from: minter,
  119. },
  120. );
  121. });
  122. it('returns amounts owned by each account in order passed', async function () {
  123. const result = await this.token.balanceOfBatch(
  124. [secondTokenHolder, firstTokenHolder, firstTokenHolder],
  125. [secondTokenId, firstTokenId, unknownTokenId],
  126. );
  127. expect(result).to.be.an('array');
  128. expect(result[0]).to.be.a.bignumber.equal(secondAmount);
  129. expect(result[1]).to.be.a.bignumber.equal(firstAmount);
  130. expect(result[2]).to.be.a.bignumber.equal('0');
  131. });
  132. it('returns multiple times the balance of the same address when asked', async function () {
  133. const result = await this.token.balanceOfBatch(
  134. [firstTokenHolder, secondTokenHolder, firstTokenHolder],
  135. [firstTokenId, secondTokenId, firstTokenId],
  136. );
  137. expect(result).to.be.an('array');
  138. expect(result[0]).to.be.a.bignumber.equal(result[2]);
  139. expect(result[0]).to.be.a.bignumber.equal(firstAmount);
  140. expect(result[1]).to.be.a.bignumber.equal(secondAmount);
  141. expect(result[2]).to.be.a.bignumber.equal(firstAmount);
  142. });
  143. });
  144. });
  145. describe('setApprovalForAll', function () {
  146. let logs;
  147. beforeEach(async function () {
  148. ({ logs } = await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder }));
  149. });
  150. it('sets approval status which can be queried via isApprovedForAll', async function () {
  151. expect(await this.token.isApprovedForAll(multiTokenHolder, proxy)).to.be.equal(true);
  152. });
  153. it('emits an ApprovalForAll log', function () {
  154. expectEvent.inLogs(logs, 'ApprovalForAll', { account: multiTokenHolder, operator: proxy, approved: true });
  155. });
  156. it('can unset approval for an operator', async function () {
  157. await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
  158. expect(await this.token.isApprovedForAll(multiTokenHolder, proxy)).to.be.equal(false);
  159. });
  160. it('reverts if attempting to approve self as an operator', async function () {
  161. await expectRevert(
  162. this.token.setApprovalForAll(multiTokenHolder, true, { from: multiTokenHolder }),
  163. 'ERC1155: setting approval status for self',
  164. );
  165. });
  166. });
  167. describe('safeTransferFrom', function () {
  168. beforeEach(async function () {
  169. await this.token.mint(multiTokenHolder, firstTokenId, firstAmount, '0x', {
  170. from: minter,
  171. });
  172. await this.token.mint(
  173. multiTokenHolder,
  174. secondTokenId,
  175. secondAmount,
  176. '0x',
  177. {
  178. from: minter,
  179. },
  180. );
  181. });
  182. it('reverts when transferring more than balance', async function () {
  183. await expectRevert.unspecified(
  184. this.token.safeTransferFrom(
  185. multiTokenHolder,
  186. recipient,
  187. firstTokenId,
  188. firstAmount.addn(1),
  189. '0x',
  190. { from: multiTokenHolder },
  191. ),
  192. );
  193. });
  194. it('reverts when transferring to zero address', async function () {
  195. await expectRevert(
  196. this.token.safeTransferFrom(
  197. multiTokenHolder,
  198. ZERO_ADDRESS,
  199. firstTokenId,
  200. firstAmount,
  201. '0x',
  202. { from: multiTokenHolder },
  203. ),
  204. 'ERC1155: transfer to the zero address',
  205. );
  206. });
  207. function transferWasSuccessful ({ operator, from, id, value }) {
  208. it('debits transferred balance from sender', async function () {
  209. const newBalance = await this.token.balanceOf(from, id);
  210. expect(newBalance).to.be.a.bignumber.equal('0');
  211. });
  212. it('credits transferred balance to receiver', async function () {
  213. const newBalance = await this.token.balanceOf(this.toWhom, id);
  214. expect(newBalance).to.be.a.bignumber.equal(value);
  215. });
  216. it('emits a TransferSingle log', function () {
  217. expectEvent.inLogs(this.transferLogs, 'TransferSingle', {
  218. operator,
  219. from,
  220. to: this.toWhom,
  221. id,
  222. value,
  223. });
  224. });
  225. }
  226. context('when called by the multiTokenHolder', async function () {
  227. beforeEach(async function () {
  228. this.toWhom = recipient;
  229. ({ logs: this.transferLogs } =
  230. await this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
  231. from: multiTokenHolder,
  232. }));
  233. });
  234. transferWasSuccessful.call(this, {
  235. operator: multiTokenHolder,
  236. from: multiTokenHolder,
  237. id: firstTokenId,
  238. value: firstAmount,
  239. });
  240. it('preserves existing balances which are not transferred by multiTokenHolder', async function () {
  241. const balance1 = await this.token.balanceOf(multiTokenHolder, secondTokenId);
  242. expect(balance1).to.be.a.bignumber.equal(secondAmount);
  243. const balance2 = await this.token.balanceOf(recipient, secondTokenId);
  244. expect(balance2).to.be.a.bignumber.equal('0');
  245. });
  246. });
  247. context('when called by an operator on behalf of the multiTokenHolder', function () {
  248. context('when operator is not approved by multiTokenHolder', function () {
  249. beforeEach(async function () {
  250. await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
  251. });
  252. it('reverts', async function () {
  253. await expectRevert(
  254. this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
  255. from: proxy,
  256. }),
  257. 'ERC1155: caller is not owner nor approved',
  258. );
  259. });
  260. });
  261. context('when operator is approved by multiTokenHolder', function () {
  262. beforeEach(async function () {
  263. this.toWhom = recipient;
  264. await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
  265. ({ logs: this.transferLogs } =
  266. await this.token.safeTransferFrom(multiTokenHolder, recipient, firstTokenId, firstAmount, '0x', {
  267. from: proxy,
  268. }));
  269. });
  270. transferWasSuccessful.call(this, {
  271. operator: proxy,
  272. from: multiTokenHolder,
  273. id: firstTokenId,
  274. value: firstAmount,
  275. });
  276. it('preserves operator\'s balances not involved in the transfer', async function () {
  277. const balance1 = await this.token.balanceOf(proxy, firstTokenId);
  278. expect(balance1).to.be.a.bignumber.equal('0');
  279. const balance2 = await this.token.balanceOf(proxy, secondTokenId);
  280. expect(balance2).to.be.a.bignumber.equal('0');
  281. });
  282. });
  283. });
  284. context('when sending to a valid receiver', function () {
  285. beforeEach(async function () {
  286. this.receiver = await ERC1155ReceiverMock.new(
  287. RECEIVER_SINGLE_MAGIC_VALUE, false,
  288. RECEIVER_BATCH_MAGIC_VALUE, false,
  289. );
  290. });
  291. context('without data', function () {
  292. beforeEach(async function () {
  293. this.toWhom = this.receiver.address;
  294. this.transferReceipt = await this.token.safeTransferFrom(
  295. multiTokenHolder,
  296. this.receiver.address,
  297. firstTokenId,
  298. firstAmount,
  299. '0x',
  300. { from: multiTokenHolder },
  301. );
  302. ({ logs: this.transferLogs } = this.transferReceipt);
  303. });
  304. transferWasSuccessful.call(this, {
  305. operator: multiTokenHolder,
  306. from: multiTokenHolder,
  307. id: firstTokenId,
  308. value: firstAmount,
  309. });
  310. it('calls onERC1155Received', async function () {
  311. await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'Received', {
  312. operator: multiTokenHolder,
  313. from: multiTokenHolder,
  314. id: firstTokenId,
  315. value: firstAmount,
  316. data: null,
  317. });
  318. });
  319. });
  320. context('with data', function () {
  321. const data = '0xf00dd00d';
  322. beforeEach(async function () {
  323. this.toWhom = this.receiver.address;
  324. this.transferReceipt = await this.token.safeTransferFrom(
  325. multiTokenHolder,
  326. this.receiver.address,
  327. firstTokenId,
  328. firstAmount,
  329. data,
  330. { from: multiTokenHolder },
  331. );
  332. ({ logs: this.transferLogs } = this.transferReceipt);
  333. });
  334. transferWasSuccessful.call(this, {
  335. operator: multiTokenHolder,
  336. from: multiTokenHolder,
  337. id: firstTokenId,
  338. value: firstAmount,
  339. });
  340. it('calls onERC1155Received', async function () {
  341. await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'Received', {
  342. operator: multiTokenHolder,
  343. from: multiTokenHolder,
  344. id: firstTokenId,
  345. value: firstAmount,
  346. data,
  347. });
  348. });
  349. });
  350. });
  351. context('to a receiver contract returning unexpected value', function () {
  352. beforeEach(async function () {
  353. this.receiver = await ERC1155ReceiverMock.new(
  354. '0x00c0ffee', false,
  355. RECEIVER_BATCH_MAGIC_VALUE, false,
  356. );
  357. });
  358. it('reverts', async function () {
  359. await expectRevert(
  360. this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', {
  361. from: multiTokenHolder,
  362. }),
  363. 'ERC1155: ERC1155Receiver rejected tokens',
  364. );
  365. });
  366. });
  367. context('to a receiver contract that reverts', function () {
  368. beforeEach(async function () {
  369. this.receiver = await ERC1155ReceiverMock.new(
  370. RECEIVER_SINGLE_MAGIC_VALUE, true,
  371. RECEIVER_BATCH_MAGIC_VALUE, false,
  372. );
  373. });
  374. it('reverts', async function () {
  375. await expectRevert(
  376. this.token.safeTransferFrom(multiTokenHolder, this.receiver.address, firstTokenId, firstAmount, '0x', {
  377. from: multiTokenHolder,
  378. }),
  379. 'ERC1155ReceiverMock: reverting on receive',
  380. );
  381. });
  382. });
  383. context('to a contract that does not implement the required function', function () {
  384. it('reverts', async function () {
  385. const invalidReceiver = this.token;
  386. await expectRevert.unspecified(
  387. this.token.safeTransferFrom(multiTokenHolder, invalidReceiver.address, firstTokenId, firstAmount, '0x', {
  388. from: multiTokenHolder,
  389. }),
  390. );
  391. });
  392. });
  393. });
  394. describe('safeBatchTransferFrom', function () {
  395. beforeEach(async function () {
  396. await this.token.mint(multiTokenHolder, firstTokenId, firstAmount, '0x', {
  397. from: minter,
  398. });
  399. await this.token.mint(
  400. multiTokenHolder,
  401. secondTokenId,
  402. secondAmount,
  403. '0x',
  404. {
  405. from: minter,
  406. },
  407. );
  408. });
  409. it('reverts when transferring amount more than any of balances', async function () {
  410. await expectRevert.unspecified(
  411. this.token.safeBatchTransferFrom(
  412. multiTokenHolder, recipient,
  413. [firstTokenId, secondTokenId],
  414. [firstAmount, secondAmount.addn(1)],
  415. '0x', { from: multiTokenHolder },
  416. ),
  417. );
  418. });
  419. it('reverts when ids array length doesn\'t match amounts array length', async function () {
  420. await expectRevert(
  421. this.token.safeBatchTransferFrom(
  422. multiTokenHolder, recipient,
  423. [firstTokenId],
  424. [firstAmount, secondAmount],
  425. '0x', { from: multiTokenHolder },
  426. ),
  427. 'ERC1155: ids and amounts length mismatch',
  428. );
  429. await expectRevert(
  430. this.token.safeBatchTransferFrom(
  431. multiTokenHolder, recipient,
  432. [firstTokenId, secondTokenId],
  433. [firstAmount],
  434. '0x', { from: multiTokenHolder },
  435. ),
  436. 'ERC1155: ids and amounts length mismatch',
  437. );
  438. });
  439. it('reverts when transferring to zero address', async function () {
  440. await expectRevert(
  441. this.token.safeBatchTransferFrom(
  442. multiTokenHolder, ZERO_ADDRESS,
  443. [firstTokenId, secondTokenId],
  444. [firstAmount, secondAmount],
  445. '0x', { from: multiTokenHolder },
  446. ),
  447. 'ERC1155: transfer to the zero address',
  448. );
  449. });
  450. function batchTransferWasSuccessful ({ operator, from, ids, values }) {
  451. it('debits transferred balances from sender', async function () {
  452. const newBalances = await this.token.balanceOfBatch(new Array(ids.length).fill(from), ids);
  453. for (const newBalance of newBalances) {
  454. expect(newBalance).to.be.a.bignumber.equal('0');
  455. }
  456. });
  457. it('credits transferred balances to receiver', async function () {
  458. const newBalances = await this.token.balanceOfBatch(new Array(ids.length).fill(this.toWhom), ids);
  459. for (let i = 0; i < newBalances.length; i++) {
  460. expect(newBalances[i]).to.be.a.bignumber.equal(values[i]);
  461. }
  462. });
  463. it('emits a TransferBatch log', function () {
  464. expectEvent.inLogs(this.transferLogs, 'TransferBatch', {
  465. operator,
  466. from,
  467. to: this.toWhom,
  468. // ids,
  469. // values,
  470. });
  471. });
  472. }
  473. context('when called by the multiTokenHolder', async function () {
  474. beforeEach(async function () {
  475. this.toWhom = recipient;
  476. ({ logs: this.transferLogs } =
  477. await this.token.safeBatchTransferFrom(
  478. multiTokenHolder, recipient,
  479. [firstTokenId, secondTokenId],
  480. [firstAmount, secondAmount],
  481. '0x', { from: multiTokenHolder },
  482. ));
  483. });
  484. batchTransferWasSuccessful.call(this, {
  485. operator: multiTokenHolder,
  486. from: multiTokenHolder,
  487. ids: [firstTokenId, secondTokenId],
  488. values: [firstAmount, secondAmount],
  489. });
  490. });
  491. context('when called by an operator on behalf of the multiTokenHolder', function () {
  492. context('when operator is not approved by multiTokenHolder', function () {
  493. beforeEach(async function () {
  494. await this.token.setApprovalForAll(proxy, false, { from: multiTokenHolder });
  495. });
  496. it('reverts', async function () {
  497. await expectRevert(
  498. this.token.safeBatchTransferFrom(
  499. multiTokenHolder, recipient,
  500. [firstTokenId, secondTokenId],
  501. [firstAmount, secondAmount],
  502. '0x', { from: proxy },
  503. ),
  504. 'ERC1155: transfer caller is not owner nor approved',
  505. );
  506. });
  507. });
  508. context('when operator is approved by multiTokenHolder', function () {
  509. beforeEach(async function () {
  510. this.toWhom = recipient;
  511. await this.token.setApprovalForAll(proxy, true, { from: multiTokenHolder });
  512. ({ logs: this.transferLogs } =
  513. await this.token.safeBatchTransferFrom(
  514. multiTokenHolder, recipient,
  515. [firstTokenId, secondTokenId],
  516. [firstAmount, secondAmount],
  517. '0x', { from: proxy },
  518. ));
  519. });
  520. batchTransferWasSuccessful.call(this, {
  521. operator: proxy,
  522. from: multiTokenHolder,
  523. ids: [firstTokenId, secondTokenId],
  524. values: [firstAmount, secondAmount],
  525. });
  526. it('preserves operator\'s balances not involved in the transfer', async function () {
  527. const balance1 = await this.token.balanceOf(proxy, firstTokenId);
  528. expect(balance1).to.be.a.bignumber.equal('0');
  529. const balance2 = await this.token.balanceOf(proxy, secondTokenId);
  530. expect(balance2).to.be.a.bignumber.equal('0');
  531. });
  532. });
  533. });
  534. context('when sending to a valid receiver', function () {
  535. beforeEach(async function () {
  536. this.receiver = await ERC1155ReceiverMock.new(
  537. RECEIVER_SINGLE_MAGIC_VALUE, false,
  538. RECEIVER_BATCH_MAGIC_VALUE, false,
  539. );
  540. });
  541. context('without data', function () {
  542. beforeEach(async function () {
  543. this.toWhom = this.receiver.address;
  544. this.transferReceipt = await this.token.safeBatchTransferFrom(
  545. multiTokenHolder, this.receiver.address,
  546. [firstTokenId, secondTokenId],
  547. [firstAmount, secondAmount],
  548. '0x', { from: multiTokenHolder },
  549. );
  550. ({ logs: this.transferLogs } = this.transferReceipt);
  551. });
  552. batchTransferWasSuccessful.call(this, {
  553. operator: multiTokenHolder,
  554. from: multiTokenHolder,
  555. ids: [firstTokenId, secondTokenId],
  556. values: [firstAmount, secondAmount],
  557. });
  558. it('calls onERC1155BatchReceived', async function () {
  559. await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
  560. operator: multiTokenHolder,
  561. from: multiTokenHolder,
  562. // ids: [firstTokenId, secondTokenId],
  563. // values: [firstAmount, secondAmount],
  564. data: null,
  565. });
  566. });
  567. });
  568. context('with data', function () {
  569. const data = '0xf00dd00d';
  570. beforeEach(async function () {
  571. this.toWhom = this.receiver.address;
  572. this.transferReceipt = await this.token.safeBatchTransferFrom(
  573. multiTokenHolder, this.receiver.address,
  574. [firstTokenId, secondTokenId],
  575. [firstAmount, secondAmount],
  576. data, { from: multiTokenHolder },
  577. );
  578. ({ logs: this.transferLogs } = this.transferReceipt);
  579. });
  580. batchTransferWasSuccessful.call(this, {
  581. operator: multiTokenHolder,
  582. from: multiTokenHolder,
  583. ids: [firstTokenId, secondTokenId],
  584. values: [firstAmount, secondAmount],
  585. });
  586. it('calls onERC1155Received', async function () {
  587. await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
  588. operator: multiTokenHolder,
  589. from: multiTokenHolder,
  590. // ids: [firstTokenId, secondTokenId],
  591. // values: [firstAmount, secondAmount],
  592. data,
  593. });
  594. });
  595. });
  596. });
  597. context('to a receiver contract returning unexpected value', function () {
  598. beforeEach(async function () {
  599. this.receiver = await ERC1155ReceiverMock.new(
  600. RECEIVER_SINGLE_MAGIC_VALUE, false,
  601. RECEIVER_SINGLE_MAGIC_VALUE, false,
  602. );
  603. });
  604. it('reverts', async function () {
  605. await expectRevert(
  606. this.token.safeBatchTransferFrom(
  607. multiTokenHolder, this.receiver.address,
  608. [firstTokenId, secondTokenId],
  609. [firstAmount, secondAmount],
  610. '0x', { from: multiTokenHolder },
  611. ),
  612. 'ERC1155: ERC1155Receiver rejected tokens',
  613. );
  614. });
  615. });
  616. context('to a receiver contract that reverts', function () {
  617. beforeEach(async function () {
  618. this.receiver = await ERC1155ReceiverMock.new(
  619. RECEIVER_SINGLE_MAGIC_VALUE, false,
  620. RECEIVER_BATCH_MAGIC_VALUE, true,
  621. );
  622. });
  623. it('reverts', async function () {
  624. await expectRevert(
  625. this.token.safeBatchTransferFrom(
  626. multiTokenHolder, this.receiver.address,
  627. [firstTokenId, secondTokenId],
  628. [firstAmount, secondAmount],
  629. '0x', { from: multiTokenHolder },
  630. ),
  631. 'ERC1155ReceiverMock: reverting on batch receive',
  632. );
  633. });
  634. });
  635. context('to a receiver contract that reverts only on single transfers', function () {
  636. beforeEach(async function () {
  637. this.receiver = await ERC1155ReceiverMock.new(
  638. RECEIVER_SINGLE_MAGIC_VALUE, true,
  639. RECEIVER_BATCH_MAGIC_VALUE, false,
  640. );
  641. this.toWhom = this.receiver.address;
  642. this.transferReceipt = await this.token.safeBatchTransferFrom(
  643. multiTokenHolder, this.receiver.address,
  644. [firstTokenId, secondTokenId],
  645. [firstAmount, secondAmount],
  646. '0x', { from: multiTokenHolder },
  647. );
  648. ({ logs: this.transferLogs } = this.transferReceipt);
  649. });
  650. batchTransferWasSuccessful.call(this, {
  651. operator: multiTokenHolder,
  652. from: multiTokenHolder,
  653. ids: [firstTokenId, secondTokenId],
  654. values: [firstAmount, secondAmount],
  655. });
  656. it('calls onERC1155BatchReceived', async function () {
  657. await expectEvent.inTransaction(this.transferReceipt.tx, ERC1155ReceiverMock, 'BatchReceived', {
  658. operator: multiTokenHolder,
  659. from: multiTokenHolder,
  660. // ids: [firstTokenId, secondTokenId],
  661. // values: [firstAmount, secondAmount],
  662. data: null,
  663. });
  664. });
  665. });
  666. context('to a contract that does not implement the required function', function () {
  667. it('reverts', async function () {
  668. const invalidReceiver = this.token;
  669. await expectRevert.unspecified(
  670. this.token.safeBatchTransferFrom(
  671. multiTokenHolder, invalidReceiver.address,
  672. [firstTokenId, secondTokenId],
  673. [firstAmount, secondAmount],
  674. '0x', { from: multiTokenHolder },
  675. ),
  676. );
  677. });
  678. });
  679. });
  680. shouldSupportInterfaces(['ERC165', 'ERC1155']);
  681. });
  682. }
  683. module.exports = {
  684. shouldBehaveLikeERC1155,
  685. };