ERC20.test.js 11 KB

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