StandardToken.test.js 18 KB

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