ERC20.test.js 11 KB

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