SafeERC20.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const { shouldFail } = require('openzeppelin-test-helpers');
  2. const ERC20ReturnFalseMock = artifacts.require('ERC20ReturnFalseMock');
  3. const ERC20ReturnTrueMock = artifacts.require('ERC20ReturnTrueMock');
  4. const ERC20NoReturnMock = artifacts.require('ERC20NoReturnMock');
  5. const SafeERC20Wrapper = artifacts.require('SafeERC20Wrapper');
  6. contract('SafeERC20', function () {
  7. describe('with token that returns false on all calls', function () {
  8. beforeEach(async function () {
  9. this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnFalseMock.new()).address);
  10. });
  11. it('reverts on transfer', async function () {
  12. await shouldFail.reverting(this.wrapper.transfer());
  13. });
  14. it('reverts on transferFrom', async function () {
  15. await shouldFail.reverting(this.wrapper.transferFrom());
  16. });
  17. it('reverts on approve', async function () {
  18. await shouldFail.reverting(this.wrapper.approve(0));
  19. });
  20. it('reverts on increaseAllowance', async function () {
  21. await shouldFail.reverting(this.wrapper.increaseAllowance(0));
  22. });
  23. it('reverts on decreaseAllowance', async function () {
  24. await shouldFail.reverting(this.wrapper.decreaseAllowance(0));
  25. });
  26. });
  27. describe('with token that returns true on all calls', function () {
  28. beforeEach(async function () {
  29. this.wrapper = await SafeERC20Wrapper.new((await ERC20ReturnTrueMock.new()).address);
  30. });
  31. shouldOnlyRevertOnErrors();
  32. });
  33. describe('with token that returns no boolean values', function () {
  34. beforeEach(async function () {
  35. this.wrapper = await SafeERC20Wrapper.new((await ERC20NoReturnMock.new()).address);
  36. });
  37. shouldOnlyRevertOnErrors();
  38. });
  39. });
  40. function shouldOnlyRevertOnErrors () {
  41. it('doesn\'t revert on transfer', async function () {
  42. await this.wrapper.transfer();
  43. });
  44. it('doesn\'t revert on transferFrom', async function () {
  45. await this.wrapper.transferFrom();
  46. });
  47. describe('approvals', function () {
  48. context('with zero allowance', function () {
  49. beforeEach(async function () {
  50. await this.wrapper.setAllowance(0);
  51. });
  52. it('doesn\'t revert when approving a non-zero allowance', async function () {
  53. await this.wrapper.approve(100);
  54. });
  55. it('doesn\'t revert when approving a zero allowance', async function () {
  56. await this.wrapper.approve(0);
  57. });
  58. it('doesn\'t revert when increasing the allowance', async function () {
  59. await this.wrapper.increaseAllowance(10);
  60. });
  61. it('reverts when decreasing the allowance', async function () {
  62. await shouldFail.reverting(this.wrapper.decreaseAllowance(10));
  63. });
  64. });
  65. context('with non-zero allowance', function () {
  66. beforeEach(async function () {
  67. await this.wrapper.setAllowance(100);
  68. });
  69. it('reverts when approving a non-zero allowance', async function () {
  70. await shouldFail.reverting(this.wrapper.approve(20));
  71. });
  72. it('doesn\'t revert when approving a zero allowance', async function () {
  73. await this.wrapper.approve(0);
  74. });
  75. it('doesn\'t revert when increasing the allowance', async function () {
  76. await this.wrapper.increaseAllowance(10);
  77. });
  78. it('doesn\'t revert when decreasing the allowance to a positive value', async function () {
  79. await this.wrapper.decreaseAllowance(50);
  80. });
  81. it('reverts when decreasing the allowance to a negative value', async function () {
  82. await shouldFail.reverting(this.wrapper.decreaseAllowance(200));
  83. });
  84. });
  85. });
  86. }