ERC20.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 ERC20 = artifacts.require('$ERC20');
  10. const ERC20Decimals = 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 ERC20.new(name, symbol);
  18. await this.token.$_mint(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('set decimals', function () {
  30. const decimals = new BN(6);
  31. it('can set decimals during construction', async function () {
  32. const token = await ERC20Decimals.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(
  44. this.token.decreaseAllowance(spender, amount, { from: initialHolder }),
  45. 'ERC20: decreased allowance below zero',
  46. );
  47. });
  48. });
  49. describe('when the spender had an approved amount', function () {
  50. const approvedAmount = amount;
  51. beforeEach(async function () {
  52. await this.token.approve(spender, approvedAmount, { from: initialHolder });
  53. });
  54. it('emits an approval event', async function () {
  55. expectEvent(
  56. await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder }),
  57. 'Approval',
  58. { owner: initialHolder, spender: spender, 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(
  91. this.token.decreaseAllowance(spender, amount, { from: initialHolder }),
  92. 'ERC20: decreased allowance below zero',
  93. );
  94. });
  95. });
  96. });
  97. describe('increase allowance', function () {
  98. const amount = initialSupply;
  99. describe('when the spender is not the zero address', function () {
  100. const spender = recipient;
  101. describe('when the sender has enough balance', function () {
  102. it('emits an approval event', async function () {
  103. expectEvent(await this.token.increaseAllowance(spender, amount, { from: initialHolder }), '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. expectEvent(await this.token.increaseAllowance(spender, amount, { from: initialHolder }), '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 }),
  156. '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(this.token.$_mint(ZERO_ADDRESS, amount), 'ERC20: mint to the zero address');
  165. });
  166. it('rejects overflow', async function () {
  167. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  168. await expectRevert(
  169. this.token.$_mint(recipient, maxUint256),
  170. 'reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)',
  171. );
  172. });
  173. describe('for a non zero account', function () {
  174. beforeEach('minting', async function () {
  175. this.receipt = await this.token.$_mint(recipient, amount);
  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(this.receipt, 'Transfer', { from: ZERO_ADDRESS, to: recipient });
  186. expect(event.args.value).to.be.bignumber.equal(amount);
  187. });
  188. });
  189. });
  190. describe('_burn', function () {
  191. it('rejects a null account', async function () {
  192. await expectRevert(this.token.$_burn(ZERO_ADDRESS, new BN(1)), '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(
  197. this.token.$_burn(initialHolder, initialSupply.addn(1)),
  198. 'ERC20: transfer amount exceeds balance',
  199. );
  200. });
  201. const describeBurn = function (description, amount) {
  202. describe(description, function () {
  203. beforeEach('burning', async function () {
  204. this.receipt = await this.token.$_burn(initialHolder, amount);
  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(this.receipt, 'Transfer', { from: initialHolder, to: ZERO_ADDRESS });
  216. expect(event.args.value).to.be.bignumber.equal(amount);
  217. });
  218. });
  219. };
  220. describeBurn('for entire balance', initialSupply);
  221. describeBurn('for less amount than balance', initialSupply.subn(1));
  222. });
  223. });
  224. describe('_update', function () {
  225. const amount = new BN(1);
  226. it('from is the zero address', async function () {
  227. const balanceBefore = await this.token.balanceOf(initialHolder);
  228. const totalSupply = await this.token.totalSupply();
  229. expectEvent(await this.token.$_update(ZERO_ADDRESS, initialHolder, amount), 'Transfer', {
  230. from: ZERO_ADDRESS,
  231. to: initialHolder,
  232. value: amount,
  233. });
  234. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply.add(amount));
  235. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(balanceBefore.add(amount));
  236. });
  237. it('to is the zero address', async function () {
  238. const balanceBefore = await this.token.balanceOf(initialHolder);
  239. const totalSupply = await this.token.totalSupply();
  240. expectEvent(await this.token.$_update(initialHolder, ZERO_ADDRESS, amount), 'Transfer', {
  241. from: initialHolder,
  242. to: ZERO_ADDRESS,
  243. value: amount,
  244. });
  245. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply.sub(amount));
  246. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(balanceBefore.sub(amount));
  247. });
  248. it('from and to are the zero address', async function () {
  249. const totalSupply = await this.token.totalSupply();
  250. await this.token.$_update(ZERO_ADDRESS, ZERO_ADDRESS, amount);
  251. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply);
  252. expectEvent(await this.token.$_update(ZERO_ADDRESS, ZERO_ADDRESS, amount), 'Transfer', {
  253. from: ZERO_ADDRESS,
  254. to: ZERO_ADDRESS,
  255. value: amount,
  256. });
  257. });
  258. });
  259. describe('_transfer', function () {
  260. shouldBehaveLikeERC20Transfer('ERC20', initialHolder, recipient, initialSupply, function (from, to, amount) {
  261. return this.token.$_transfer(from, to, amount);
  262. });
  263. describe('when the sender is the zero address', function () {
  264. it('reverts', async function () {
  265. await expectRevert(
  266. this.token.$_transfer(ZERO_ADDRESS, recipient, initialSupply),
  267. 'ERC20: transfer from the zero address',
  268. );
  269. });
  270. });
  271. });
  272. describe('_approve', function () {
  273. shouldBehaveLikeERC20Approve('ERC20', initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  274. return this.token.$_approve(owner, spender, amount);
  275. });
  276. describe('when the owner is the zero address', function () {
  277. it('reverts', async function () {
  278. await expectRevert(
  279. this.token.$_approve(ZERO_ADDRESS, recipient, initialSupply),
  280. 'ERC20: approve from the zero address',
  281. );
  282. });
  283. });
  284. });
  285. });