ERC20Pausable.test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. const { BN, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const ERC20PausableMock = artifacts.require('ERC20PausableMock');
  3. const { shouldBehaveLikePublicRole } = require('../../behavior/access/roles/PublicRole.behavior');
  4. contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherAccount, ...otherAccounts]) {
  5. const initialSupply = new BN(100);
  6. beforeEach(async function () {
  7. this.token = await ERC20PausableMock.new(pauser, initialSupply, { 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, initialSupply, { from: pauser });
  93. (await this.token.balanceOf(pauser)).should.be.bignumber.equal('0');
  94. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(initialSupply);
  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, initialSupply, { from: pauser });
  100. (await this.token.balanceOf(pauser)).should.be.bignumber.equal('0');
  101. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(initialSupply);
  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, initialSupply, { from: pauser }));
  106. });
  107. });
  108. describe('approve', function () {
  109. const allowance = new BN(40);
  110. it('allows to approve when unpaused', async function () {
  111. await this.token.approve(anotherAccount, allowance, { from: pauser });
  112. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance);
  113. });
  114. it('allows to approve when paused and then unpaused', async function () {
  115. await this.token.pause({ from: pauser });
  116. await this.token.unpause({ from: pauser });
  117. await this.token.approve(anotherAccount, allowance, { from: pauser });
  118. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance);
  119. });
  120. it('reverts when trying to approve when paused', async function () {
  121. await this.token.pause({ from: pauser });
  122. await shouldFail.reverting(this.token.approve(anotherAccount, allowance, { from: pauser }));
  123. });
  124. });
  125. describe('transfer from', function () {
  126. const allowance = new BN(40);
  127. beforeEach(async function () {
  128. await this.token.approve(anotherAccount, allowance, { from: pauser });
  129. });
  130. it('allows to transfer from when unpaused', async function () {
  131. await this.token.transferFrom(pauser, recipient, allowance, { from: anotherAccount });
  132. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(allowance);
  133. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(initialSupply.sub(allowance));
  134. });
  135. it('allows to transfer when paused and then unpaused', async function () {
  136. await this.token.pause({ from: pauser });
  137. await this.token.unpause({ from: pauser });
  138. await this.token.transferFrom(pauser, recipient, allowance, { from: anotherAccount });
  139. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(allowance);
  140. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(initialSupply.sub(allowance));
  141. });
  142. it('reverts when trying to transfer from when paused', async function () {
  143. await this.token.pause({ from: pauser });
  144. await shouldFail.reverting(this.token.transferFrom(pauser, recipient, allowance, { from: anotherAccount }));
  145. });
  146. });
  147. describe('decrease approval', function () {
  148. const allowance = new BN(40);
  149. const decrement = new BN(10);
  150. beforeEach(async function () {
  151. await this.token.approve(anotherAccount, allowance, { from: pauser });
  152. });
  153. it('allows to decrease approval when unpaused', async function () {
  154. await this.token.decreaseAllowance(anotherAccount, decrement, { from: pauser });
  155. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.sub(decrement));
  156. });
  157. it('allows to decrease approval when paused and then unpaused', async function () {
  158. await this.token.pause({ from: pauser });
  159. await this.token.unpause({ from: pauser });
  160. await this.token.decreaseAllowance(anotherAccount, decrement, { from: pauser });
  161. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.sub(decrement));
  162. });
  163. it('reverts when trying to transfer when paused', async function () {
  164. await this.token.pause({ from: pauser });
  165. await shouldFail.reverting(this.token.decreaseAllowance(anotherAccount, decrement, { from: pauser }));
  166. });
  167. });
  168. describe('increase approval', function () {
  169. const allowance = new BN(40);
  170. const increment = new BN(30);
  171. beforeEach(async function () {
  172. await this.token.approve(anotherAccount, allowance, { from: pauser });
  173. });
  174. it('allows to increase approval when unpaused', async function () {
  175. await this.token.increaseAllowance(anotherAccount, increment, { from: pauser });
  176. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.add(increment));
  177. });
  178. it('allows to increase approval when paused and then unpaused', async function () {
  179. await this.token.pause({ from: pauser });
  180. await this.token.unpause({ from: pauser });
  181. await this.token.increaseAllowance(anotherAccount, increment, { from: pauser });
  182. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.add(increment));
  183. });
  184. it('reverts when trying to increase approval when paused', async function () {
  185. await this.token.pause({ from: pauser });
  186. await shouldFail.reverting(this.token.increaseAllowance(anotherAccount, increment, { from: pauser }));
  187. });
  188. });
  189. });
  190. });