ERC20.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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('_setupDecimals', 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. ({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: initialHolder }));
  51. });
  52. it('emits an approval event', async function () {
  53. const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  54. expectEvent.inLogs(logs, 'Approval', {
  55. owner: initialHolder,
  56. spender: spender,
  57. 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. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  102. expectEvent.inLogs(logs, 'Approval', {
  103. owner: initialHolder,
  104. spender: spender,
  105. value: amount,
  106. });
  107. });
  108. describe('when there was no approved amount before', function () {
  109. it('approves the requested amount', async function () {
  110. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  111. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  112. });
  113. });
  114. describe('when the spender had an approved amount', function () {
  115. beforeEach(async function () {
  116. await this.token.approve(spender, new BN(1), { from: initialHolder });
  117. });
  118. it('increases the spender allowance adding the requested amount', async function () {
  119. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  120. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  121. });
  122. });
  123. });
  124. describe('when the sender does not have enough balance', function () {
  125. const amount = initialSupply.addn(1);
  126. it('emits an approval event', async function () {
  127. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  128. expectEvent.inLogs(logs, '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 }), 'ERC20: approve to the zero address',
  156. );
  157. });
  158. });
  159. });
  160. describe('_mint', function () {
  161. const amount = new BN(50);
  162. it('rejects a null account', async function () {
  163. await expectRevert(
  164. this.token.mint(ZERO_ADDRESS, amount), 'ERC20: mint to the zero address',
  165. );
  166. });
  167. describe('for a non zero account', function () {
  168. beforeEach('minting', async function () {
  169. const { logs } = await this.token.mint(recipient, amount);
  170. this.logs = logs;
  171. });
  172. it('increments totalSupply', async function () {
  173. const expectedSupply = initialSupply.add(amount);
  174. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  175. });
  176. it('increments recipient balance', async function () {
  177. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  178. });
  179. it('emits Transfer event', async function () {
  180. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  181. from: ZERO_ADDRESS,
  182. to: recipient,
  183. });
  184. expect(event.args.value).to.be.bignumber.equal(amount);
  185. });
  186. });
  187. });
  188. describe('_burn', function () {
  189. it('rejects a null account', async function () {
  190. await expectRevert(this.token.burn(ZERO_ADDRESS, new BN(1)),
  191. 'ERC20: burn from the zero address');
  192. });
  193. describe('for a non zero account', function () {
  194. it('rejects burning more than balance', async function () {
  195. await expectRevert(this.token.burn(
  196. initialHolder, initialSupply.addn(1)), 'ERC20: burn amount exceeds balance',
  197. );
  198. });
  199. const describeBurn = function (description, amount) {
  200. describe(description, function () {
  201. beforeEach('burning', async function () {
  202. const { logs } = await this.token.burn(initialHolder, amount);
  203. this.logs = logs;
  204. });
  205. it('decrements totalSupply', async function () {
  206. const expectedSupply = initialSupply.sub(amount);
  207. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  208. });
  209. it('decrements initialHolder balance', async function () {
  210. const expectedBalance = initialSupply.sub(amount);
  211. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  212. });
  213. it('emits Transfer event', async function () {
  214. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  215. from: initialHolder,
  216. to: ZERO_ADDRESS,
  217. });
  218. expect(event.args.value).to.be.bignumber.equal(amount);
  219. });
  220. });
  221. };
  222. describeBurn('for entire balance', initialSupply);
  223. describeBurn('for less amount than balance', initialSupply.subn(1));
  224. });
  225. });
  226. describe('_transfer', function () {
  227. shouldBehaveLikeERC20Transfer('ERC20', initialHolder, recipient, initialSupply, function (from, to, amount) {
  228. return this.token.transferInternal(from, to, amount);
  229. });
  230. describe('when the sender is the zero address', function () {
  231. it('reverts', async function () {
  232. await expectRevert(this.token.transferInternal(ZERO_ADDRESS, recipient, initialSupply),
  233. 'ERC20: transfer from the zero address',
  234. );
  235. });
  236. });
  237. });
  238. describe('_approve', function () {
  239. shouldBehaveLikeERC20Approve('ERC20', initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  240. return this.token.approveInternal(owner, spender, amount);
  241. });
  242. describe('when the owner is the zero address', function () {
  243. it('reverts', async function () {
  244. await expectRevert(this.token.approveInternal(ZERO_ADDRESS, recipient, initialSupply),
  245. 'ERC20: approve from the zero address',
  246. );
  247. });
  248. });
  249. });
  250. });