SafeERC20.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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('safeApprove fails to update approval to non-zero', async function () {
  157. await expectRevert(
  158. this.mock.$safeApprove(this.token.address, spender, 200),
  159. 'SafeERC20: approve from non-zero to non-zero allowance',
  160. );
  161. });
  162. it('safeApprove can update approval to zero', async function () {
  163. await this.mock.$safeApprove(this.token.address, spender, 0);
  164. });
  165. it('safeApprove can increase approval', async function () {
  166. await expectRevert(this.mock.$safeIncreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
  167. });
  168. it('safeApprove can decrease approval', async function () {
  169. await expectRevert(this.mock.$safeDecreaseAllowance(this.token.address, spender, 10), 'USDT approval failure');
  170. });
  171. it('forceApprove works', async function () {
  172. await this.mock.$forceApprove(this.token.address, spender, 200);
  173. });
  174. });
  175. });
  176. });
  177. function shouldRevertOnAllCalls([receiver, spender], reason) {
  178. it('reverts on transfer', async function () {
  179. await expectRevert(this.mock.$safeTransfer(this.token.address, receiver, 0), reason);
  180. });
  181. it('reverts on transferFrom', async function () {
  182. await expectRevert(this.mock.$safeTransferFrom(this.token.address, this.mock.address, receiver, 0), reason);
  183. });
  184. it('reverts on approve', async function () {
  185. await expectRevert(this.mock.$safeApprove(this.token.address, spender, 0), reason);
  186. });
  187. it('reverts on increaseAllowance', async function () {
  188. // [TODO] make sure it's reverting for the right reason
  189. await expectRevert.unspecified(this.mock.$safeIncreaseAllowance(this.token.address, spender, 0));
  190. });
  191. it('reverts on decreaseAllowance', async function () {
  192. // [TODO] make sure it's reverting for the right reason
  193. await expectRevert.unspecified(this.mock.$safeDecreaseAllowance(this.token.address, spender, 0));
  194. });
  195. it('reverts on forceApprove', async function () {
  196. await expectRevert(this.mock.$forceApprove(this.token.address, spender, 0), reason);
  197. });
  198. }
  199. function shouldOnlyRevertOnErrors([owner, receiver, spender]) {
  200. describe('transfers', function () {
  201. beforeEach(async function () {
  202. await this.token.$_mint(owner, 100);
  203. await this.token.$_mint(this.mock.address, 100);
  204. await this.token.approve(this.mock.address, constants.MAX_UINT256, { from: owner });
  205. });
  206. it("doesn't revert on transfer", async function () {
  207. const { tx } = await this.mock.$safeTransfer(this.token.address, receiver, 10);
  208. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  209. from: this.mock.address,
  210. to: receiver,
  211. value: '10',
  212. });
  213. });
  214. it("doesn't revert on transferFrom", async function () {
  215. const { tx } = await this.mock.$safeTransferFrom(this.token.address, owner, receiver, 10);
  216. await expectEvent.inTransaction(tx, this.token, 'Transfer', {
  217. from: owner,
  218. to: receiver,
  219. value: '10',
  220. });
  221. });
  222. });
  223. describe('approvals', function () {
  224. context('with zero allowance', function () {
  225. beforeEach(async function () {
  226. await this.token.$_approve(this.mock.address, spender, 0);
  227. });
  228. it("doesn't revert when approving a non-zero allowance", async function () {
  229. await this.mock.$safeApprove(this.token.address, spender, 100);
  230. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('100');
  231. });
  232. it("doesn't revert when approving a zero allowance", async function () {
  233. await this.mock.$safeApprove(this.token.address, spender, 0);
  234. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
  235. });
  236. it("doesn't revert when force approving a non-zero allowance", async function () {
  237. await this.mock.$forceApprove(this.token.address, spender, 100);
  238. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('100');
  239. });
  240. it("doesn't revert when force approving a zero allowance", async function () {
  241. await this.mock.$forceApprove(this.token.address, spender, 0);
  242. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
  243. });
  244. it("doesn't revert when increasing the allowance", async function () {
  245. await this.mock.$safeIncreaseAllowance(this.token.address, spender, 10);
  246. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('10');
  247. });
  248. it('reverts when decreasing the allowance', async function () {
  249. await expectRevert(
  250. this.mock.$safeDecreaseAllowance(this.token.address, spender, 10),
  251. 'SafeERC20: decreased allowance below zero',
  252. );
  253. });
  254. });
  255. context('with non-zero allowance', function () {
  256. beforeEach(async function () {
  257. await this.token.$_approve(this.mock.address, spender, 100);
  258. });
  259. it('reverts when approving a non-zero allowance', async function () {
  260. await expectRevert(
  261. this.mock.$safeApprove(this.token.address, spender, 20),
  262. 'SafeERC20: approve from non-zero to non-zero allowance',
  263. );
  264. });
  265. it("doesn't revert when approving a zero allowance", async function () {
  266. await this.mock.$safeApprove(this.token.address, spender, 0);
  267. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
  268. });
  269. it("doesn't revert when force approving a non-zero allowance", async function () {
  270. await this.mock.$forceApprove(this.token.address, spender, 20);
  271. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('20');
  272. });
  273. it("doesn't revert when force approving a zero allowance", async function () {
  274. await this.mock.$forceApprove(this.token.address, spender, 0);
  275. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('0');
  276. });
  277. it("doesn't revert when increasing the allowance", async function () {
  278. await this.mock.$safeIncreaseAllowance(this.token.address, spender, 10);
  279. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('110');
  280. });
  281. it("doesn't revert when decreasing the allowance to a positive value", async function () {
  282. await this.mock.$safeDecreaseAllowance(this.token.address, spender, 50);
  283. expect(await this.token.allowance(this.mock.address, spender)).to.be.bignumber.equal('50');
  284. });
  285. it('reverts when decreasing the allowance to a negative value', async function () {
  286. await expectRevert(
  287. this.mock.$safeDecreaseAllowance(this.token.address, spender, 200),
  288. 'SafeERC20: decreased allowance below zero',
  289. );
  290. });
  291. });
  292. });
  293. }