ERC20.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 { expectRevertCustomError } = require('../../helpers/customError');
  10. const TOKENS = [
  11. { Token: artifacts.require('$ERC20') },
  12. { Token: artifacts.require('$ERC20ApprovalMock'), forcedApproval: true },
  13. ];
  14. contract('ERC20', function (accounts) {
  15. const [initialHolder, recipient] = accounts;
  16. const name = 'My Token';
  17. const symbol = 'MTKN';
  18. const initialSupply = new BN(100);
  19. for (const { Token, forcedApproval } of TOKENS) {
  20. describe(`using ${Token._json.contractName}`, function () {
  21. beforeEach(async function () {
  22. this.token = await Token.new(name, symbol);
  23. await this.token.$_mint(initialHolder, initialSupply);
  24. });
  25. shouldBehaveLikeERC20(initialSupply, accounts, { forcedApproval });
  26. it('has a name', async function () {
  27. expect(await this.token.name()).to.equal(name);
  28. });
  29. it('has a symbol', async function () {
  30. expect(await this.token.symbol()).to.equal(symbol);
  31. });
  32. it('has 18 decimals', async function () {
  33. expect(await this.token.decimals()).to.be.bignumber.equal('18');
  34. });
  35. describe('decrease allowance', function () {
  36. describe('when the spender is not the zero address', function () {
  37. const spender = recipient;
  38. function shouldDecreaseApproval(amount) {
  39. describe('when there was no approved amount before', function () {
  40. it('reverts', async function () {
  41. const allowance = await this.token.allowance(initialHolder, spender);
  42. await expectRevertCustomError(
  43. this.token.decreaseAllowance(spender, amount, { from: initialHolder }),
  44. 'ERC20FailedDecreaseAllowance',
  45. [spender, allowance, amount],
  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 expectRevertCustomError(
  71. this.token.decreaseAllowance(spender, approvedAmount.addn(1), { from: initialHolder }),
  72. 'ERC20FailedDecreaseAllowance',
  73. [spender, approvedAmount, approvedAmount.addn(1)],
  74. );
  75. });
  76. });
  77. }
  78. describe('when the sender has enough balance', function () {
  79. const amount = initialSupply;
  80. shouldDecreaseApproval(amount);
  81. });
  82. describe('when the sender does not have enough balance', function () {
  83. const amount = initialSupply.addn(1);
  84. shouldDecreaseApproval(amount);
  85. });
  86. });
  87. describe('when the spender is the zero address', function () {
  88. const amount = initialSupply;
  89. const spender = ZERO_ADDRESS;
  90. it('reverts', async function () {
  91. await expectRevertCustomError(
  92. this.token.decreaseAllowance(spender, amount, { from: initialHolder }),
  93. 'ERC20FailedDecreaseAllowance',
  94. [spender, 0, amount],
  95. );
  96. });
  97. });
  98. });
  99. describe('increase allowance', function () {
  100. const amount = initialSupply;
  101. describe('when the spender is not the zero address', function () {
  102. const spender = recipient;
  103. describe('when the sender has enough balance', function () {
  104. it('emits an approval event', async function () {
  105. expectEvent(await this.token.increaseAllowance(spender, amount, { from: initialHolder }), 'Approval', {
  106. owner: initialHolder,
  107. spender: spender,
  108. value: amount,
  109. });
  110. });
  111. describe('when there was no approved amount before', function () {
  112. it('approves the requested amount', async function () {
  113. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  114. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  115. });
  116. });
  117. describe('when the spender had an approved amount', function () {
  118. beforeEach(async function () {
  119. await this.token.approve(spender, new BN(1), { from: initialHolder });
  120. });
  121. it('increases the spender allowance adding the requested amount', async function () {
  122. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  123. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  124. });
  125. });
  126. });
  127. describe('when the sender does not have enough balance', function () {
  128. const amount = initialSupply.addn(1);
  129. it('emits an approval event', async function () {
  130. expectEvent(await this.token.increaseAllowance(spender, amount, { from: initialHolder }), 'Approval', {
  131. owner: initialHolder,
  132. spender: spender,
  133. value: amount,
  134. });
  135. });
  136. describe('when there was no approved amount before', function () {
  137. it('approves the requested amount', async function () {
  138. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  139. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  140. });
  141. });
  142. describe('when the spender had an approved amount', function () {
  143. beforeEach(async function () {
  144. await this.token.approve(spender, new BN(1), { from: initialHolder });
  145. });
  146. it('increases the spender allowance adding the requested amount', async function () {
  147. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  148. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  149. });
  150. });
  151. });
  152. });
  153. describe('when the spender is the zero address', function () {
  154. const spender = ZERO_ADDRESS;
  155. it('reverts', async function () {
  156. await expectRevertCustomError(
  157. this.token.increaseAllowance(spender, amount, { from: initialHolder }),
  158. 'ERC20InvalidSpender',
  159. [ZERO_ADDRESS],
  160. );
  161. });
  162. });
  163. });
  164. describe('_mint', function () {
  165. const amount = new BN(50);
  166. it('rejects a null account', async function () {
  167. await expectRevertCustomError(this.token.$_mint(ZERO_ADDRESS, amount), 'ERC20InvalidReceiver', [
  168. ZERO_ADDRESS,
  169. ]);
  170. });
  171. it('rejects overflow', async function () {
  172. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  173. await expectRevert(
  174. this.token.$_mint(recipient, maxUint256),
  175. 'reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)',
  176. );
  177. });
  178. describe('for a non zero account', function () {
  179. beforeEach('minting', async function () {
  180. this.receipt = await this.token.$_mint(recipient, amount);
  181. });
  182. it('increments totalSupply', async function () {
  183. const expectedSupply = initialSupply.add(amount);
  184. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  185. });
  186. it('increments recipient balance', async function () {
  187. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  188. });
  189. it('emits Transfer event', async function () {
  190. const event = expectEvent(this.receipt, 'Transfer', { from: ZERO_ADDRESS, to: recipient });
  191. expect(event.args.value).to.be.bignumber.equal(amount);
  192. });
  193. });
  194. });
  195. describe('_burn', function () {
  196. it('rejects a null account', async function () {
  197. await expectRevertCustomError(this.token.$_burn(ZERO_ADDRESS, new BN(1)), 'ERC20InvalidSender', [
  198. ZERO_ADDRESS,
  199. ]);
  200. });
  201. describe('for a non zero account', function () {
  202. it('rejects burning more than balance', async function () {
  203. await expectRevertCustomError(
  204. this.token.$_burn(initialHolder, initialSupply.addn(1)),
  205. 'ERC20InsufficientBalance',
  206. [initialHolder, initialSupply, initialSupply.addn(1)],
  207. );
  208. });
  209. const describeBurn = function (description, amount) {
  210. describe(description, function () {
  211. beforeEach('burning', async function () {
  212. this.receipt = await this.token.$_burn(initialHolder, amount);
  213. });
  214. it('decrements totalSupply', async function () {
  215. const expectedSupply = initialSupply.sub(amount);
  216. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  217. });
  218. it('decrements initialHolder balance', async function () {
  219. const expectedBalance = initialSupply.sub(amount);
  220. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  221. });
  222. it('emits Transfer event', async function () {
  223. const event = expectEvent(this.receipt, 'Transfer', { from: initialHolder, to: ZERO_ADDRESS });
  224. expect(event.args.value).to.be.bignumber.equal(amount);
  225. });
  226. });
  227. };
  228. describeBurn('for entire balance', initialSupply);
  229. describeBurn('for less amount than balance', initialSupply.subn(1));
  230. });
  231. });
  232. describe('_update', function () {
  233. const amount = new BN(1);
  234. it('from is the zero address', async function () {
  235. const balanceBefore = await this.token.balanceOf(initialHolder);
  236. const totalSupply = await this.token.totalSupply();
  237. expectEvent(await this.token.$_update(ZERO_ADDRESS, initialHolder, amount), 'Transfer', {
  238. from: ZERO_ADDRESS,
  239. to: initialHolder,
  240. value: amount,
  241. });
  242. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply.add(amount));
  243. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(balanceBefore.add(amount));
  244. });
  245. it('to is the zero address', async function () {
  246. const balanceBefore = await this.token.balanceOf(initialHolder);
  247. const totalSupply = await this.token.totalSupply();
  248. expectEvent(await this.token.$_update(initialHolder, ZERO_ADDRESS, amount), 'Transfer', {
  249. from: initialHolder,
  250. to: ZERO_ADDRESS,
  251. value: amount,
  252. });
  253. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply.sub(amount));
  254. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(balanceBefore.sub(amount));
  255. });
  256. it('from and to are the zero address', async function () {
  257. const totalSupply = await this.token.totalSupply();
  258. await this.token.$_update(ZERO_ADDRESS, ZERO_ADDRESS, amount);
  259. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply);
  260. expectEvent(await this.token.$_update(ZERO_ADDRESS, ZERO_ADDRESS, amount), 'Transfer', {
  261. from: ZERO_ADDRESS,
  262. to: ZERO_ADDRESS,
  263. value: amount,
  264. });
  265. });
  266. });
  267. describe('_transfer', function () {
  268. shouldBehaveLikeERC20Transfer(initialHolder, recipient, initialSupply, function (from, to, amount) {
  269. return this.token.$_transfer(from, to, amount);
  270. });
  271. describe('when the sender is the zero address', function () {
  272. it('reverts', async function () {
  273. await expectRevertCustomError(
  274. this.token.$_transfer(ZERO_ADDRESS, recipient, initialSupply),
  275. 'ERC20InvalidSender',
  276. [ZERO_ADDRESS],
  277. );
  278. });
  279. });
  280. });
  281. describe('_approve', function () {
  282. shouldBehaveLikeERC20Approve(initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  283. return this.token.$_approve(owner, spender, amount);
  284. });
  285. describe('when the owner is the zero address', function () {
  286. it('reverts', async function () {
  287. await expectRevertCustomError(
  288. this.token.$_approve(ZERO_ADDRESS, recipient, initialSupply),
  289. 'ERC20InvalidApprover',
  290. [ZERO_ADDRESS],
  291. );
  292. });
  293. });
  294. });
  295. });
  296. }
  297. });