SafeERC20.test.js 11 KB

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