ERC20Pausable.test.js 8.5 KB

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