ERC20Wrapper.test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { shouldBehaveLikeERC20 } = require('../ERC20.behavior');
  5. const name = 'My Token';
  6. const symbol = 'MTKN';
  7. const initialSupply = 100n;
  8. async function fixture() {
  9. const [initialHolder, recipient, anotherAccount] = await ethers.getSigners();
  10. const underlying = await ethers.deployContract('$ERC20DecimalsMock', [name, symbol, 9]);
  11. await underlying.$_mint(initialHolder, initialSupply);
  12. const token = await ethers.deployContract('$ERC20Wrapper', [`Wrapped ${name}`, `W${symbol}`, underlying]);
  13. return { initialHolder, recipient, anotherAccount, underlying, token };
  14. }
  15. describe('ERC20Wrapper', function () {
  16. const name = 'My Token';
  17. const symbol = 'MTKN';
  18. beforeEach(async function () {
  19. Object.assign(this, await loadFixture(fixture));
  20. });
  21. afterEach('Underlying balance', async function () {
  22. expect(await this.underlying.balanceOf(this.token)).to.be.equal(await this.token.totalSupply());
  23. });
  24. it('has a name', async function () {
  25. expect(await this.token.name()).to.equal(`Wrapped ${name}`);
  26. });
  27. it('has a symbol', async function () {
  28. expect(await this.token.symbol()).to.equal(`W${symbol}`);
  29. });
  30. it('has the same decimals as the underlying token', async function () {
  31. expect(await this.token.decimals()).to.be.equal(9n);
  32. });
  33. it('decimals default back to 18 if token has no metadata', async function () {
  34. const noDecimals = await ethers.deployContract('CallReceiverMock');
  35. const token = await ethers.deployContract('$ERC20Wrapper', [`Wrapped ${name}`, `W${symbol}`, noDecimals]);
  36. expect(await token.decimals()).to.be.equal(18n);
  37. });
  38. it('has underlying', async function () {
  39. expect(await this.token.underlying()).to.be.equal(this.underlying.target);
  40. });
  41. describe('deposit', function () {
  42. it('executes with approval', async function () {
  43. await this.underlying.connect(this.initialHolder).approve(this.token, initialSupply);
  44. const tx = await this.token.connect(this.initialHolder).depositFor(this.initialHolder, initialSupply);
  45. await expect(tx)
  46. .to.emit(this.underlying, 'Transfer')
  47. .withArgs(this.initialHolder.address, this.token.target, initialSupply)
  48. .to.emit(this.token, 'Transfer')
  49. .withArgs(ethers.ZeroAddress, this.initialHolder.address, initialSupply);
  50. await expect(tx).to.changeTokenBalances(
  51. this.underlying,
  52. [this.initialHolder, this.token],
  53. [-initialSupply, initialSupply],
  54. );
  55. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, initialSupply);
  56. });
  57. it('reverts when missing approval', async function () {
  58. await expect(this.token.connect(this.initialHolder).depositFor(this.initialHolder, initialSupply))
  59. .to.be.revertedWithCustomError(this.underlying, 'ERC20InsufficientAllowance')
  60. .withArgs(this.token.target, 0, initialSupply);
  61. });
  62. it('reverts when inssuficient balance', async function () {
  63. await this.underlying.connect(this.initialHolder).approve(this.token, ethers.MaxUint256);
  64. await expect(this.token.connect(this.initialHolder).depositFor(this.initialHolder, ethers.MaxUint256))
  65. .to.be.revertedWithCustomError(this.underlying, 'ERC20InsufficientBalance')
  66. .withArgs(this.initialHolder.address, initialSupply, ethers.MaxUint256);
  67. });
  68. it('deposits to other account', async function () {
  69. await this.underlying.connect(this.initialHolder).approve(this.token, initialSupply);
  70. const tx = await this.token.connect(this.initialHolder).depositFor(this.recipient, initialSupply);
  71. await expect(tx)
  72. .to.emit(this.underlying, 'Transfer')
  73. .withArgs(this.initialHolder.address, this.token.target, initialSupply)
  74. .to.emit(this.token, 'Transfer')
  75. .withArgs(ethers.ZeroAddress, this.recipient.address, initialSupply);
  76. await expect(tx).to.changeTokenBalances(
  77. this.underlying,
  78. [this.initialHolder, this.token],
  79. [-initialSupply, initialSupply],
  80. );
  81. await expect(tx).to.changeTokenBalances(this.token, [this.initialHolder, this.recipient], [0, initialSupply]);
  82. });
  83. it('reverts minting to the wrapper contract', async function () {
  84. await this.underlying.connect(this.initialHolder).approve(this.token, ethers.MaxUint256);
  85. await expect(this.token.connect(this.initialHolder).depositFor(this.token, ethers.MaxUint256))
  86. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  87. .withArgs(this.token.target);
  88. });
  89. });
  90. describe('withdraw', function () {
  91. beforeEach(async function () {
  92. await this.underlying.connect(this.initialHolder).approve(this.token, initialSupply);
  93. await this.token.connect(this.initialHolder).depositFor(this.initialHolder, initialSupply);
  94. });
  95. it('reverts when inssuficient balance', async function () {
  96. await expect(this.token.connect(this.initialHolder).withdrawTo(this.initialHolder, ethers.MaxInt256))
  97. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  98. .withArgs(this.initialHolder.address, initialSupply, ethers.MaxInt256);
  99. });
  100. it('executes when operation is valid', async function () {
  101. const value = 42n;
  102. const tx = await this.token.connect(this.initialHolder).withdrawTo(this.initialHolder, value);
  103. await expect(tx)
  104. .to.emit(this.underlying, 'Transfer')
  105. .withArgs(this.token.target, this.initialHolder.address, value)
  106. .to.emit(this.token, 'Transfer')
  107. .withArgs(this.initialHolder.address, ethers.ZeroAddress, value);
  108. await expect(tx).to.changeTokenBalances(this.underlying, [this.token, this.initialHolder], [-value, value]);
  109. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, -value);
  110. });
  111. it('entire balance', async function () {
  112. const tx = await this.token.connect(this.initialHolder).withdrawTo(this.initialHolder, initialSupply);
  113. await expect(tx)
  114. .to.emit(this.underlying, 'Transfer')
  115. .withArgs(this.token.target, this.initialHolder.address, initialSupply)
  116. .to.emit(this.token, 'Transfer')
  117. .withArgs(this.initialHolder.address, ethers.ZeroAddress, initialSupply);
  118. await expect(tx).to.changeTokenBalances(
  119. this.underlying,
  120. [this.token, this.initialHolder],
  121. [-initialSupply, initialSupply],
  122. );
  123. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, -initialSupply);
  124. });
  125. it('to other account', async function () {
  126. const tx = await this.token.connect(this.initialHolder).withdrawTo(this.recipient, initialSupply);
  127. await expect(tx)
  128. .to.emit(this.underlying, 'Transfer')
  129. .withArgs(this.token.target, this.recipient.address, initialSupply)
  130. .to.emit(this.token, 'Transfer')
  131. .withArgs(this.initialHolder.address, ethers.ZeroAddress, initialSupply);
  132. await expect(tx).to.changeTokenBalances(
  133. this.underlying,
  134. [this.token, this.initialHolder, this.recipient],
  135. [-initialSupply, 0, initialSupply],
  136. );
  137. await expect(tx).to.changeTokenBalance(this.token, this.initialHolder, -initialSupply);
  138. });
  139. it('reverts withdrawing to the wrapper contract', async function () {
  140. await expect(this.token.connect(this.initialHolder).withdrawTo(this.token, initialSupply))
  141. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  142. .withArgs(this.token.target);
  143. });
  144. });
  145. describe('recover', function () {
  146. it('nothing to recover', async function () {
  147. await this.underlying.connect(this.initialHolder).approve(this.token, initialSupply);
  148. await this.token.connect(this.initialHolder).depositFor(this.initialHolder, initialSupply);
  149. const tx = await this.token.$_recover(this.recipient);
  150. await expect(tx).to.emit(this.token, 'Transfer').withArgs(ethers.ZeroAddress, this.recipient.address, 0n);
  151. await expect(tx).to.changeTokenBalance(this.token, this.recipient, 0);
  152. });
  153. it('something to recover', async function () {
  154. await this.underlying.connect(this.initialHolder).transfer(this.token, initialSupply);
  155. const tx = await this.token.$_recover(this.recipient);
  156. await expect(tx)
  157. .to.emit(this.token, 'Transfer')
  158. .withArgs(ethers.ZeroAddress, this.recipient.address, initialSupply);
  159. await expect(tx).to.changeTokenBalance(this.token, this.recipient, initialSupply);
  160. });
  161. });
  162. describe('erc20 behaviour', function () {
  163. beforeEach(async function () {
  164. await this.underlying.connect(this.initialHolder).approve(this.token, initialSupply);
  165. await this.token.connect(this.initialHolder).depositFor(this.initialHolder, initialSupply);
  166. });
  167. shouldBehaveLikeERC20(initialSupply);
  168. });
  169. });