SafeERC20.test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const name = 'ERC20Mock';
  5. const symbol = 'ERC20Mock';
  6. async function fixture() {
  7. const [hasNoCode, owner, receiver, spender] = await ethers.getSigners();
  8. const mock = await ethers.deployContract('$SafeERC20');
  9. const erc20ReturnFalseMock = await ethers.deployContract('$ERC20ReturnFalseMock', [name, symbol]);
  10. const erc20ReturnTrueMock = await ethers.deployContract('$ERC20', [name, symbol]); // default implementation returns true
  11. const erc20NoReturnMock = await ethers.deployContract('$ERC20NoReturnMock', [name, symbol]);
  12. const erc20ForceApproveMock = await ethers.deployContract('$ERC20ForceApproveMock', [name, symbol]);
  13. return {
  14. hasNoCode,
  15. owner,
  16. receiver,
  17. spender,
  18. mock,
  19. erc20ReturnFalseMock,
  20. erc20ReturnTrueMock,
  21. erc20NoReturnMock,
  22. erc20ForceApproveMock,
  23. };
  24. }
  25. describe('SafeERC20', function () {
  26. before(async function () {
  27. Object.assign(this, await loadFixture(fixture));
  28. });
  29. describe('with address that has no contract code', function () {
  30. beforeEach(async function () {
  31. this.token = this.hasNoCode;
  32. });
  33. it('reverts on transfer', async function () {
  34. await expect(this.mock.$safeTransfer(this.token, this.receiver, 0n))
  35. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  36. .withArgs(this.token);
  37. });
  38. it('reverts on transferFrom', async function () {
  39. await expect(this.mock.$safeTransferFrom(this.token, this.mock, this.receiver, 0n))
  40. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  41. .withArgs(this.token);
  42. });
  43. it('reverts on increaseAllowance', async function () {
  44. // Call to 'token.allowance' does not return any data, resulting in a decoding error (revert without reason)
  45. await expect(this.mock.$safeIncreaseAllowance(this.token, this.spender, 0n)).to.be.revertedWithoutReason();
  46. });
  47. it('reverts on decreaseAllowance', async function () {
  48. // Call to 'token.allowance' does not return any data, resulting in a decoding error (revert without reason)
  49. await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 0n)).to.be.revertedWithoutReason();
  50. });
  51. it('reverts on forceApprove', async function () {
  52. await expect(this.mock.$forceApprove(this.token, this.spender, 0n))
  53. .to.be.revertedWithCustomError(this.mock, 'AddressEmptyCode')
  54. .withArgs(this.token);
  55. });
  56. });
  57. describe('with token that returns false on all calls', function () {
  58. beforeEach(async function () {
  59. this.token = this.erc20ReturnFalseMock;
  60. });
  61. it('reverts on transfer', async function () {
  62. await expect(this.mock.$safeTransfer(this.token, this.receiver, 0n))
  63. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
  64. .withArgs(this.token);
  65. });
  66. it('reverts on transferFrom', async function () {
  67. await expect(this.mock.$safeTransferFrom(this.token, this.mock, this.receiver, 0n))
  68. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
  69. .withArgs(this.token);
  70. });
  71. it('reverts on increaseAllowance', async function () {
  72. await expect(this.mock.$safeIncreaseAllowance(this.token, this.spender, 0n))
  73. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
  74. .withArgs(this.token);
  75. });
  76. it('reverts on decreaseAllowance', async function () {
  77. await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 0n))
  78. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
  79. .withArgs(this.token);
  80. });
  81. it('reverts on forceApprove', async function () {
  82. await expect(this.mock.$forceApprove(this.token, this.spender, 0n))
  83. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedOperation')
  84. .withArgs(this.token);
  85. });
  86. });
  87. describe('with token that returns true on all calls', function () {
  88. beforeEach(async function () {
  89. this.token = this.erc20ReturnTrueMock;
  90. });
  91. shouldOnlyRevertOnErrors();
  92. });
  93. describe('with token that returns no boolean values', function () {
  94. beforeEach(async function () {
  95. this.token = this.erc20NoReturnMock;
  96. });
  97. shouldOnlyRevertOnErrors();
  98. });
  99. describe('with usdt approval beaviour', function () {
  100. beforeEach(async function () {
  101. this.token = this.erc20ForceApproveMock;
  102. });
  103. describe('with initial approval', function () {
  104. beforeEach(async function () {
  105. await this.token.$_approve(this.mock, this.spender, 100n);
  106. });
  107. it('safeIncreaseAllowance works', async function () {
  108. await this.mock.$safeIncreaseAllowance(this.token, this.spender, 10n);
  109. expect(await this.token.allowance(this.mock, this.spender)).to.equal(110n);
  110. });
  111. it('safeDecreaseAllowance works', async function () {
  112. await this.mock.$safeDecreaseAllowance(this.token, this.spender, 10n);
  113. expect(await this.token.allowance(this.mock, this.spender)).to.equal(90n);
  114. });
  115. it('forceApprove works', async function () {
  116. await this.mock.$forceApprove(this.token, this.spender, 200n);
  117. expect(await this.token.allowance(this.mock, this.spender)).to.equal(200n);
  118. });
  119. });
  120. });
  121. });
  122. function shouldOnlyRevertOnErrors() {
  123. describe('transfers', function () {
  124. beforeEach(async function () {
  125. await this.token.$_mint(this.owner, 100n);
  126. await this.token.$_mint(this.mock, 100n);
  127. await this.token.$_approve(this.owner, this.mock, ethers.MaxUint256);
  128. });
  129. it("doesn't revert on transfer", async function () {
  130. await expect(this.mock.$safeTransfer(this.token, this.receiver, 10n))
  131. .to.emit(this.token, 'Transfer')
  132. .withArgs(this.mock, this.receiver, 10n);
  133. });
  134. it("doesn't revert on transferFrom", async function () {
  135. await expect(this.mock.$safeTransferFrom(this.token, this.owner, this.receiver, 10n))
  136. .to.emit(this.token, 'Transfer')
  137. .withArgs(this.owner, this.receiver, 10n);
  138. });
  139. });
  140. describe('approvals', function () {
  141. context('with zero allowance', function () {
  142. beforeEach(async function () {
  143. await this.token.$_approve(this.mock, this.spender, 0n);
  144. });
  145. it("doesn't revert when force approving a non-zero allowance", async function () {
  146. await this.mock.$forceApprove(this.token, this.spender, 100n);
  147. expect(await this.token.allowance(this.mock, this.spender)).to.equal(100n);
  148. });
  149. it("doesn't revert when force approving a zero allowance", async function () {
  150. await this.mock.$forceApprove(this.token, this.spender, 0n);
  151. expect(await this.token.allowance(this.mock, this.spender)).to.equal(0n);
  152. });
  153. it("doesn't revert when increasing the allowance", async function () {
  154. await this.mock.$safeIncreaseAllowance(this.token, this.spender, 10n);
  155. expect(await this.token.allowance(this.mock, this.spender)).to.equal(10n);
  156. });
  157. it('reverts when decreasing the allowance', async function () {
  158. await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 10n))
  159. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedDecreaseAllowance')
  160. .withArgs(this.spender, 0n, 10n);
  161. });
  162. });
  163. context('with non-zero allowance', function () {
  164. beforeEach(async function () {
  165. await this.token.$_approve(this.mock, this.spender, 100n);
  166. });
  167. it("doesn't revert when force approving a non-zero allowance", async function () {
  168. await this.mock.$forceApprove(this.token, this.spender, 20n);
  169. expect(await this.token.allowance(this.mock, this.spender)).to.equal(20n);
  170. });
  171. it("doesn't revert when force approving a zero allowance", async function () {
  172. await this.mock.$forceApprove(this.token, this.spender, 0n);
  173. expect(await this.token.allowance(this.mock, this.spender)).to.equal(0n);
  174. });
  175. it("doesn't revert when increasing the allowance", async function () {
  176. await this.mock.$safeIncreaseAllowance(this.token, this.spender, 10n);
  177. expect(await this.token.allowance(this.mock, this.spender)).to.equal(110n);
  178. });
  179. it("doesn't revert when decreasing the allowance to a positive value", async function () {
  180. await this.mock.$safeDecreaseAllowance(this.token, this.spender, 50n);
  181. expect(await this.token.allowance(this.mock, this.spender)).to.equal(50n);
  182. });
  183. it('reverts when decreasing the allowance to a negative value', async function () {
  184. await expect(this.mock.$safeDecreaseAllowance(this.token, this.spender, 200n))
  185. .to.be.revertedWithCustomError(this.mock, 'SafeERC20FailedDecreaseAllowance')
  186. .withArgs(this.spender, 100n, 200n);
  187. });
  188. });
  189. });
  190. }