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