ERC20.test.js 11 KB

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