ERC20.test.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. });
  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. ({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: initialHolder }));
  52. });
  53. it('emits an approval event', async function () {
  54. const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  55. expectEvent.inLogs(logs, 'Approval', {
  56. owner: initialHolder,
  57. spender: spender,
  58. 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(this.token.decreaseAllowance(
  91. spender, amount, { from: initialHolder }), 'ERC20: decreased allowance below zero'
  92. );
  93. });
  94. });
  95. });
  96. describe('increase allowance', function () {
  97. const amount = initialSupply;
  98. describe('when the spender is not the zero address', function () {
  99. const spender = recipient;
  100. describe('when the sender has enough balance', function () {
  101. it('emits an approval event', async function () {
  102. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  103. expectEvent.inLogs(logs, '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. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  129. expectEvent.inLogs(logs, 'Approval', {
  130. owner: initialHolder,
  131. spender: spender,
  132. value: amount,
  133. });
  134. });
  135. describe('when there was no approved amount before', function () {
  136. it('approves the requested amount', async function () {
  137. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  138. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  139. });
  140. });
  141. describe('when the spender had an approved amount', function () {
  142. beforeEach(async function () {
  143. await this.token.approve(spender, new BN(1), { from: initialHolder });
  144. });
  145. it('increases the spender allowance adding the requested amount', async function () {
  146. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  147. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  148. });
  149. });
  150. });
  151. });
  152. describe('when the spender is the zero address', function () {
  153. const spender = ZERO_ADDRESS;
  154. it('reverts', async function () {
  155. await expectRevert(
  156. this.token.increaseAllowance(spender, amount, { from: initialHolder }), '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(
  165. this.token.mint(ZERO_ADDRESS, amount), 'ERC20: mint to the zero address'
  166. );
  167. });
  168. describe('for a non zero account', function () {
  169. beforeEach('minting', async function () {
  170. const { logs } = await this.token.mint(recipient, amount);
  171. this.logs = logs;
  172. });
  173. it('increments totalSupply', async function () {
  174. const expectedSupply = initialSupply.add(amount);
  175. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  176. });
  177. it('increments recipient balance', async function () {
  178. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  179. });
  180. it('emits Transfer event', async function () {
  181. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  182. from: ZERO_ADDRESS,
  183. to: recipient,
  184. });
  185. expect(event.args.value).to.be.bignumber.equal(amount);
  186. });
  187. });
  188. });
  189. describe('_burn', function () {
  190. it('rejects a null account', async function () {
  191. await expectRevert(this.token.burn(ZERO_ADDRESS, new BN(1)),
  192. 'ERC20: burn from the zero address');
  193. });
  194. describe('for a non zero account', function () {
  195. it('rejects burning more than balance', async function () {
  196. await expectRevert(this.token.burn(
  197. initialHolder, initialSupply.addn(1)), 'ERC20: burn amount exceeds balance'
  198. );
  199. });
  200. const describeBurn = function (description, amount) {
  201. describe(description, function () {
  202. beforeEach('burning', async function () {
  203. const { logs } = await this.token.burn(initialHolder, amount);
  204. this.logs = logs;
  205. });
  206. it('decrements totalSupply', async function () {
  207. const expectedSupply = initialSupply.sub(amount);
  208. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  209. });
  210. it('decrements initialHolder balance', async function () {
  211. const expectedBalance = initialSupply.sub(amount);
  212. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  213. });
  214. it('emits Transfer event', async function () {
  215. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  216. from: initialHolder,
  217. to: ZERO_ADDRESS,
  218. });
  219. expect(event.args.value).to.be.bignumber.equal(amount);
  220. });
  221. });
  222. };
  223. describeBurn('for entire balance', initialSupply);
  224. describeBurn('for less amount than balance', initialSupply.subn(1));
  225. });
  226. });
  227. describe('_transfer', function () {
  228. shouldBehaveLikeERC20Transfer('ERC20', initialHolder, recipient, initialSupply, function (from, to, amount) {
  229. return this.token.transferInternal(from, to, amount);
  230. });
  231. describe('when the sender is the zero address', function () {
  232. it('reverts', async function () {
  233. await expectRevert(this.token.transferInternal(ZERO_ADDRESS, recipient, initialSupply),
  234. 'ERC20: transfer from the zero address'
  235. );
  236. });
  237. });
  238. });
  239. describe('_approve', function () {
  240. shouldBehaveLikeERC20Approve('ERC20', initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  241. return this.token.approveInternal(owner, spender, amount);
  242. });
  243. describe('when the owner is the zero address', function () {
  244. it('reverts', async function () {
  245. await expectRevert(this.token.approveInternal(ZERO_ADDRESS, recipient, initialSupply),
  246. 'ERC20: approve from the zero address'
  247. );
  248. });
  249. });
  250. });
  251. });