ERC20Pausable.test.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. const expectEvent = require('../../helpers/expectEvent');
  2. const shouldFail = require('../../helpers/shouldFail');
  3. const ERC20PausableMock = artifacts.require('ERC20PausableMock');
  4. const { shouldBehaveLikePublicRole } = require('../../access/roles/PublicRole.behavior');
  5. contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherAccount, ...otherAccounts]) {
  6. beforeEach(async function () {
  7. this.token = await ERC20PausableMock.new(pauser, 100, { from: pauser });
  8. });
  9. describe('pauser role', function () {
  10. beforeEach(async function () {
  11. this.contract = this.token;
  12. await this.contract.addPauser(otherPauser, { from: pauser });
  13. });
  14. shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser');
  15. });
  16. describe('pause', function () {
  17. describe('when the sender is the token pauser', function () {
  18. const from = pauser;
  19. describe('when the token is unpaused', function () {
  20. it('pauses the token', async function () {
  21. await this.token.pause({ from });
  22. (await this.token.paused()).should.equal(true);
  23. });
  24. it('emits a Pause event', async function () {
  25. const { logs } = await this.token.pause({ from });
  26. expectEvent.inLogs(logs, 'Paused');
  27. });
  28. });
  29. describe('when the token is paused', function () {
  30. beforeEach(async function () {
  31. await this.token.pause({ from });
  32. });
  33. it('reverts', async function () {
  34. await shouldFail.reverting(this.token.pause({ from }));
  35. });
  36. });
  37. });
  38. describe('when the sender is not the token pauser', function () {
  39. const from = anotherAccount;
  40. it('reverts', async function () {
  41. await shouldFail.reverting(this.token.pause({ from }));
  42. });
  43. });
  44. });
  45. describe('unpause', function () {
  46. describe('when the sender is the token pauser', function () {
  47. const from = pauser;
  48. describe('when the token is paused', function () {
  49. beforeEach(async function () {
  50. await this.token.pause({ from });
  51. });
  52. it('unpauses the token', async function () {
  53. await this.token.unpause({ from });
  54. (await this.token.paused()).should.equal(false);
  55. });
  56. it('emits an Unpause event', async function () {
  57. const { logs } = await this.token.unpause({ from });
  58. expectEvent.inLogs(logs, 'Unpaused');
  59. });
  60. });
  61. describe('when the token is unpaused', function () {
  62. it('reverts', async function () {
  63. await shouldFail.reverting(this.token.unpause({ from }));
  64. });
  65. });
  66. });
  67. describe('when the sender is not the token pauser', function () {
  68. const from = anotherAccount;
  69. it('reverts', async function () {
  70. await shouldFail.reverting(this.token.unpause({ from }));
  71. });
  72. });
  73. });
  74. describe('pausable token', function () {
  75. const from = pauser;
  76. describe('paused', function () {
  77. it('is not paused by default', async function () {
  78. (await this.token.paused({ from })).should.equal(false);
  79. });
  80. it('is paused after being paused', async function () {
  81. await this.token.pause({ from });
  82. (await this.token.paused({ from })).should.equal(true);
  83. });
  84. it('is not paused after being paused and then unpaused', async function () {
  85. await this.token.pause({ from });
  86. await this.token.unpause({ from });
  87. (await this.token.paused()).should.equal(false);
  88. });
  89. });
  90. describe('transfer', function () {
  91. it('allows to transfer when unpaused', async function () {
  92. await this.token.transfer(recipient, 100, { from: pauser });
  93. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(0);
  94. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(100);
  95. });
  96. it('allows to transfer when paused and then unpaused', async function () {
  97. await this.token.pause({ from: pauser });
  98. await this.token.unpause({ from: pauser });
  99. await this.token.transfer(recipient, 100, { from: pauser });
  100. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(0);
  101. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(100);
  102. });
  103. it('reverts when trying to transfer when paused', async function () {
  104. await this.token.pause({ from: pauser });
  105. await shouldFail.reverting(this.token.transfer(recipient, 100, { from: pauser }));
  106. });
  107. });
  108. describe('approve', function () {
  109. it('allows to approve when unpaused', async function () {
  110. await this.token.approve(anotherAccount, 40, { from: pauser });
  111. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(40);
  112. });
  113. it('allows to transfer when paused and then unpaused', async function () {
  114. await this.token.pause({ from: pauser });
  115. await this.token.unpause({ from: pauser });
  116. await this.token.approve(anotherAccount, 40, { from: pauser });
  117. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(40);
  118. });
  119. it('reverts when trying to transfer when paused', async function () {
  120. await this.token.pause({ from: pauser });
  121. await shouldFail.reverting(this.token.approve(anotherAccount, 40, { from: pauser }));
  122. });
  123. });
  124. describe('transfer from', function () {
  125. beforeEach(async function () {
  126. await this.token.approve(anotherAccount, 50, { from: pauser });
  127. });
  128. it('allows to transfer from when unpaused', async function () {
  129. await this.token.transferFrom(pauser, recipient, 40, { from: anotherAccount });
  130. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(60);
  131. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(40);
  132. });
  133. it('allows to transfer when paused and then unpaused', async function () {
  134. await this.token.pause({ from: pauser });
  135. await this.token.unpause({ from: pauser });
  136. await this.token.transferFrom(pauser, recipient, 40, { from: anotherAccount });
  137. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(60);
  138. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(40);
  139. });
  140. it('reverts when trying to transfer from when paused', async function () {
  141. await this.token.pause({ from: pauser });
  142. await shouldFail.reverting(this.token.transferFrom(pauser, recipient, 40, { from: anotherAccount }));
  143. });
  144. });
  145. describe('decrease approval', function () {
  146. beforeEach(async function () {
  147. await this.token.approve(anotherAccount, 100, { from: pauser });
  148. });
  149. it('allows to decrease approval when unpaused', async function () {
  150. await this.token.decreaseAllowance(anotherAccount, 40, { from: pauser });
  151. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60);
  152. });
  153. it('allows to decrease approval when paused and then unpaused', async function () {
  154. await this.token.pause({ from: pauser });
  155. await this.token.unpause({ from: pauser });
  156. await this.token.decreaseAllowance(anotherAccount, 40, { from: pauser });
  157. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(60);
  158. });
  159. it('reverts when trying to transfer when paused', async function () {
  160. await this.token.pause({ from: pauser });
  161. await shouldFail.reverting(this.token.decreaseAllowance(anotherAccount, 40, { from: pauser }));
  162. });
  163. });
  164. describe('increase approval', function () {
  165. beforeEach(async function () {
  166. await this.token.approve(anotherAccount, 100, { from: pauser });
  167. });
  168. it('allows to increase approval when unpaused', async function () {
  169. await this.token.increaseAllowance(anotherAccount, 40, { from: pauser });
  170. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140);
  171. });
  172. it('allows to increase approval when paused and then unpaused', async function () {
  173. await this.token.pause({ from: pauser });
  174. await this.token.unpause({ from: pauser });
  175. await this.token.increaseAllowance(anotherAccount, 40, { from: pauser });
  176. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(140);
  177. });
  178. it('reverts when trying to increase approval when paused', async function () {
  179. await this.token.pause({ from: pauser });
  180. await shouldFail.reverting(this.token.increaseAllowance(anotherAccount, 40, { from: pauser }));
  181. });
  182. });
  183. });
  184. });