ERC20.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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(this.token.decreaseAllowance(
  44. spender, amount, { from: initialHolder }), 'ERC20: decreased allowance below zero',
  45. );
  46. });
  47. });
  48. describe('when the spender had an approved amount', function () {
  49. const approvedAmount = amount;
  50. beforeEach(async function () {
  51. await this.token.approve(spender, approvedAmount, { from: initialHolder });
  52. });
  53. it('emits an approval event', async function () {
  54. expectEvent(
  55. await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder }),
  56. 'Approval',
  57. { owner: initialHolder, spender: spender, value: new BN(0) },
  58. );
  59. });
  60. it('decreases the spender allowance subtracting the requested amount', async function () {
  61. await this.token.decreaseAllowance(spender, approvedAmount.subn(1), { from: initialHolder });
  62. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal('1');
  63. });
  64. it('sets the allowance to zero when all allowance is removed', async function () {
  65. await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  66. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal('0');
  67. });
  68. it('reverts when more than the full allowance is removed', async function () {
  69. await expectRevert(
  70. this.token.decreaseAllowance(spender, approvedAmount.addn(1), { from: initialHolder }),
  71. 'ERC20: decreased allowance below zero',
  72. );
  73. });
  74. });
  75. }
  76. describe('when the sender has enough balance', function () {
  77. const amount = initialSupply;
  78. shouldDecreaseApproval(amount);
  79. });
  80. describe('when the sender does not have enough balance', function () {
  81. const amount = initialSupply.addn(1);
  82. shouldDecreaseApproval(amount);
  83. });
  84. });
  85. describe('when the spender is the zero address', function () {
  86. const amount = initialSupply;
  87. const spender = ZERO_ADDRESS;
  88. it('reverts', async function () {
  89. await expectRevert(this.token.decreaseAllowance(
  90. spender, amount, { from: initialHolder }), 'ERC20: decreased allowance below zero',
  91. );
  92. });
  93. });
  94. });
  95. describe('increase allowance', function () {
  96. const amount = initialSupply;
  97. describe('when the spender is not the zero address', function () {
  98. const spender = recipient;
  99. describe('when the sender has enough balance', function () {
  100. it('emits an approval event', async function () {
  101. expectEvent(
  102. await this.token.increaseAllowance(spender, amount, { from: initialHolder }),
  103. 'Approval',
  104. { owner: initialHolder, spender: spender, value: amount },
  105. );
  106. });
  107. describe('when there was no approved amount before', function () {
  108. it('approves the requested amount', async function () {
  109. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  110. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  111. });
  112. });
  113. describe('when the spender had an approved amount', function () {
  114. beforeEach(async function () {
  115. await this.token.approve(spender, new BN(1), { from: initialHolder });
  116. });
  117. it('increases the spender allowance adding the requested amount', async function () {
  118. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  119. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  120. });
  121. });
  122. });
  123. describe('when the sender does not have enough balance', function () {
  124. const amount = initialSupply.addn(1);
  125. it('emits an approval event', async function () {
  126. expectEvent(
  127. await this.token.increaseAllowance(spender, amount, { from: initialHolder }),
  128. 'Approval',
  129. { owner: initialHolder, spender: spender, value: amount },
  130. );
  131. });
  132. describe('when there was no approved amount before', function () {
  133. it('approves the requested amount', async function () {
  134. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  135. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  136. });
  137. });
  138. describe('when the spender had an approved amount', function () {
  139. beforeEach(async function () {
  140. await this.token.approve(spender, new BN(1), { from: initialHolder });
  141. });
  142. it('increases the spender allowance adding the requested amount', async function () {
  143. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  144. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  145. });
  146. });
  147. });
  148. });
  149. describe('when the spender is the zero address', function () {
  150. const spender = ZERO_ADDRESS;
  151. it('reverts', async function () {
  152. await expectRevert(
  153. this.token.increaseAllowance(spender, amount, { from: initialHolder }), 'ERC20: approve to the zero address',
  154. );
  155. });
  156. });
  157. });
  158. describe('_mint', function () {
  159. const amount = new BN(50);
  160. it('rejects a null account', async function () {
  161. await expectRevert(
  162. this.token.$_mint(ZERO_ADDRESS, amount), 'ERC20: mint to the zero address',
  163. );
  164. });
  165. describe('for a non zero account', function () {
  166. beforeEach('minting', async function () {
  167. this.receipt = await this.token.$_mint(recipient, amount);
  168. });
  169. it('increments totalSupply', async function () {
  170. const expectedSupply = initialSupply.add(amount);
  171. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  172. });
  173. it('increments recipient balance', async function () {
  174. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  175. });
  176. it('emits Transfer event', async function () {
  177. const event = expectEvent(
  178. this.receipt,
  179. 'Transfer',
  180. { from: ZERO_ADDRESS, to: recipient },
  181. );
  182. expect(event.args.value).to.be.bignumber.equal(amount);
  183. });
  184. });
  185. });
  186. describe('_burn', function () {
  187. it('rejects a null account', async function () {
  188. await expectRevert(this.token.$_burn(ZERO_ADDRESS, new BN(1)),
  189. 'ERC20: burn from the zero address');
  190. });
  191. describe('for a non zero account', function () {
  192. it('rejects burning more than balance', async function () {
  193. await expectRevert(this.token.$_burn(
  194. initialHolder, initialSupply.addn(1)), 'ERC20: burn amount exceeds balance',
  195. );
  196. });
  197. const describeBurn = function (description, amount) {
  198. describe(description, function () {
  199. beforeEach('burning', async function () {
  200. this.receipt = await this.token.$_burn(initialHolder, amount);
  201. });
  202. it('decrements totalSupply', async function () {
  203. const expectedSupply = initialSupply.sub(amount);
  204. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  205. });
  206. it('decrements initialHolder balance', async function () {
  207. const expectedBalance = initialSupply.sub(amount);
  208. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  209. });
  210. it('emits Transfer event', async function () {
  211. const event = expectEvent(
  212. this.receipt,
  213. 'Transfer',
  214. { from: initialHolder, to: ZERO_ADDRESS },
  215. );
  216. expect(event.args.value).to.be.bignumber.equal(amount);
  217. });
  218. });
  219. };
  220. describeBurn('for entire balance', initialSupply);
  221. describeBurn('for less amount than balance', initialSupply.subn(1));
  222. });
  223. });
  224. describe('_transfer', function () {
  225. shouldBehaveLikeERC20Transfer('ERC20', initialHolder, recipient, initialSupply, function (from, to, amount) {
  226. return this.token.$_transfer(from, to, amount);
  227. });
  228. describe('when the sender is the zero address', function () {
  229. it('reverts', async function () {
  230. await expectRevert(this.token.$_transfer(ZERO_ADDRESS, recipient, initialSupply),
  231. 'ERC20: transfer from the zero address',
  232. );
  233. });
  234. });
  235. });
  236. describe('_approve', function () {
  237. shouldBehaveLikeERC20Approve('ERC20', initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  238. return this.token.$_approve(owner, spender, amount);
  239. });
  240. describe('when the owner is the zero address', function () {
  241. it('reverts', async function () {
  242. await expectRevert(this.token.$_approve(ZERO_ADDRESS, recipient, initialSupply),
  243. 'ERC20: approve from the zero address',
  244. );
  245. });
  246. });
  247. });
  248. });