ERC721Wrapper.test.js 11 KB

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