ERC20Pausable.test.js 9.5 KB

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