ERC721Wrapper.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.underlying, 'Transfer', {
  165. from: this.token.address,
  166. to: initialHolder,
  167. tokenId: secondTokenId,
  168. });
  169. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  170. from: initialHolder,
  171. to: constants.ZERO_ADDRESS,
  172. tokenId: firstTokenId,
  173. });
  174. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  175. from: initialHolder,
  176. to: constants.ZERO_ADDRESS,
  177. tokenId: secondTokenId,
  178. });
  179. });
  180. it('works to another account', async function () {
  181. const { tx } = await this.token.withdrawTo(anotherAccount, [firstTokenId], { from: initialHolder });
  182. await expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  183. from: this.token.address,
  184. to: anotherAccount,
  185. tokenId: firstTokenId,
  186. });
  187. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  188. from: initialHolder,
  189. to: constants.ZERO_ADDRESS,
  190. tokenId: firstTokenId,
  191. });
  192. });
  193. });
  194. describe('onERC721Received', function () {
  195. const WRAPPER_ACCEPT_MAGIC = bufferToHex(keccakFromString('WRAPPER_ACCEPT_MAGIC')).slice(0, 26); // Include 0x
  196. const magicWithAddresss = address =>
  197. web3.utils.encodePacked(
  198. {
  199. value: WRAPPER_ACCEPT_MAGIC,
  200. type: 'bytes12',
  201. },
  202. {
  203. value: address,
  204. type: 'address',
  205. },
  206. );
  207. it('only allows calls from underlying', async function () {
  208. await expectRevert(
  209. this.token.onERC721Received(
  210. initialHolder,
  211. this.token.address,
  212. firstTokenId,
  213. magicWithAddresss(anotherAccount), // Correct data
  214. { from: anotherAccount },
  215. ),
  216. 'ERC721Wrapper: caller is not underlying',
  217. );
  218. });
  219. describe('when data length is > 0', function () {
  220. it('reverts with arbitrary data', async function () {
  221. await expectRevert(
  222. this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
  223. initialHolder,
  224. this.token.address,
  225. firstTokenId,
  226. '0x0123',
  227. {
  228. from: initialHolder,
  229. },
  230. ),
  231. 'ERC721Wrapper: Invalid data format',
  232. );
  233. });
  234. it('reverts with the magic value and data length different to 32', async function () {
  235. await expectRevert(
  236. this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
  237. initialHolder,
  238. this.token.address,
  239. firstTokenId,
  240. WRAPPER_ACCEPT_MAGIC, // Reverts for any non-32 bytes value
  241. {
  242. from: initialHolder,
  243. },
  244. ),
  245. 'ERC721Wrapper: Invalid data format',
  246. );
  247. });
  248. it('mints token to specific holder with address after magic value', async function () {
  249. const { tx } = await this.underlying.methods['safeTransferFrom(address,address,uint256,bytes)'](
  250. initialHolder,
  251. this.token.address,
  252. firstTokenId,
  253. magicWithAddresss(anotherAccount),
  254. {
  255. from: initialHolder,
  256. },
  257. );
  258. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  259. from: constants.ZERO_ADDRESS,
  260. to: anotherAccount,
  261. tokenId: firstTokenId,
  262. });
  263. });
  264. });
  265. it('mints a token to from if no data is specified', async function () {
  266. const { tx } = await this.underlying.safeTransferFrom(initialHolder, this.token.address, firstTokenId, {
  267. from: initialHolder,
  268. });
  269. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  270. from: constants.ZERO_ADDRESS,
  271. to: initialHolder,
  272. tokenId: firstTokenId,
  273. });
  274. });
  275. });
  276. describe('_recover', function () {
  277. it('works if there is something to recover', async function () {
  278. // Should use `transferFrom` to avoid `onERC721Received` minting
  279. await this.underlying.transferFrom(initialHolder, this.token.address, firstTokenId, { from: initialHolder });
  280. const { tx } = await this.token.$_recover(anotherAccount, firstTokenId);
  281. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  282. from: constants.ZERO_ADDRESS,
  283. to: anotherAccount,
  284. tokenId: firstTokenId,
  285. });
  286. });
  287. it('reverts if there is nothing to recover', async function () {
  288. await expectRevert(
  289. this.token.$_recover(initialHolder, firstTokenId),
  290. 'ERC721Wrapper: wrapper is not token owner',
  291. );
  292. });
  293. });
  294. describe('ERC712 behavior', function () {
  295. shouldBehaveLikeERC721('ERC721', ...accounts);
  296. });
  297. });