ERC20.test.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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(value) {
  39. describe('when there was no approved value before', function () {
  40. it('reverts', async function () {
  41. const allowance = await this.token.allowance(initialHolder, spender);
  42. await expectRevertCustomError(
  43. this.token.decreaseAllowance(spender, value, { from: initialHolder }),
  44. 'ERC20FailedDecreaseAllowance',
  45. [spender, allowance, value],
  46. );
  47. });
  48. });
  49. describe('when the spender had an approved value', function () {
  50. const approvedValue = value;
  51. beforeEach(async function () {
  52. await this.token.approve(spender, approvedValue, { from: initialHolder });
  53. });
  54. it('emits an approval event', async function () {
  55. expectEvent(
  56. await this.token.decreaseAllowance(spender, approvedValue, { from: initialHolder }),
  57. 'Approval',
  58. { owner: initialHolder, spender: spender, value: new BN(0) },
  59. );
  60. });
  61. it('decreases the spender allowance subtracting the requested value', async function () {
  62. await this.token.decreaseAllowance(spender, approvedValue.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, approvedValue, { 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, approvedValue.addn(1), { from: initialHolder }),
  72. 'ERC20FailedDecreaseAllowance',
  73. [spender, approvedValue, approvedValue.addn(1)],
  74. );
  75. });
  76. });
  77. }
  78. describe('when the sender has enough balance', function () {
  79. const value = initialSupply;
  80. shouldDecreaseApproval(value);
  81. });
  82. describe('when the sender does not have enough balance', function () {
  83. const value = initialSupply.addn(1);
  84. shouldDecreaseApproval(value);
  85. });
  86. });
  87. describe('when the spender is the zero address', function () {
  88. const value = initialSupply;
  89. const spender = ZERO_ADDRESS;
  90. it('reverts', async function () {
  91. await expectRevertCustomError(
  92. this.token.decreaseAllowance(spender, value, { from: initialHolder }),
  93. 'ERC20FailedDecreaseAllowance',
  94. [spender, 0, value],
  95. );
  96. });
  97. });
  98. });
  99. describe('increase allowance', function () {
  100. const value = 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, value, { from: initialHolder }), 'Approval', {
  106. owner: initialHolder,
  107. spender: spender,
  108. value: value,
  109. });
  110. });
  111. describe('when there was no approved value before', function () {
  112. it('approves the requested value', async function () {
  113. await this.token.increaseAllowance(spender, value, { from: initialHolder });
  114. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value);
  115. });
  116. });
  117. describe('when the spender had an approved value', 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 value', async function () {
  122. await this.token.increaseAllowance(spender, value, { from: initialHolder });
  123. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value.addn(1));
  124. });
  125. });
  126. });
  127. describe('when the sender does not have enough balance', function () {
  128. const value = initialSupply.addn(1);
  129. it('emits an approval event', async function () {
  130. expectEvent(await this.token.increaseAllowance(spender, value, { from: initialHolder }), 'Approval', {
  131. owner: initialHolder,
  132. spender: spender,
  133. value: value,
  134. });
  135. });
  136. describe('when there was no approved value before', function () {
  137. it('approves the requested value', async function () {
  138. await this.token.increaseAllowance(spender, value, { from: initialHolder });
  139. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value);
  140. });
  141. });
  142. describe('when the spender had an approved value', 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 value', async function () {
  147. await this.token.increaseAllowance(spender, value, { from: initialHolder });
  148. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(value.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, value, { from: initialHolder }),
  158. 'ERC20InvalidSpender',
  159. [ZERO_ADDRESS],
  160. );
  161. });
  162. });
  163. });
  164. describe('_mint', function () {
  165. const value = new BN(50);
  166. it('rejects a null account', async function () {
  167. await expectRevertCustomError(this.token.$_mint(ZERO_ADDRESS, value), 'ERC20InvalidReceiver', [ZERO_ADDRESS]);
  168. });
  169. it('rejects overflow', async function () {
  170. const maxUint256 = new BN('2').pow(new BN(256)).subn(1);
  171. await expectRevert(
  172. this.token.$_mint(recipient, maxUint256),
  173. 'reverted with panic code 0x11 (Arithmetic operation underflowed or overflowed outside of an unchecked block)',
  174. );
  175. });
  176. describe('for a non zero account', function () {
  177. beforeEach('minting', async function () {
  178. this.receipt = await this.token.$_mint(recipient, value);
  179. });
  180. it('increments totalSupply', async function () {
  181. const expectedSupply = initialSupply.add(value);
  182. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  183. });
  184. it('increments recipient balance', async function () {
  185. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(value);
  186. });
  187. it('emits Transfer event', async function () {
  188. const event = expectEvent(this.receipt, 'Transfer', { from: ZERO_ADDRESS, to: recipient });
  189. expect(event.args.value).to.be.bignumber.equal(value);
  190. });
  191. });
  192. });
  193. describe('_burn', function () {
  194. it('rejects a null account', async function () {
  195. await expectRevertCustomError(this.token.$_burn(ZERO_ADDRESS, new BN(1)), 'ERC20InvalidSender', [
  196. ZERO_ADDRESS,
  197. ]);
  198. });
  199. describe('for a non zero account', function () {
  200. it('rejects burning more than balance', async function () {
  201. await expectRevertCustomError(
  202. this.token.$_burn(initialHolder, initialSupply.addn(1)),
  203. 'ERC20InsufficientBalance',
  204. [initialHolder, initialSupply, initialSupply.addn(1)],
  205. );
  206. });
  207. const describeBurn = function (description, value) {
  208. describe(description, function () {
  209. beforeEach('burning', async function () {
  210. this.receipt = await this.token.$_burn(initialHolder, value);
  211. });
  212. it('decrements totalSupply', async function () {
  213. const expectedSupply = initialSupply.sub(value);
  214. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  215. });
  216. it('decrements initialHolder balance', async function () {
  217. const expectedBalance = initialSupply.sub(value);
  218. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  219. });
  220. it('emits Transfer event', async function () {
  221. const event = expectEvent(this.receipt, 'Transfer', { from: initialHolder, to: ZERO_ADDRESS });
  222. expect(event.args.value).to.be.bignumber.equal(value);
  223. });
  224. });
  225. };
  226. describeBurn('for entire balance', initialSupply);
  227. describeBurn('for less value than balance', initialSupply.subn(1));
  228. });
  229. });
  230. describe('_update', function () {
  231. const value = new BN(1);
  232. it('from is the zero address', async function () {
  233. const balanceBefore = await this.token.balanceOf(initialHolder);
  234. const totalSupply = await this.token.totalSupply();
  235. expectEvent(await this.token.$_update(ZERO_ADDRESS, initialHolder, value), 'Transfer', {
  236. from: ZERO_ADDRESS,
  237. to: initialHolder,
  238. value: value,
  239. });
  240. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply.add(value));
  241. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(balanceBefore.add(value));
  242. });
  243. it('to is the zero address', async function () {
  244. const balanceBefore = await this.token.balanceOf(initialHolder);
  245. const totalSupply = await this.token.totalSupply();
  246. expectEvent(await this.token.$_update(initialHolder, ZERO_ADDRESS, value), 'Transfer', {
  247. from: initialHolder,
  248. to: ZERO_ADDRESS,
  249. value: value,
  250. });
  251. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply.sub(value));
  252. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(balanceBefore.sub(value));
  253. });
  254. it('from and to are the zero address', async function () {
  255. const totalSupply = await this.token.totalSupply();
  256. await this.token.$_update(ZERO_ADDRESS, ZERO_ADDRESS, value);
  257. expect(await this.token.totalSupply()).to.be.bignumber.equal(totalSupply);
  258. expectEvent(await this.token.$_update(ZERO_ADDRESS, ZERO_ADDRESS, value), 'Transfer', {
  259. from: ZERO_ADDRESS,
  260. to: ZERO_ADDRESS,
  261. value: value,
  262. });
  263. });
  264. });
  265. describe('_transfer', function () {
  266. shouldBehaveLikeERC20Transfer(initialHolder, recipient, initialSupply, function (from, to, value) {
  267. return this.token.$_transfer(from, to, value);
  268. });
  269. describe('when the sender is the zero address', function () {
  270. it('reverts', async function () {
  271. await expectRevertCustomError(
  272. this.token.$_transfer(ZERO_ADDRESS, recipient, initialSupply),
  273. 'ERC20InvalidSender',
  274. [ZERO_ADDRESS],
  275. );
  276. });
  277. });
  278. });
  279. describe('_approve', function () {
  280. shouldBehaveLikeERC20Approve(initialHolder, recipient, initialSupply, function (owner, spender, value) {
  281. return this.token.$_approve(owner, spender, value);
  282. });
  283. describe('when the owner is the zero address', function () {
  284. it('reverts', async function () {
  285. await expectRevertCustomError(
  286. this.token.$_approve(ZERO_ADDRESS, recipient, initialSupply),
  287. 'ERC20InvalidApprover',
  288. [ZERO_ADDRESS],
  289. );
  290. });
  291. });
  292. });
  293. });
  294. }
  295. });