ERC20.test.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. const shouldFail = require('../../helpers/shouldFail');
  2. const expectEvent = require('../../helpers/expectEvent');
  3. const { ZERO_ADDRESS } = require('../../helpers/constants');
  4. const ERC20Mock = artifacts.require('ERC20Mock');
  5. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. contract('ERC20', function ([_, owner, recipient, anotherAccount]) {
  10. beforeEach(async function () {
  11. this.token = await ERC20Mock.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 shouldFail.reverting(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. expectEvent.inLogs(logs, 'Transfer', {
  49. from: owner,
  50. to: to,
  51. value: amount,
  52. });
  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 shouldFail.reverting(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. expectEvent.inLogs(logs, 'Approval', {
  71. owner: owner,
  72. spender: spender,
  73. value: amount,
  74. });
  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. expectEvent.inLogs(logs, 'Approval', {
  97. owner: owner,
  98. spender: spender,
  99. value: amount,
  100. });
  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 shouldFail.reverting(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. expectEvent.inLogs(logs, 'Transfer', {
  149. from: owner,
  150. to: to,
  151. value: amount,
  152. });
  153. });
  154. it('emits an approval event', async function () {
  155. const { logs } = await this.token.transferFrom(owner, to, amount, { from: spender });
  156. expectEvent.inLogs(logs, 'Approval', {
  157. owner: owner,
  158. spender: spender,
  159. value: await this.token.allowance(owner, spender),
  160. });
  161. });
  162. });
  163. describe('when the owner does not have enough balance', function () {
  164. const amount = 101;
  165. it('reverts', async function () {
  166. await shouldFail.reverting(this.token.transferFrom(owner, to, amount, { from: spender }));
  167. });
  168. });
  169. });
  170. describe('when the spender does not have enough approved balance', function () {
  171. beforeEach(async function () {
  172. await this.token.approve(spender, 99, { from: owner });
  173. });
  174. describe('when the owner has enough balance', function () {
  175. const amount = 100;
  176. it('reverts', async function () {
  177. await shouldFail.reverting(this.token.transferFrom(owner, to, amount, { from: spender }));
  178. });
  179. });
  180. describe('when the owner does not have enough balance', function () {
  181. const amount = 101;
  182. it('reverts', async function () {
  183. await shouldFail.reverting(this.token.transferFrom(owner, to, amount, { from: spender }));
  184. });
  185. });
  186. });
  187. });
  188. describe('when the recipient is the zero address', function () {
  189. const amount = 100;
  190. const to = ZERO_ADDRESS;
  191. beforeEach(async function () {
  192. await this.token.approve(spender, amount, { from: owner });
  193. });
  194. it('reverts', async function () {
  195. await shouldFail.reverting(this.token.transferFrom(owner, to, amount, { from: spender }));
  196. });
  197. });
  198. });
  199. describe('decrease allowance', function () {
  200. describe('when the spender is not the zero address', function () {
  201. const spender = recipient;
  202. function shouldDecreaseApproval (amount) {
  203. describe('when there was no approved amount before', function () {
  204. it('reverts', async function () {
  205. await shouldFail.reverting(this.token.decreaseAllowance(spender, amount, { from: owner }));
  206. });
  207. });
  208. describe('when the spender had an approved amount', function () {
  209. const approvedAmount = amount;
  210. beforeEach(async function () {
  211. ({ logs: this.logs } = await this.token.approve(spender, approvedAmount, { from: owner }));
  212. });
  213. it('emits an approval event', async function () {
  214. const { logs } = await this.token.decreaseAllowance(spender, approvedAmount, { from: owner });
  215. expectEvent.inLogs(logs, 'Approval', {
  216. owner: owner,
  217. spender: spender,
  218. value: 0,
  219. });
  220. });
  221. it('decreases the spender allowance subtracting the requested amount', async function () {
  222. await this.token.decreaseAllowance(spender, approvedAmount - 1, { from: owner });
  223. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(1);
  224. });
  225. it('sets the allowance to zero when all allowance is removed', async function () {
  226. await this.token.decreaseAllowance(spender, approvedAmount, { from: owner });
  227. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(0);
  228. });
  229. it('reverts when more than the full allowance is removed', async function () {
  230. await shouldFail.reverting(this.token.decreaseAllowance(spender, approvedAmount + 1, { from: owner }));
  231. });
  232. });
  233. }
  234. describe('when the sender has enough balance', function () {
  235. const amount = 100;
  236. shouldDecreaseApproval(amount);
  237. });
  238. describe('when the sender does not have enough balance', function () {
  239. const amount = 101;
  240. shouldDecreaseApproval(amount);
  241. });
  242. });
  243. describe('when the spender is the zero address', function () {
  244. const amount = 100;
  245. const spender = ZERO_ADDRESS;
  246. it('reverts', async function () {
  247. await shouldFail.reverting(this.token.decreaseAllowance(spender, amount, { from: owner }));
  248. });
  249. });
  250. });
  251. describe('increase allowance', function () {
  252. const amount = 100;
  253. describe('when the spender is not the zero address', function () {
  254. const spender = recipient;
  255. describe('when the sender has enough balance', function () {
  256. it('emits an approval event', async function () {
  257. const { logs } = await this.token.increaseAllowance(spender, amount, { from: owner });
  258. expectEvent.inLogs(logs, 'Approval', {
  259. owner: owner,
  260. spender: spender,
  261. value: amount,
  262. });
  263. });
  264. describe('when there was no approved amount before', function () {
  265. it('approves the requested amount', async function () {
  266. await this.token.increaseAllowance(spender, amount, { from: owner });
  267. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  268. });
  269. });
  270. describe('when the spender had an approved amount', function () {
  271. beforeEach(async function () {
  272. await this.token.approve(spender, 1, { from: owner });
  273. });
  274. it('increases the spender allowance adding the requested amount', async function () {
  275. await this.token.increaseAllowance(spender, amount, { from: owner });
  276. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount + 1);
  277. });
  278. });
  279. });
  280. describe('when the sender does not have enough balance', function () {
  281. const amount = 101;
  282. it('emits an approval event', async function () {
  283. const { logs } = await this.token.increaseAllowance(spender, amount, { from: owner });
  284. expectEvent.inLogs(logs, 'Approval', {
  285. owner: owner,
  286. spender: spender,
  287. value: amount,
  288. });
  289. });
  290. describe('when there was no approved amount before', function () {
  291. it('approves the requested amount', async function () {
  292. await this.token.increaseAllowance(spender, amount, { from: owner });
  293. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount);
  294. });
  295. });
  296. describe('when the spender had an approved amount', function () {
  297. beforeEach(async function () {
  298. await this.token.approve(spender, 1, { from: owner });
  299. });
  300. it('increases the spender allowance adding the requested amount', async function () {
  301. await this.token.increaseAllowance(spender, amount, { from: owner });
  302. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(amount + 1);
  303. });
  304. });
  305. });
  306. });
  307. describe('when the spender is the zero address', function () {
  308. const spender = ZERO_ADDRESS;
  309. it('reverts', async function () {
  310. await shouldFail.reverting(this.token.increaseAllowance(spender, amount, { from: owner }));
  311. });
  312. });
  313. });
  314. describe('_mint', function () {
  315. const initialSupply = new BigNumber(100);
  316. const amount = new BigNumber(50);
  317. it('rejects a null account', async function () {
  318. await shouldFail.reverting(this.token.mint(ZERO_ADDRESS, amount));
  319. });
  320. describe('for a non null account', function () {
  321. beforeEach('minting', async function () {
  322. const { logs } = await this.token.mint(recipient, amount);
  323. this.logs = logs;
  324. });
  325. it('increments totalSupply', async function () {
  326. const expectedSupply = initialSupply.plus(amount);
  327. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  328. });
  329. it('increments recipient balance', async function () {
  330. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(amount);
  331. });
  332. it('emits Transfer event', async function () {
  333. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  334. from: ZERO_ADDRESS,
  335. to: recipient,
  336. });
  337. event.args.value.should.be.bignumber.equal(amount);
  338. });
  339. });
  340. });
  341. describe('_burn', function () {
  342. const initialSupply = new BigNumber(100);
  343. it('rejects a null account', async function () {
  344. await shouldFail.reverting(this.token.burn(ZERO_ADDRESS, 1));
  345. });
  346. describe('for a non null account', function () {
  347. it('rejects burning more than balance', async function () {
  348. await shouldFail.reverting(this.token.burn(owner, initialSupply.plus(1)));
  349. });
  350. const describeBurn = function (description, amount) {
  351. describe(description, function () {
  352. beforeEach('burning', async function () {
  353. const { logs } = await this.token.burn(owner, amount);
  354. this.logs = logs;
  355. });
  356. it('decrements totalSupply', async function () {
  357. const expectedSupply = initialSupply.minus(amount);
  358. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  359. });
  360. it('decrements owner balance', async function () {
  361. const expectedBalance = initialSupply.minus(amount);
  362. (await this.token.balanceOf(owner)).should.be.bignumber.equal(expectedBalance);
  363. });
  364. it('emits Transfer event', async function () {
  365. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  366. from: owner,
  367. to: ZERO_ADDRESS,
  368. });
  369. event.args.value.should.be.bignumber.equal(amount);
  370. });
  371. });
  372. };
  373. describeBurn('for entire balance', initialSupply);
  374. describeBurn('for less amount than balance', initialSupply.sub(1));
  375. });
  376. });
  377. describe('_burnFrom', function () {
  378. const initialSupply = new BigNumber(100);
  379. const allowance = new BigNumber(70);
  380. const spender = anotherAccount;
  381. beforeEach('approving', async function () {
  382. await this.token.approve(spender, allowance, { from: owner });
  383. });
  384. it('rejects a null account', async function () {
  385. await shouldFail.reverting(this.token.burnFrom(ZERO_ADDRESS, 1));
  386. });
  387. describe('for a non null account', function () {
  388. it('rejects burning more than allowance', async function () {
  389. await shouldFail.reverting(this.token.burnFrom(owner, allowance.plus(1)));
  390. });
  391. it('rejects burning more than balance', async function () {
  392. await shouldFail.reverting(this.token.burnFrom(owner, initialSupply.plus(1)));
  393. });
  394. const describeBurnFrom = function (description, amount) {
  395. describe(description, function () {
  396. beforeEach('burning', async function () {
  397. const { logs } = await this.token.burnFrom(owner, amount, { from: spender });
  398. this.logs = logs;
  399. });
  400. it('decrements totalSupply', async function () {
  401. const expectedSupply = initialSupply.minus(amount);
  402. (await this.token.totalSupply()).should.be.bignumber.equal(expectedSupply);
  403. });
  404. it('decrements owner balance', async function () {
  405. const expectedBalance = initialSupply.minus(amount);
  406. (await this.token.balanceOf(owner)).should.be.bignumber.equal(expectedBalance);
  407. });
  408. it('decrements spender allowance', async function () {
  409. const expectedAllowance = allowance.minus(amount);
  410. (await this.token.allowance(owner, spender)).should.be.bignumber.equal(expectedAllowance);
  411. });
  412. it('emits a Transfer event', async function () {
  413. const event = expectEvent.inLogs(this.logs, 'Transfer', {
  414. from: owner,
  415. to: ZERO_ADDRESS,
  416. });
  417. event.args.value.should.be.bignumber.equal(amount);
  418. });
  419. it('emits an Approval event', async function () {
  420. expectEvent.inLogs(this.logs, 'Approval', {
  421. owner: owner,
  422. spender: spender,
  423. value: await this.token.allowance(owner, spender),
  424. });
  425. });
  426. });
  427. };
  428. describeBurnFrom('for entire allowance', allowance);
  429. describeBurnFrom('for less amount than allowance', allowance.sub(1));
  430. });
  431. });
  432. });