ERC721Wrapper.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. const { BN, expectEvent, constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
  4. const ERC721 = artifacts.require('$ERC721');
  5. const ERC721Wrapper = artifacts.require('$ERC721Wrapper');
  6. contract('ERC721Wrapper', function (accounts) {
  7. const [initialHolder, anotherAccount, approvedAccount] = accounts;
  8. const name = 'My Token';
  9. const symbol = 'MTKN';
  10. const firstTokenId = new BN(1);
  11. const secondTokenId = new BN(2);
  12. beforeEach(async function () {
  13. this.underlying = await ERC721.new(name, symbol);
  14. this.token = await ERC721Wrapper.new(`Wrapped ${name}`, `W${symbol}`, this.underlying.address);
  15. await this.underlying.$_safeMint(initialHolder, firstTokenId);
  16. await this.underlying.$_safeMint(initialHolder, secondTokenId);
  17. });
  18. it('has a name', async function () {
  19. expect(await this.token.name()).to.equal(`Wrapped ${name}`);
  20. });
  21. it('has a symbol', async function () {
  22. expect(await this.token.symbol()).to.equal(`W${symbol}`);
  23. });
  24. it('has underlying', async function () {
  25. expect(await this.token.underlying()).to.be.bignumber.equal(this.underlying.address);
  26. });
  27. describe('depositFor', function () {
  28. it('works with token approval', async function () {
  29. await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
  30. const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
  31. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  32. from: initialHolder,
  33. to: this.token.address,
  34. tokenId: firstTokenId,
  35. });
  36. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  37. from: constants.ZERO_ADDRESS,
  38. to: initialHolder,
  39. tokenId: firstTokenId,
  40. });
  41. });
  42. it('works with approval for all', async function () {
  43. await this.underlying.setApprovalForAll(this.token.address, true, { from: initialHolder });
  44. const { tx } = await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
  45. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  46. from: initialHolder,
  47. to: this.token.address,
  48. tokenId: firstTokenId,
  49. });
  50. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  51. from: constants.ZERO_ADDRESS,
  52. to: initialHolder,
  53. tokenId: firstTokenId,
  54. });
  55. });
  56. it('works sending to another account', async function () {
  57. await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
  58. const { tx } = await this.token.depositFor(anotherAccount, [firstTokenId], { from: initialHolder });
  59. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  60. from: initialHolder,
  61. to: this.token.address,
  62. tokenId: firstTokenId,
  63. });
  64. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  65. from: constants.ZERO_ADDRESS,
  66. to: anotherAccount,
  67. tokenId: firstTokenId,
  68. });
  69. });
  70. it('works with multiple tokens', async function () {
  71. await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
  72. await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
  73. const { tx } = await this.token.depositFor(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
  74. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  75. from: initialHolder,
  76. to: this.token.address,
  77. tokenId: firstTokenId,
  78. });
  79. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  80. from: constants.ZERO_ADDRESS,
  81. to: initialHolder,
  82. tokenId: firstTokenId,
  83. });
  84. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  85. from: initialHolder,
  86. to: this.token.address,
  87. tokenId: secondTokenId,
  88. });
  89. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  90. from: constants.ZERO_ADDRESS,
  91. to: initialHolder,
  92. tokenId: secondTokenId,
  93. });
  94. });
  95. it('reverts with missing approval', async function () {
  96. await expectRevert(
  97. this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder }),
  98. 'ERC721: caller is not token owner or approved',
  99. );
  100. });
  101. });
  102. describe('withdrawTo', function () {
  103. beforeEach(async function () {
  104. await this.underlying.approve(this.token.address, firstTokenId, { from: initialHolder });
  105. await this.token.depositFor(initialHolder, [firstTokenId], { from: initialHolder });
  106. });
  107. it('works for an owner', async function () {
  108. const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: initialHolder });
  109. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  110. from: this.token.address,
  111. to: initialHolder,
  112. tokenId: firstTokenId,
  113. });
  114. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  115. from: initialHolder,
  116. to: constants.ZERO_ADDRESS,
  117. tokenId: firstTokenId,
  118. });
  119. });
  120. it('works for an approved', async function () {
  121. await this.token.approve(approvedAccount, firstTokenId, { from: initialHolder });
  122. const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
  123. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  124. from: this.token.address,
  125. to: initialHolder,
  126. tokenId: firstTokenId,
  127. });
  128. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  129. from: initialHolder,
  130. to: constants.ZERO_ADDRESS,
  131. tokenId: firstTokenId,
  132. });
  133. });
  134. it('works for an approved for all', async function () {
  135. await this.token.setApprovalForAll(approvedAccount, true, { from: initialHolder });
  136. const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId], { from: approvedAccount });
  137. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  138. from: this.token.address,
  139. to: initialHolder,
  140. tokenId: firstTokenId,
  141. });
  142. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  143. from: initialHolder,
  144. to: constants.ZERO_ADDRESS,
  145. tokenId: firstTokenId,
  146. });
  147. });
  148. it("doesn't work for a non-owner nor approved", async function () {
  149. await expectRevert(
  150. this.token.withdrawTo(initialHolder, [firstTokenId], { from: anotherAccount }),
  151. 'ERC721Wrapper: caller is not token owner or approved',
  152. );
  153. });
  154. it('works with multiple tokens', async function () {
  155. await this.underlying.approve(this.token.address, secondTokenId, { from: initialHolder });
  156. await this.token.depositFor(initialHolder, [secondTokenId], { from: initialHolder });
  157. const { tx } = await this.token.withdrawTo(initialHolder, [firstTokenId, secondTokenId], { from: initialHolder });
  158. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  159. from: this.token.address,
  160. to: initialHolder,
  161. tokenId: firstTokenId,
  162. });
  163. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  164. from: this.token.address,
  165. to: initialHolder,
  166. tokenId: secondTokenId,
  167. });
  168. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  169. from: initialHolder,
  170. to: constants.ZERO_ADDRESS,
  171. tokenId: firstTokenId,
  172. });
  173. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  174. from: initialHolder,
  175. to: constants.ZERO_ADDRESS,
  176. tokenId: secondTokenId,
  177. });
  178. });
  179. it('works to another account', async function () {
  180. const { tx } = await this.token.withdrawTo(anotherAccount, [firstTokenId], { from: initialHolder });
  181. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  182. from: this.token.address,
  183. to: anotherAccount,
  184. tokenId: firstTokenId,
  185. });
  186. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  187. from: initialHolder,
  188. to: constants.ZERO_ADDRESS,
  189. tokenId: firstTokenId,
  190. });
  191. });
  192. });
  193. describe('onERC721Received', function () {
  194. it('only allows calls from underlying', async function () {
  195. await expectRevert(
  196. this.token.onERC721Received(
  197. initialHolder,
  198. this.token.address,
  199. firstTokenId,
  200. anotherAccount, // Correct data
  201. { from: anotherAccount },
  202. ),
  203. 'ERC721Wrapper: caller is not underlying',
  204. );
  205. });
  206. describe('when data length is > 0', function () {
  207. it('reverts with arbitrary data', async function () {
  208. await expectRevert(
  209. this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
  210. initialHolder,
  211. this.token.address,
  212. firstTokenId,
  213. '0x0123',
  214. {
  215. from: initialHolder,
  216. },
  217. ),
  218. 'ERC721Wrapper: Invalid data format',
  219. );
  220. });
  221. it('reverts with correct data from an untrusted operator', async function () {
  222. await expectRevert(
  223. this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
  224. initialHolder,
  225. this.token.address,
  226. firstTokenId,
  227. anotherAccount,
  228. {
  229. from: initialHolder,
  230. },
  231. ),
  232. 'ERC721Wrapper: Invalid data format',
  233. );
  234. });
  235. });
  236. it('mints a token to from if no data is specified', async function () {
  237. const { tx } = await this.underlying.safeTransferFrom(initialHolder, this.token.address, firstTokenId, {
  238. from: initialHolder,
  239. });
  240. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  241. from: constants.ZERO_ADDRESS,
  242. to: initialHolder,
  243. tokenId: firstTokenId,
  244. });
  245. });
  246. });
  247. describe('_recover', function () {
  248. it('works if there is something to recover', async function () {
  249. // Should use `transferFrom` to avoid `onERC721Received` minting
  250. await this.underlying.transferFrom(initialHolder, this.token.address, firstTokenId, { from: initialHolder });
  251. const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
  252. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  253. from: constants.ZERO_ADDRESS,
  254. to: anotherAccount,
  255. tokenId: firstTokenId,
  256. });
  257. });
  258. it('reverts if there is nothing to recover', async function () {
  259. await expectRevert(
  260. this.token.$_recover(initialHolder, firstTokenId),
  261. 'ERC721Wrapper: wrapper is not token owner',
  262. );
  263. });
  264. });
  265. describe('ERC712 behavior', function () {
  266. shouldBehaveLikeERC721('ERC721', ...accounts);
  267. });
  268. });