ERC20.test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. const { BN, constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const {
  4. shouldBehaveLikeERC20,
  5. shouldBehaveLikeERC20Approve,
  6. } = require('./ERC20.behavior');
  7. const ERC20Mock = artifacts.require('ERC20Mock');
  8. contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
  9. const initialSupply = new BN(100);
  10. beforeEach(async function () {
  11. this.token = await ERC20Mock.new(initialHolder, initialSupply);
  12. });
  13. shouldBehaveLikeERC20('ERC20', initialSupply, initialHolder, recipient, anotherAccount);
  14. describe('decrease allowance', function () {
  15. describe('when the spender is not the zero address', function () {
  16. const spender = recipient;
  17. function shouldDecreaseApproval (amount) {
  18. describe('when there was no approved amount before', function () {
  19. it('reverts', async function () {
  20. await shouldFail.reverting.withMessage(this.token.decreaseAllowance(
  21. spender, amount, { from: initialHolder }), 'SafeMath: subtraction overflow'
  22. );
  23. });
  24. });
  25. describe('when the spender had an approved amount', function () {
  26. const approvedAmount = amount;
  27. beforeEach(async function () {
  28. ({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: initialHolder }));
  29. });
  30. it('emits an approval event', async function () {
  31. const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  32. expectEvent.inLogs(logs, 'Approval', {
  33. owner: initialHolder,
  34. spender: spender,
  35. value: new BN(0),
  36. });
  37. });
  38. it('decreases the spender allowance subtracting the requested amount', async function () {
  39. await this.token.decreaseAllowance(spender, approvedAmount.subn(1), { from: initialHolder });
  40. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal('1');
  41. });
  42. it('sets the allowance to zero when all allowance is removed', async function () {
  43. await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  44. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal('0');
  45. });
  46. it('reverts when more than the full allowance is removed', async function () {
  47. await shouldFail.reverting.withMessage(
  48. this.token.decreaseAllowance(spender, approvedAmount.addn(1), { from: initialHolder }),
  49. 'SafeMath: subtraction overflow'
  50. );
  51. });
  52. });
  53. }
  54. describe('when the sender has enough balance', function () {
  55. const amount = initialSupply;
  56. shouldDecreaseApproval(amount);
  57. });
  58. describe('when the sender does not have enough balance', function () {
  59. const amount = initialSupply.addn(1);
  60. shouldDecreaseApproval(amount);
  61. });
  62. });
  63. describe('when the spender is the zero address', function () {
  64. const amount = initialSupply;
  65. const spender = ZERO_ADDRESS;
  66. it('reverts', async function () {
  67. await shouldFail.reverting.withMessage(this.token.decreaseAllowance(
  68. spender, amount, { from: initialHolder }, 'ERC20: approve to the zero address')
  69. );
  70. });
  71. });
  72. });
  73. describe('increase allowance', function () {
  74. const amount = initialSupply;
  75. describe('when the spender is not the zero address', function () {
  76. const spender = recipient;
  77. describe('when the sender has enough balance', function () {
  78. it('emits an approval event', async function () {
  79. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  80. expectEvent.inLogs(logs, 'Approval', {
  81. owner: initialHolder,
  82. spender: spender,
  83. value: amount,
  84. });
  85. });
  86. describe('when there was no approved amount before', function () {
  87. it('approves the requested amount', async function () {
  88. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  89. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount);
  90. });
  91. });
  92. describe('when the spender had an approved amount', function () {
  93. beforeEach(async function () {
  94. await this.token.approve(spender, new BN(1), { from: initialHolder });
  95. });
  96. it('increases the spender allowance adding the requested amount', async function () {
  97. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  98. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount.addn(1));
  99. });
  100. });
  101. });
  102. describe('when the sender does not have enough balance', function () {
  103. const amount = initialSupply.addn(1);
  104. it('emits an approval event', async function () {
  105. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  106. expectEvent.inLogs(logs, 'Approval', {
  107. owner: initialHolder,
  108. spender: spender,
  109. value: amount,
  110. });
  111. });
  112. describe('when there was no approved amount before', function () {
  113. it('approves the requested amount', async function () {
  114. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  115. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount);
  116. });
  117. });
  118. describe('when the spender had an approved amount', function () {
  119. beforeEach(async function () {
  120. await this.token.approve(spender, new BN(1), { from: initialHolder });
  121. });
  122. it('increases the spender allowance adding the requested amount', async function () {
  123. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  124. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(amount.addn(1));
  125. });
  126. });
  127. });
  128. });
  129. describe('when the spender is the zero address', function () {
  130. const spender = ZERO_ADDRESS;
  131. it('reverts', async function () {
  132. await shouldFail.reverting.withMessage(
  133. this.token.increaseAllowance(spender, amount, { from: initialHolder }), 'ERC20: approve to the zero address'
  134. );
  135. });
  136. });
  137. });
  138. describe('_mint', function () {
  139. const amount = new BN(50);
  140. it('rejects a null account', async function () {
  141. await shouldFail.reverting.withMessage(
  142. this.token.mint(ZERO_ADDRESS, amount), 'ERC20: mint to the zero address'
  143. );
  144. });
  145. describe('for a non zero account', function () {
  146. beforeEach('minting', async function () {
  147. const { logs } = await this.token.mint(recipient, amount);
  148. this.logs = logs;
  149. });
  150. it('increments totalSupply', async function () {
  151. const expectedSupply = initialSupply.add(amount);
  152. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  153. });
  154. it('increments recipient balance', async function () {
  155. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(amount);
  156. });
  157. it('emits Transfer event', async function () {
  158. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  159. from: ZERO_ADDRESS,
  160. to: recipient,
  161. });
  162. event.args.value.should.be.bignumber.equal(amount);
  163. });
  164. });
  165. });
  166. describe('_burn', function () {
  167. it('rejects a null account', async function () {
  168. await shouldFail.reverting.withMessage(this.token.burn(ZERO_ADDRESS, new BN(1)),
  169. 'ERC20: burn from the zero address');
  170. });
  171. describe('for a non zero account', function () {
  172. it('rejects burning more than balance', async function () {
  173. await shouldFail.reverting.withMessage(this.token.burn(
  174. initialHolder, initialSupply.addn(1)), 'SafeMath: subtraction overflow'
  175. );
  176. });
  177. const describeBurn = function (description, amount) {
  178. describe(description, function () {
  179. beforeEach('burning', async function () {
  180. const { logs } = await this.token.burn(initialHolder, amount);
  181. this.logs = logs;
  182. });
  183. it('decrements totalSupply', async function () {
  184. const expectedSupply = initialSupply.sub(amount);
  185. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  186. });
  187. it('decrements initialHolder balance', async function () {
  188. const expectedBalance = initialSupply.sub(amount);
  189. (await this.token.balanceOf(initialHolder)).should.be.bignumber.equal(expectedBalance);
  190. });
  191. it('emits Transfer event', async function () {
  192. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  193. from: initialHolder,
  194. to: ZERO_ADDRESS,
  195. });
  196. event.args.value.should.be.bignumber.equal(amount);
  197. });
  198. });
  199. };
  200. describeBurn('for entire balance', initialSupply);
  201. describeBurn('for less amount than balance', initialSupply.subn(1));
  202. });
  203. });
  204. describe('_burnFrom', function () {
  205. const allowance = new BN(70);
  206. const spender = anotherAccount;
  207. beforeEach('approving', async function () {
  208. await this.token.approve(spender, allowance, { from: initialHolder });
  209. });
  210. it('rejects a null account', async function () {
  211. await shouldFail.reverting.withMessage(this.token.burnFrom(ZERO_ADDRESS, new BN(1)),
  212. 'ERC20: burn from the zero address'
  213. );
  214. });
  215. describe('for a non zero account', function () {
  216. it('rejects burning more than allowance', async function () {
  217. await shouldFail.reverting.withMessage(this.token.burnFrom(initialHolder, allowance.addn(1)),
  218. 'SafeMath: subtraction overflow'
  219. );
  220. });
  221. it('rejects burning more than balance', async function () {
  222. await shouldFail.reverting.withMessage(this.token.burnFrom(initialHolder, initialSupply.addn(1)),
  223. 'SafeMath: subtraction overflow'
  224. );
  225. });
  226. const describeBurnFrom = function (description, amount) {
  227. describe(description, function () {
  228. beforeEach('burning', async function () {
  229. const { logs } = await this.token.burnFrom(initialHolder, amount, { from: spender });
  230. this.logs = logs;
  231. });
  232. it('decrements totalSupply', async function () {
  233. const expectedSupply = initialSupply.sub(amount);
  234. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  235. });
  236. it('decrements initialHolder balance', async function () {
  237. const expectedBalance = initialSupply.sub(amount);
  238. (await this.token.balanceOf(initialHolder)).should.be.bignumber.equal(expectedBalance);
  239. });
  240. it('decrements spender allowance', async function () {
  241. const expectedAllowance = allowance.sub(amount);
  242. (await this.token.allowance(initialHolder, spender)).should.be.bignumber.equal(expectedAllowance);
  243. });
  244. it('emits a Transfer event', async function () {
  245. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  246. from: initialHolder,
  247. to: ZERO_ADDRESS,
  248. });
  249. event.args.value.should.be.bignumber.equal(amount);
  250. });
  251. it('emits an Approval event', async function () {
  252. expectEvent.inLogs(this.logs, 'Approval', {
  253. owner: initialHolder,
  254. spender: spender,
  255. value: await this.token.allowance(initialHolder, spender),
  256. });
  257. });
  258. });
  259. };
  260. describeBurnFrom('for entire allowance', allowance);
  261. describeBurnFrom('for less amount than allowance', allowance.subn(1));
  262. });
  263. });
  264. describe('_approve', function () {
  265. shouldBehaveLikeERC20Approve('ERC20', initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  266. return this.token.approveInternal(owner, spender, amount);
  267. });
  268. describe('when the owner is the zero address', function () {
  269. it('reverts', async function () {
  270. await shouldFail.reverting.withMessage(this.token.approveInternal(ZERO_ADDRESS, recipient, initialSupply),
  271. 'ERC20: approve from the zero address'
  272. );
  273. });
  274. });
  275. });
  276. });