StandardToken.test.js 21 KB

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