SafeERC20.test.js 8.7 KB

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