ERC20Pausable.test.js 9.6 KB

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