ERC20.test.js 13 KB

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