SafeERC20.test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. const { constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const SafeERC20 = artifacts.require('$SafeERC20');
  3. const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock');
  4. const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
  5. const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
  6. const ERC20PermitNoRevertMock = artifacts.require('ERC20PermitNoRevertMock');
  7. const { getDomain, domainType, Permit } = require('../../../helpers/eip712');
  8. const { fromRpcSig } = require('ethereumjs-util');
  9. const ethSigUtil = require('eth-sig-util');
  10. const Wallet = require('ethereumjs-wallet').default;
  11. contract('SafeERC20', function (accounts) {
  12. const [hasNoCode] = accounts;
  13. before(async function () {
  14. this.mock = await SafeERC20.new();
  15. });
  16. describe('with address that has no contract code', function () {
  17. beforeEach(async function () {
  18. this.token = { address: hasNoCode };
  19. });
  20. shouldRevertOnAllCalls('Address: call to non-contract');
  21. });
  22. describe('with token that returns false on all calls', function () {
  23. beforeEach(async function () {
  24. this.token = await ERC20ReturnFalseMock.new();
  25. });
  26. shouldRevertOnAllCalls('SafeERC20: ERC20 operation did not succeed');
  27. });
  28. describe('with token that returns true on all calls', function () {
  29. beforeEach(async function () {
  30. this.token = await ERC20ReturnTrueMock.new();
  31. });
  32. shouldOnlyRevertOnErrors();
  33. });
  34. describe('with token that returns no boolean values', function () {
  35. beforeEach(async function () {
  36. this.token = await ERC20NoReturnMock.new();
  37. });
  38. shouldOnlyRevertOnErrors();
  39. });
  40. describe("with token that doesn't revert on invalid permit", function () {
  41. const wallet = Wallet.generate();
  42. const owner = wallet.getAddressString();
  43. const spender = hasNoCode;
  44. beforeEach(async function () {
  45. this.token = await ERC20PermitNoRevertMock.new();
  46. this.data = await getDomain(this.token).then(domain => ({
  47. primaryType: 'Permit',
  48. types: { EIP712Domain: domainType(domain), Permit },
  49. domain,
  50. message: { owner, spender, value: '42', nonce: '0', deadline: constants.MAX_UINT256 },
  51. }));
  52. this.signature = fromRpcSig(ethSigUtil.signTypedMessage(wallet.getPrivateKey(), { data: this.data }));
  53. });
  54. it('accepts owner signature', async function () {
  55. expect(await this.token.nonces(owner)).to.be.bignumber.equal('0');
  56. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal('0');
  57. await this.mock.$safePermit(
  58. this.token.address,
  59. this.data.message.owner,
  60. this.data.message.spender,
  61. this.data.message.value,
  62. this.data.message.deadline,
  63. this.signature.v,
  64. this.signature.r,
  65. this.signature.s,
  66. );
  67. expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
  68. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(this.data.message.value);
  69. });
  70. it('revert on reused signature', async function () {
  71. expect(await this.token.nonces(owner)).to.be.bignumber.equal('0');
  72. // use valid signature and consume nounce
  73. await this.mock.$safePermit(
  74. this.token.address,
  75. this.data.message.owner,
  76. this.data.message.spender,
  77. this.data.message.value,
  78. this.data.message.deadline,
  79. this.signature.v,
  80. this.signature.r,
  81. this.signature.s,
  82. );
  83. expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
  84. // invalid call does not revert for this token implementation
  85. await this.token.permit(
  86. this.data.message.owner,
  87. this.data.message.spender,
  88. this.data.message.value,
  89. this.data.message.deadline,
  90. this.signature.v,
  91. this.signature.r,
  92. this.signature.s,
  93. );
  94. expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
  95. // invalid call revert when called through the SafeERC20 library
  96. await expectRevert(
  97. this.mock.$safePermit(
  98. this.token.address,
  99. this.data.message.owner,
  100. this.data.message.spender,
  101. this.data.message.value,
  102. this.data.message.deadline,
  103. this.signature.v,
  104. this.signature.r,
  105. this.signature.s,
  106. ),
  107. 'SafeERC20: permit did not succeed',
  108. );
  109. expect(await this.token.nonces(owner)).to.be.bignumber.equal('1');
  110. });
  111. it('revert on invalid signature', async function () {
  112. // signature that is not valid for owner
  113. const invalidSignature = {
  114. v: 27,
  115. r: '0x71753dc5ecb5b4bfc0e3bc530d79ce5988760ed3f3a234c86a5546491f540775',
  116. s: '0x0049cedee5aed990aabed5ad6a9f6e3c565b63379894b5fa8b512eb2b79e485d',
  117. };
  118. // invalid call does not revert for this token implementation
  119. await this.token.permit(
  120. this.data.message.owner,
  121. this.data.message.spender,
  122. this.data.message.value,
  123. this.data.message.deadline,
  124. invalidSignature.v,
  125. invalidSignature.r,
  126. invalidSignature.s,
  127. );
  128. // invalid call revert when called through the SafeERC20 library
  129. await expectRevert(
  130. this.mock.$safePermit(
  131. this.token.address,
  132. this.data.message.owner,
  133. this.data.message.spender,
  134. this.data.message.value,
  135. this.data.message.deadline,
  136. invalidSignature.v,
  137. invalidSignature.r,
  138. invalidSignature.s,
  139. ),
  140. 'SafeERC20: permit did not succeed',
  141. );
  142. });
  143. });
  144. });
  145. function shouldRevertOnAllCalls(reason) {
  146. it('reverts on transfer', async function () {
  147. await expectRevert(this.mock.$safeTransfer(this.token.address, constants.ZERO_ADDRESS, 0), reason);
  148. });
  149. it('reverts on transferFrom', async function () {
  150. await expectRevert(
  151. this.mock.$safeTransferFrom(this.token.address, this.mock.address, constants.ZERO_ADDRESS, 0),
  152. reason,
  153. );
  154. });
  155. it('reverts on approve', async function () {
  156. await expectRevert(this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0), reason);
  157. });
  158. it('reverts on increaseAllowance', async function () {
  159. // [TODO] make sure it's reverting for the right reason
  160. await expectRevert.unspecified(this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 0));
  161. });
  162. it('reverts on decreaseAllowance', async function () {
  163. // [TODO] make sure it's reverting for the right reason
  164. await expectRevert.unspecified(this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 0));
  165. });
  166. }
  167. function shouldOnlyRevertOnErrors() {
  168. it("doesn't revert on transfer", async function () {
  169. await this.mock.$safeTransfer(this.token.address, constants.ZERO_ADDRESS, 0);
  170. });
  171. it("doesn't revert on transferFrom", async function () {
  172. await this.mock.$safeTransferFrom(this.token.address, this.mock.address, constants.ZERO_ADDRESS, 0);
  173. });
  174. describe('approvals', function () {
  175. context('with zero allowance', function () {
  176. beforeEach(async function () {
  177. await this.token.setAllowance(this.mock.address, 0);
  178. });
  179. it("doesn't revert when approving a non-zero allowance", async function () {
  180. await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 100);
  181. });
  182. it("doesn't revert when approving a zero allowance", async function () {
  183. await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0);
  184. });
  185. it("doesn't revert when increasing the allowance", async function () {
  186. await this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10);
  187. });
  188. it('reverts when decreasing the allowance', async function () {
  189. await expectRevert(
  190. this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10),
  191. 'SafeERC20: decreased allowance below zero',
  192. );
  193. });
  194. });
  195. context('with non-zero allowance', function () {
  196. beforeEach(async function () {
  197. await this.token.setAllowance(this.mock.address, 100);
  198. });
  199. it('reverts when approving a non-zero allowance', async function () {
  200. await expectRevert(
  201. this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 20),
  202. 'SafeERC20: approve from non-zero to non-zero allowance',
  203. );
  204. });
  205. it("doesn't revert when approving a zero allowance", async function () {
  206. await this.mock.$safeApprove(this.token.address, constants.ZERO_ADDRESS, 0);
  207. });
  208. it("doesn't revert when increasing the allowance", async function () {
  209. await this.mock.$safeIncreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 10);
  210. });
  211. it("doesn't revert when decreasing the allowance to a positive value", async function () {
  212. await this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 50);
  213. });
  214. it('reverts when decreasing the allowance to a negative value', async function () {
  215. await expectRevert(
  216. this.mock.$safeDecreaseAllowance(this.token.address, constants.ZERO_ADDRESS, 200),
  217. 'SafeERC20: decreased allowance below zero',
  218. );
  219. });
  220. });
  221. });
  222. }