ERC20.test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const expectEvent = require('../../helpers/expectEvent');
  3. const ERC20 = artifacts.require('ERC20Mock');
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. contract('ERC20', function ([_, owner, recipient, anotherAccount]) {
  9. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  10. beforeEach(async function () {
  11. this.token = await ERC20.new(owner, 100);
  12. });
  13. describe('total supply', function () {
  14. it('returns the total amount of tokens', async function () {
  15. (await this.token.totalSupply()).should.be.bignumber.equal(100);
  16. });
  17. });
  18. describe('balanceOf', function () {
  19. describe('when the requested account has no tokens', function () {
  20. it('returns zero', async function () {
  21. (await this.token.balanceOf(anotherAccount)).should.be.bignumber.equal(0);
  22. });
  23. });
  24. describe('when the requested account has some tokens', function () {
  25. it('returns the total amount of tokens', async function () {
  26. (await this.token.balanceOf(owner)).should.be.bignumber.equal(100);
  27. });
  28. });
  29. });
  30. describe('transfer', function () {
  31. describe('when the recipient is not the zero address', function () {
  32. const to = recipient;
  33. describe('when the sender does not have enough balance', function () {
  34. const amount = 101;
  35. it('reverts', async function () {
  36. await assertRevert(this.token.transfer(to, amount, { from: owner }));
  37. });
  38. });
  39. describe('when the sender has enough balance', function () {
  40. const amount = 100;
  41. it('transfers the requested amount', async function () {
  42. await this.token.transfer(to, amount, { from: owner });
  43. (await this.token.balanceOf(owner)).should.be.bignumber.equal(0);
  44. (await this.token.balanceOf(to)).should.be.bignumber.equal(amount);
  45. });
  46. it('emits a transfer event', async function () {
  47. const { logs } = await this.token.transfer(to, amount, { from: owner });
  48. const event = expectEvent.inLogs(logs, 'Transfer', {
  49. from: owner,
  50. to: to,
  51. });
  52. event.args.value.should.be.bignumber.equal(amount);
  53. });
  54. });
  55. });
  56. describe('when the recipient is the zero address', function () {
  57. const to = ZERO_ADDRESS;
  58. it('reverts', async function () {
  59. await assertRevert(this.token.transfer(to, 100, { from: owner }));
  60. });
  61. });
  62. });
  63. describe('approve', function () {
  64. describe('when the spender is not the zero address', function () {
  65. const spender = recipient;
  66. describe('when the sender has enough balance', function () {
  67. const amount = 100;
  68. it('emits an approval event', async function () {
  69. const { logs } = await this.token.approve(spender, amount, { from: owner });
  70. logs.length.should.equal(1);
  71. logs[0].event.should.equal('Approval');
  72. logs[0].args.owner.should.equal(owner);
  73. logs[0].args.spender.should.equal(spender);
  74. logs[0].args.value.should.be.bignumber.equal(amount);
  75. });
  76. describe('when there was no approved amount before', function () {
  77. it('approves the requested amount', async function () {
  78. await this.token.approve(spender, amount, { from: owner });
  79. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  80. });
  81. });
  82. describe('when the spender had an approved amount', function () {
  83. beforeEach(async function () {
  84. await this.token.approve(spender, 1, { from: owner });
  85. });
  86. it('approves the requested amount and replaces the previous one', async function () {
  87. await this.token.approve(spender, amount, { from: owner });
  88. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  89. });
  90. });
  91. });
  92. describe('when the sender does not have enough balance', function () {
  93. const amount = 101;
  94. it('emits an approval event', async function () {
  95. const { logs } = await this.token.approve(spender, amount, { from: owner });
  96. logs.length.should.equal(1);
  97. logs[0].event.should.equal('Approval');
  98. logs[0].args.owner.should.equal(owner);
  99. logs[0].args.spender.should.equal(spender);
  100. logs[0].args.value.should.be.bignumber.equal(amount);
  101. });
  102. describe('when there was no approved amount before', function () {
  103. it('approves the requested amount', async function () {
  104. await this.token.approve(spender, amount, { from: owner });
  105. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  106. });
  107. });
  108. describe('when the spender had an approved amount', function () {
  109. beforeEach(async function () {
  110. await this.token.approve(spender, 1, { from: owner });
  111. });
  112. it('approves the requested amount and replaces the previous one', async function () {
  113. await this.token.approve(spender, amount, { from: owner });
  114. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  115. });
  116. });
  117. });
  118. });
  119. describe('when the spender is the zero address', function () {
  120. const amount = 100;
  121. const spender = ZERO_ADDRESS;
  122. it('reverts', async function () {
  123. await assertRevert(this.token.approve(spender, amount, { from: owner }));
  124. });
  125. });
  126. });
  127. describe('transfer from', function () {
  128. const spender = recipient;
  129. describe('when the recipient is not the zero address', function () {
  130. const to = anotherAccount;
  131. describe('when the spender has enough approved balance', function () {
  132. beforeEach(async function () {
  133. await this.token.approve(spender, 100, { from: owner });
  134. });
  135. describe('when the owner has enough balance', function () {
  136. const amount = 100;
  137. it('transfers the requested amount', async function () {
  138. await this.token.transferFrom(owner, to, amount, { from: spender });
  139. (await this.token.balanceOf(owner)).should.be.bignumber.equal(0);
  140. (await this.token.balanceOf(to)).should.be.bignumber.equal(amount);
  141. });
  142. it('decreases the spender allowance', async function () {
  143. await this.token.transferFrom(owner, to, amount, { from: spender });
  144. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
  145. });
  146. it('emits a transfer event', async function () {
  147. const { logs } = await this.token.transferFrom(owner, to, amount, { from: spender });
  148. logs.length.should.equal(1);
  149. logs[0].event.should.equal('Transfer');
  150. logs[0].args.from.should.equal(owner);
  151. logs[0].args.to.should.equal(to);
  152. logs[0].args.value.should.be.bignumber.equal(amount);
  153. });
  154. });
  155. describe('when the owner does not have enough balance', function () {
  156. const amount = 101;
  157. it('reverts', async function () {
  158. await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
  159. });
  160. });
  161. });
  162. describe('when the spender does not have enough approved balance', function () {
  163. beforeEach(async function () {
  164. await this.token.approve(spender, 99, { from: owner });
  165. });
  166. describe('when the owner has enough balance', function () {
  167. const amount = 100;
  168. it('reverts', async function () {
  169. await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
  170. });
  171. });
  172. describe('when the owner does not have enough balance', function () {
  173. const amount = 101;
  174. it('reverts', async function () {
  175. await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
  176. });
  177. });
  178. });
  179. });
  180. describe('when the recipient is the zero address', function () {
  181. const amount = 100;
  182. const to = ZERO_ADDRESS;
  183. beforeEach(async function () {
  184. await this.token.approve(spender, amount, { from: owner });
  185. });
  186. it('reverts', async function () {
  187. await assertRevert(this.token.transferFrom(owner, to, amount, { from: spender }));
  188. });
  189. });
  190. });
  191. describe('decrease allowance', function () {
  192. describe('when the spender is not the zero address', function () {
  193. const spender = recipient;
  194. function shouldDecreaseApproval (amount) {
  195. describe('when there was no approved amount before', function () {
  196. it('reverts', async function () {
  197. await assertRevert(this.token.decreaseAllowance(spender, amount, { from: owner }));
  198. });
  199. });
  200. describe('when the spender had an approved amount', function () {
  201. const approvedAmount = amount;
  202. beforeEach(async function () {
  203. ({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: owner }));
  204. });
  205. it('emits an approval event', async function () {
  206. const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: owner });
  207. logs.length.should.equal(1);
  208. logs[0].event.should.equal('Approval');
  209. logs[0].args.owner.should.equal(owner);
  210. logs[0].args.spender.should.equal(spender);
  211. logs[0].args.value.should.be.bignumber.equal(0);
  212. });
  213. it('decreases the spender allowance subtracting the requested amount', async function () {
  214. await this.token.decreaseAllowance(spender, approvedAmount - 1, { from: owner });
  215. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(1);
  216. });
  217. it('sets the allowance to zero when all allowance is removed', async function () {
  218. await this.token.decreaseAllowance(spender, approvedAmount, { from: owner });
  219. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
  220. });
  221. it('reverts when more than the full allowance is removed', async function () {
  222. await assertRevert(this.token.decreaseAllowance(spender, approvedAmount + 1, { from: owner }));
  223. });
  224. });
  225. }
  226. describe('when the sender has enough balance', function () {
  227. const amount = 100;
  228. shouldDecreaseApproval(amount);
  229. });
  230. describe('when the sender does not have enough balance', function () {
  231. const amount = 101;
  232. shouldDecreaseApproval(amount);
  233. });
  234. });
  235. describe('when the spender is the zero address', function () {
  236. const amount = 100;
  237. const spender = ZERO_ADDRESS;
  238. it('reverts', async function () {
  239. await assertRevert(this.token.decreaseAllowance(spender, amount, { from: owner }));
  240. });
  241. });
  242. });
  243. describe('increase allowance', function () {
  244. const amount = 100;
  245. describe('when the spender is not the zero address', function () {
  246. const spender = recipient;
  247. describe('when the sender has enough balance', function () {
  248. it('emits an approval event', async function () {
  249. const { logs } = await this.token.increaseAllowance(spender, amount, { from: owner });
  250. logs.length.should.equal(1);
  251. logs[0].event.should.equal('Approval');
  252. logs[0].args.owner.should.equal(owner);
  253. logs[0].args.spender.should.equal(spender);
  254. logs[0].args.value.should.be.bignumber.equal(amount);
  255. });
  256. describe('when there was no approved amount before', function () {
  257. it('approves the requested amount', async function () {
  258. await this.token.increaseAllowance(spender, amount, { from: owner });
  259. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  260. });
  261. });
  262. describe('when the spender had an approved amount', function () {
  263. beforeEach(async function () {
  264. await this.token.approve(spender, 1, { from: owner });
  265. });
  266. it('increases the spender allowance adding the requested amount', async function () {
  267. await this.token.increaseAllowance(spender, amount, { from: owner });
  268. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount + 1);
  269. });
  270. });
  271. });
  272. describe('when the sender does not have enough balance', function () {
  273. const amount = 101;
  274. it('emits an approval event', async function () {
  275. const { logs } = await this.token.increaseAllowance(spender, amount, { from: owner });
  276. logs.length.should.equal(1);
  277. logs[0].event.should.equal('Approval');
  278. logs[0].args.owner.should.equal(owner);
  279. logs[0].args.spender.should.equal(spender);
  280. logs[0].args.value.should.be.bignumber.equal(amount);
  281. });
  282. describe('when there was no approved amount before', function () {
  283. it('approves the requested amount', async function () {
  284. await this.token.increaseAllowance(spender, amount, { from: owner });
  285. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  286. });
  287. });
  288. describe('when the spender had an approved amount', function () {
  289. beforeEach(async function () {
  290. await this.token.approve(spender, 1, { from: owner });
  291. });
  292. it('increases the spender allowance adding the requested amount', async function () {
  293. await this.token.increaseAllowance(spender, amount, { from: owner });
  294. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount + 1);
  295. });
  296. });
  297. });
  298. });
  299. describe('when the spender is the zero address', function () {
  300. const spender = ZERO_ADDRESS;
  301. it('reverts', async function () {
  302. await assertRevert(this.token.increaseAllowance(spender, amount, { from: owner }));
  303. });
  304. });
  305. });
  306. describe('_mint', function () {
  307. const initialSupply = new BigNumber(100);
  308. const amount = new BigNumber(50);
  309. it('rejects a null account', async function () {
  310. await assertRevert(this.token.mint(ZERO_ADDRESS, amount));
  311. });
  312. describe('for a non null account', function () {
  313. beforeEach('minting', async function () {
  314. const { logs } = await this.token.mint(recipient, amount);
  315. this.logs = logs;
  316. });
  317. it('increments totalSupply', async function () {
  318. const expectedSupply = initialSupply.plus(amount);
  319. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  320. });
  321. it('increments recipient balance', async function () {
  322. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(amount);
  323. });
  324. it('emits Transfer event', async function () {
  325. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  326. from: ZERO_ADDRESS,
  327. to: recipient,
  328. });
  329. event.args.value.should.be.bignumber.equal(amount);
  330. });
  331. });
  332. });
  333. describe('_burn', function () {
  334. const initialSupply = new BigNumber(100);
  335. it('rejects a null account', async function () {
  336. await assertRevert(this.token.burn(ZERO_ADDRESS, 1));
  337. });
  338. describe('for a non null account', function () {
  339. it('rejects burning more than balance', async function () {
  340. await assertRevert(this.token.burn(owner, initialSupply.plus(1)));
  341. });
  342. const describeBurn = function (description, amount) {
  343. describe(description, function () {
  344. beforeEach('burning', async function () {
  345. const { logs } = await this.token.burn(owner, amount);
  346. this.logs = logs;
  347. });
  348. it('decrements totalSupply', async function () {
  349. const expectedSupply = initialSupply.minus(amount);
  350. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  351. });
  352. it('decrements owner balance', async function () {
  353. const expectedBalance = initialSupply.minus(amount);
  354. (await this.token.balanceOf(owner)).should.be.bignumber.equal(expectedBalance);
  355. });
  356. it('emits Transfer event', async function () {
  357. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  358. from: owner,
  359. to: ZERO_ADDRESS,
  360. });
  361. event.args.value.should.be.bignumber.equal(amount);
  362. });
  363. });
  364. };
  365. describeBurn('for entire balance', initialSupply);
  366. describeBurn('for less amount than balance', initialSupply.sub(1));
  367. });
  368. });
  369. describe('_burnFrom', function () {
  370. const initialSupply = new BigNumber(100);
  371. const allowance = new BigNumber(70);
  372. const spender = anotherAccount;
  373. beforeEach('approving', async function () {
  374. await this.token.approve(spender, allowance, { from: owner });
  375. });
  376. it('rejects a null account', async function () {
  377. await assertRevert(this.token.burnFrom(ZERO_ADDRESS, 1));
  378. });
  379. describe('for a non null account', function () {
  380. it('rejects burning more than allowance', async function () {
  381. await assertRevert(this.token.burnFrom(owner, allowance.plus(1)));
  382. });
  383. it('rejects burning more than balance', async function () {
  384. await assertRevert(this.token.burnFrom(owner, initialSupply.plus(1)));
  385. });
  386. const describeBurnFrom = function (description, amount) {
  387. describe(description, function () {
  388. beforeEach('burning', async function () {
  389. const { logs } = await this.token.burnFrom(owner, amount, { from: spender });
  390. this.logs = logs;
  391. });
  392. it('decrements totalSupply', async function () {
  393. const expectedSupply = initialSupply.minus(amount);
  394. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  395. });
  396. it('decrements owner balance', async function () {
  397. const expectedBalance = initialSupply.minus(amount);
  398. (await this.token.balanceOf(owner)).should.be.bignumber.equal(expectedBalance);
  399. });
  400. it('decrements spender allowance', async function () {
  401. const expectedAllowance = allowance.minus(amount);
  402. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(expectedAllowance);
  403. });
  404. it('emits Transfer event', async function () {
  405. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  406. from: owner,
  407. to: ZERO_ADDRESS,
  408. });
  409. event.args.value.should.be.bignumber.equal(amount);
  410. });
  411. });
  412. };
  413. describeBurnFrom('for entire allowance', allowance);
  414. describeBurnFrom('for less amount than allowance', allowance.sub(1));
  415. });
  416. });
  417. });