ERC721Wrapper.test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { shouldBehaveLikeERC721 } = require('../ERC721.behavior');
  5. const name = 'Non Fungible Token';
  6. const symbol = 'NFT';
  7. const tokenId = 1n;
  8. const otherTokenId = 2n;
  9. async function fixture() {
  10. const accounts = await ethers.getSigners();
  11. const [owner, approved, other] = accounts;
  12. const underlying = await ethers.deployContract('$ERC721', [name, symbol]);
  13. await underlying.$_safeMint(owner, tokenId);
  14. await underlying.$_safeMint(owner, otherTokenId);
  15. const token = await ethers.deployContract('$ERC721Wrapper', [`Wrapped ${name}`, `W${symbol}`, underlying]);
  16. return { accounts, owner, approved, other, underlying, token };
  17. }
  18. describe('ERC721Wrapper', function () {
  19. beforeEach(async function () {
  20. Object.assign(this, await loadFixture(fixture));
  21. });
  22. it('has a name', async function () {
  23. expect(await this.token.name()).to.equal(`Wrapped ${name}`);
  24. });
  25. it('has a symbol', async function () {
  26. expect(await this.token.symbol()).to.equal(`W${symbol}`);
  27. });
  28. it('has underlying', async function () {
  29. expect(await this.token.underlying()).to.equal(this.underlying.target);
  30. });
  31. describe('depositFor', function () {
  32. it('works with token approval', async function () {
  33. await this.underlying.connect(this.owner).approve(this.token, tokenId);
  34. await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
  35. .to.emit(this.underlying, 'Transfer')
  36. .withArgs(this.owner.address, this.token.target, tokenId)
  37. .to.emit(this.token, 'Transfer')
  38. .withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
  39. });
  40. it('works with approval for all', async function () {
  41. await this.underlying.connect(this.owner).setApprovalForAll(this.token, true);
  42. await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
  43. .to.emit(this.underlying, 'Transfer')
  44. .withArgs(this.owner.address, this.token.target, tokenId)
  45. .to.emit(this.token, 'Transfer')
  46. .withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
  47. });
  48. it('works sending to another account', async function () {
  49. await this.underlying.connect(this.owner).approve(this.token, tokenId);
  50. await expect(this.token.connect(this.owner).depositFor(this.other, [tokenId]))
  51. .to.emit(this.underlying, 'Transfer')
  52. .withArgs(this.owner.address, this.token.target, tokenId)
  53. .to.emit(this.token, 'Transfer')
  54. .withArgs(ethers.ZeroAddress, this.other.address, tokenId);
  55. });
  56. it('works with multiple tokens', async function () {
  57. await this.underlying.connect(this.owner).approve(this.token, tokenId);
  58. await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
  59. await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId, otherTokenId]))
  60. .to.emit(this.underlying, 'Transfer')
  61. .withArgs(this.owner.address, this.token.target, tokenId)
  62. .to.emit(this.token, 'Transfer')
  63. .withArgs(ethers.ZeroAddress, this.owner.address, tokenId)
  64. .to.emit(this.underlying, 'Transfer')
  65. .withArgs(this.owner.address, this.token.target, otherTokenId)
  66. .to.emit(this.token, 'Transfer')
  67. .withArgs(ethers.ZeroAddress, this.owner.address, otherTokenId);
  68. });
  69. it('reverts with missing approval', async function () {
  70. await expect(this.token.connect(this.owner).depositFor(this.owner, [tokenId]))
  71. .to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
  72. .withArgs(this.token.target, tokenId);
  73. });
  74. });
  75. describe('withdrawTo', function () {
  76. beforeEach(async function () {
  77. await this.underlying.connect(this.owner).approve(this.token, tokenId);
  78. await this.token.connect(this.owner).depositFor(this.owner, [tokenId]);
  79. });
  80. it('works for an owner', async function () {
  81. await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId]))
  82. .to.emit(this.underlying, 'Transfer')
  83. .withArgs(this.token.target, this.owner.address, tokenId)
  84. .to.emit(this.token, 'Transfer')
  85. .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
  86. });
  87. it('works for an approved', async function () {
  88. await this.token.connect(this.owner).approve(this.approved, tokenId);
  89. await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
  90. .to.emit(this.underlying, 'Transfer')
  91. .withArgs(this.token.target, this.owner.address, tokenId)
  92. .to.emit(this.token, 'Transfer')
  93. .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
  94. });
  95. it('works for an approved for all', async function () {
  96. await this.token.connect(this.owner).setApprovalForAll(this.approved, true);
  97. await expect(this.token.connect(this.approved).withdrawTo(this.owner, [tokenId]))
  98. .to.emit(this.underlying, 'Transfer')
  99. .withArgs(this.token.target, this.owner.address, tokenId)
  100. .to.emit(this.token, 'Transfer')
  101. .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
  102. });
  103. it("doesn't work for a non-owner nor approved", async function () {
  104. await expect(this.token.connect(this.other).withdrawTo(this.owner, [tokenId]))
  105. .to.be.revertedWithCustomError(this.token, 'ERC721InsufficientApproval')
  106. .withArgs(this.other.address, tokenId);
  107. });
  108. it('works with multiple tokens', async function () {
  109. await this.underlying.connect(this.owner).approve(this.token, otherTokenId);
  110. await this.token.connect(this.owner).depositFor(this.owner, [otherTokenId]);
  111. await expect(this.token.connect(this.owner).withdrawTo(this.owner, [tokenId, otherTokenId]))
  112. .to.emit(this.underlying, 'Transfer')
  113. .withArgs(this.token.target, this.owner.address, tokenId)
  114. .to.emit(this.underlying, 'Transfer')
  115. .withArgs(this.token.target, this.owner.address, tokenId)
  116. .to.emit(this.token, 'Transfer')
  117. .withArgs(this.owner.address, ethers.ZeroAddress, tokenId)
  118. .to.emit(this.token, 'Transfer')
  119. .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
  120. });
  121. it('works to another account', async function () {
  122. await expect(this.token.connect(this.owner).withdrawTo(this.other, [tokenId]))
  123. .to.emit(this.underlying, 'Transfer')
  124. .withArgs(this.token.target, this.other.address, tokenId)
  125. .to.emit(this.token, 'Transfer')
  126. .withArgs(this.owner.address, ethers.ZeroAddress, tokenId);
  127. });
  128. });
  129. describe('onERC721Received', function () {
  130. it('only allows calls from underlying', async function () {
  131. await expect(
  132. this.token.connect(this.other).onERC721Received(
  133. this.owner,
  134. this.token,
  135. tokenId,
  136. this.other.address, // Correct data
  137. ),
  138. )
  139. .to.be.revertedWithCustomError(this.token, 'ERC721UnsupportedToken')
  140. .withArgs(this.other.address);
  141. });
  142. it('mints a token to from', async function () {
  143. await expect(this.underlying.connect(this.owner).safeTransferFrom(this.owner, this.token, tokenId))
  144. .to.emit(this.token, 'Transfer')
  145. .withArgs(ethers.ZeroAddress, this.owner.address, tokenId);
  146. });
  147. });
  148. describe('_recover', function () {
  149. it('works if there is something to recover', async function () {
  150. // Should use `transferFrom` to avoid `onERC721Received` minting
  151. await this.underlying.connect(this.owner).transferFrom(this.owner, this.token, tokenId);
  152. await expect(this.token.$_recover(this.other, tokenId))
  153. .to.emit(this.token, 'Transfer')
  154. .withArgs(ethers.ZeroAddress, this.other.address, tokenId);
  155. });
  156. it('reverts if there is nothing to recover', async function () {
  157. const holder = await this.underlying.ownerOf(tokenId);
  158. await expect(this.token.$_recover(holder, tokenId))
  159. .to.be.revertedWithCustomError(this.token, 'ERC721IncorrectOwner')
  160. .withArgs(this.token.target, tokenId, holder);
  161. });
  162. });
  163. describe('ERC712 behavior', function () {
  164. shouldBehaveLikeERC721();
  165. });
  166. });