ERC20.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. contract('ERC20', function ([_, initialHolder, recipient, anotherAccount]) {
  11. const initialSupply = new BN(100);
  12. beforeEach(async function () {
  13. this.token = await ERC20Mock.new(initialHolder, initialSupply);
  14. });
  15. shouldBehaveLikeERC20('ERC20', initialSupply, initialHolder, recipient, anotherAccount);
  16. describe('decrease allowance', function () {
  17. describe('when the spender is not the zero address', function () {
  18. const spender = recipient;
  19. function shouldDecreaseApproval (amount) {
  20. describe('when there was no approved amount before', function () {
  21. it('reverts', async function () {
  22. await expectRevert(this.token.decreaseAllowance(
  23. spender, amount, { from: initialHolder }), 'ERC20: decreased allowance below zero'
  24. );
  25. });
  26. });
  27. describe('when the spender had an approved amount', function () {
  28. const approvedAmount = amount;
  29. beforeEach(async function () {
  30. ({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: initialHolder }));
  31. });
  32. it('emits an approval event', async function () {
  33. const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  34. expectEvent.inLogs(logs, 'Approval', {
  35. owner: initialHolder,
  36. spender: spender,
  37. value: new BN(0),
  38. });
  39. });
  40. it('decreases the spender allowance subtracting the requested amount', async function () {
  41. await this.token.decreaseAllowance(spender, approvedAmount.subn(1), { from: initialHolder });
  42. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal('1');
  43. });
  44. it('sets the allowance to zero when all allowance is removed', async function () {
  45. await this.token.decreaseAllowance(spender, approvedAmount, { from: initialHolder });
  46. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal('0');
  47. });
  48. it('reverts when more than the full allowance is removed', async function () {
  49. await expectRevert(
  50. this.token.decreaseAllowance(spender, approvedAmount.addn(1), { from: initialHolder }),
  51. 'ERC20: decreased allowance below zero'
  52. );
  53. });
  54. });
  55. }
  56. describe('when the sender has enough balance', function () {
  57. const amount = initialSupply;
  58. shouldDecreaseApproval(amount);
  59. });
  60. describe('when the sender does not have enough balance', function () {
  61. const amount = initialSupply.addn(1);
  62. shouldDecreaseApproval(amount);
  63. });
  64. });
  65. describe('when the spender is the zero address', function () {
  66. const amount = initialSupply;
  67. const spender = ZERO_ADDRESS;
  68. it('reverts', async function () {
  69. await expectRevert(this.token.decreaseAllowance(
  70. spender, amount, { from: initialHolder }), 'ERC20: decreased allowance below zero'
  71. );
  72. });
  73. });
  74. });
  75. describe('increase allowance', function () {
  76. const amount = initialSupply;
  77. describe('when the spender is not the zero address', function () {
  78. const spender = recipient;
  79. describe('when the sender has enough balance', function () {
  80. it('emits an approval event', async function () {
  81. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  82. expectEvent.inLogs(logs, 'Approval', {
  83. owner: initialHolder,
  84. spender: spender,
  85. value: amount,
  86. });
  87. });
  88. describe('when there was no approved amount before', function () {
  89. it('approves the requested amount', async function () {
  90. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  91. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  92. });
  93. });
  94. describe('when the spender had an approved amount', function () {
  95. beforeEach(async function () {
  96. await this.token.approve(spender, new BN(1), { from: initialHolder });
  97. });
  98. it('increases the spender allowance adding the requested amount', async function () {
  99. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  100. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  101. });
  102. });
  103. });
  104. describe('when the sender does not have enough balance', function () {
  105. const amount = initialSupply.addn(1);
  106. it('emits an approval event', async function () {
  107. const { logs } = await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  108. expectEvent.inLogs(logs, 'Approval', {
  109. owner: initialHolder,
  110. spender: spender,
  111. value: amount,
  112. });
  113. });
  114. describe('when there was no approved amount before', function () {
  115. it('approves the requested amount', async function () {
  116. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  117. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount);
  118. });
  119. });
  120. describe('when the spender had an approved amount', function () {
  121. beforeEach(async function () {
  122. await this.token.approve(spender, new BN(1), { from: initialHolder });
  123. });
  124. it('increases the spender allowance adding the requested amount', async function () {
  125. await this.token.increaseAllowance(spender, amount, { from: initialHolder });
  126. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(amount.addn(1));
  127. });
  128. });
  129. });
  130. });
  131. describe('when the spender is the zero address', function () {
  132. const spender = ZERO_ADDRESS;
  133. it('reverts', async function () {
  134. await expectRevert(
  135. this.token.increaseAllowance(spender, amount, { from: initialHolder }), 'ERC20: approve to the zero address'
  136. );
  137. });
  138. });
  139. });
  140. describe('_mint', function () {
  141. const amount = new BN(50);
  142. it('rejects a null account', async function () {
  143. await expectRevert(
  144. this.token.mint(ZERO_ADDRESS, amount), 'ERC20: mint to the zero address'
  145. );
  146. });
  147. describe('for a non zero account', function () {
  148. beforeEach('minting', async function () {
  149. const { logs } = await this.token.mint(recipient, amount);
  150. this.logs = logs;
  151. });
  152. it('increments totalSupply', async function () {
  153. const expectedSupply = initialSupply.add(amount);
  154. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  155. });
  156. it('increments recipient balance', async function () {
  157. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  158. });
  159. it('emits Transfer event', async function () {
  160. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  161. from: ZERO_ADDRESS,
  162. to: recipient,
  163. });
  164. expect(event.args.value).to.be.bignumber.equal(amount);
  165. });
  166. });
  167. });
  168. describe('_burn', function () {
  169. it('rejects a null account', async function () {
  170. await expectRevert(this.token.burn(ZERO_ADDRESS, new BN(1)),
  171. 'ERC20: burn from the zero address');
  172. });
  173. describe('for a non zero account', function () {
  174. it('rejects burning more than balance', async function () {
  175. await expectRevert(this.token.burn(
  176. initialHolder, initialSupply.addn(1)), 'ERC20: burn amount exceeds balance'
  177. );
  178. });
  179. const describeBurn = function (description, amount) {
  180. describe(description, function () {
  181. beforeEach('burning', async function () {
  182. const { logs } = await this.token.burn(initialHolder, amount);
  183. this.logs = logs;
  184. });
  185. it('decrements totalSupply', async function () {
  186. const expectedSupply = initialSupply.sub(amount);
  187. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  188. });
  189. it('decrements initialHolder balance', async function () {
  190. const expectedBalance = initialSupply.sub(amount);
  191. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  192. });
  193. it('emits Transfer event', async function () {
  194. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  195. from: initialHolder,
  196. to: ZERO_ADDRESS,
  197. });
  198. expect(event.args.value).to.be.bignumber.equal(amount);
  199. });
  200. });
  201. };
  202. describeBurn('for entire balance', initialSupply);
  203. describeBurn('for less amount than balance', initialSupply.subn(1));
  204. });
  205. });
  206. describe('_burnFrom', function () {
  207. const allowance = new BN(70);
  208. const spender = anotherAccount;
  209. beforeEach('approving', async function () {
  210. await this.token.approve(spender, allowance, { from: initialHolder });
  211. });
  212. it('rejects a null account', async function () {
  213. await expectRevert(this.token.burnFrom(ZERO_ADDRESS, new BN(1)),
  214. 'ERC20: burn from the zero address'
  215. );
  216. });
  217. describe('for a non zero account', function () {
  218. it('rejects burning more than allowance', async function () {
  219. await expectRevert(this.token.burnFrom(initialHolder, allowance.addn(1)),
  220. 'ERC20: burn amount exceeds allowance'
  221. );
  222. });
  223. it('rejects burning more than balance', async function () {
  224. await expectRevert(this.token.burnFrom(initialHolder, initialSupply.addn(1)),
  225. 'ERC20: burn amount exceeds balance'
  226. );
  227. });
  228. const describeBurnFrom = function (description, amount) {
  229. describe(description, function () {
  230. beforeEach('burning', async function () {
  231. const { logs } = await this.token.burnFrom(initialHolder, amount, { from: spender });
  232. this.logs = logs;
  233. });
  234. it('decrements totalSupply', async function () {
  235. const expectedSupply = initialSupply.sub(amount);
  236. expect(await this.token.totalSupply()).to.be.bignumber.equal(expectedSupply);
  237. });
  238. it('decrements initialHolder balance', async function () {
  239. const expectedBalance = initialSupply.sub(amount);
  240. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(expectedBalance);
  241. });
  242. it('decrements spender allowance', async function () {
  243. const expectedAllowance = allowance.sub(amount);
  244. expect(await this.token.allowance(initialHolder, spender)).to.be.bignumber.equal(expectedAllowance);
  245. });
  246. it('emits a Transfer event', async function () {
  247. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  248. from: initialHolder,
  249. to: ZERO_ADDRESS,
  250. });
  251. expect(event.args.value).to.be.bignumber.equal(amount);
  252. });
  253. it('emits an Approval event', async function () {
  254. expectEvent.inLogs(this.logs, 'Approval', {
  255. owner: initialHolder,
  256. spender: spender,
  257. value: await this.token.allowance(initialHolder, spender),
  258. });
  259. });
  260. });
  261. };
  262. describeBurnFrom('for entire allowance', allowance);
  263. describeBurnFrom('for less amount than allowance', allowance.subn(1));
  264. });
  265. });
  266. describe('_transfer', function () {
  267. shouldBehaveLikeERC20Transfer('ERC20', initialHolder, recipient, initialSupply, function (from, to, amount) {
  268. return this.token.transferInternal(from, to, amount);
  269. });
  270. describe('when the sender is the zero address', function () {
  271. it('reverts', async function () {
  272. await expectRevert(this.token.transferInternal(ZERO_ADDRESS, recipient, initialSupply),
  273. 'ERC20: transfer from the zero address'
  274. );
  275. });
  276. });
  277. });
  278. describe('_approve', function () {
  279. shouldBehaveLikeERC20Approve('ERC20', initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  280. return this.token.approveInternal(owner, spender, amount);
  281. });
  282. describe('when the owner is the zero address', function () {
  283. it('reverts', async function () {
  284. await expectRevert(this.token.approveInternal(ZERO_ADDRESS, recipient, initialSupply),
  285. 'ERC20: approve from the zero address'
  286. );
  287. });
  288. });
  289. });
  290. });