ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { ZERO_ADDRESS } = constants;
  4. function shouldBehaveLikeERC20 (errorPrefix, initialSupply, initialHolder, recipient, anotherAccount) {
  5. describe('total supply', function () {
  6. it('returns the total amount of tokens', async function () {
  7. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  8. });
  9. });
  10. describe('balanceOf', function () {
  11. describe('when the requested account has no tokens', function () {
  12. it('returns zero', async function () {
  13. expect(await this.token.balanceOf(anotherAccount)).to.be.bignumber.equal('0');
  14. });
  15. });
  16. describe('when the requested account has some tokens', function () {
  17. it('returns the total amount of tokens', async function () {
  18. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(initialSupply);
  19. });
  20. });
  21. });
  22. describe('transfer', function () {
  23. shouldBehaveLikeERC20Transfer(errorPrefix, initialHolder, recipient, initialSupply,
  24. function (from, to, value) {
  25. return this.token.transfer(to, value, { from });
  26. },
  27. );
  28. });
  29. describe('transfer from', function () {
  30. const spender = recipient;
  31. describe('when the token owner is not the zero address', function () {
  32. const tokenOwner = initialHolder;
  33. describe('when the recipient is not the zero address', function () {
  34. const to = anotherAccount;
  35. describe('when the spender has enough allowance', function () {
  36. beforeEach(async function () {
  37. await this.token.approve(spender, initialSupply, { from: initialHolder });
  38. });
  39. describe('when the token owner has enough balance', function () {
  40. const amount = initialSupply;
  41. it('transfers the requested amount', async function () {
  42. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  43. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('0');
  44. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  45. });
  46. it('decreases the spender allowance', async function () {
  47. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  48. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal('0');
  49. });
  50. it('emits a transfer event', async function () {
  51. expectEvent(
  52. await this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  53. 'Transfer',
  54. { from: tokenOwner, to: to, value: amount },
  55. );
  56. });
  57. it('emits an approval event', async function () {
  58. expectEvent(
  59. await this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  60. 'Approval',
  61. { owner: tokenOwner, spender: spender, value: await this.token.allowance(tokenOwner, spender) },
  62. );
  63. });
  64. });
  65. describe('when the token owner does not have enough balance', function () {
  66. const amount = initialSupply;
  67. beforeEach('reducing balance', async function () {
  68. await this.token.transfer(to, 1, { from: tokenOwner });
  69. });
  70. it('reverts', async function () {
  71. await expectRevert(
  72. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  73. `${errorPrefix}: transfer amount exceeds balance`,
  74. );
  75. });
  76. });
  77. });
  78. describe('when the spender does not have enough allowance', function () {
  79. const allowance = initialSupply.subn(1);
  80. beforeEach(async function () {
  81. await this.token.approve(spender, allowance, { from: tokenOwner });
  82. });
  83. describe('when the token owner has enough balance', function () {
  84. const amount = initialSupply;
  85. it('reverts', async function () {
  86. await expectRevert(
  87. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  88. `${errorPrefix}: transfer amount exceeds allowance`,
  89. );
  90. });
  91. });
  92. describe('when the token owner does not have enough balance', function () {
  93. const amount = allowance;
  94. beforeEach('reducing balance', async function () {
  95. await this.token.transfer(to, 2, { from: tokenOwner });
  96. });
  97. it('reverts', async function () {
  98. await expectRevert(
  99. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  100. `${errorPrefix}: transfer amount exceeds balance`,
  101. );
  102. });
  103. });
  104. });
  105. });
  106. describe('when the recipient is the zero address', function () {
  107. const amount = initialSupply;
  108. const to = ZERO_ADDRESS;
  109. beforeEach(async function () {
  110. await this.token.approve(spender, amount, { from: tokenOwner });
  111. });
  112. it('reverts', async function () {
  113. await expectRevert(this.token.transferFrom(
  114. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer to the zero address`,
  115. );
  116. });
  117. });
  118. });
  119. describe('when the token owner is the zero address', function () {
  120. const amount = 0;
  121. const tokenOwner = ZERO_ADDRESS;
  122. const to = recipient;
  123. it('reverts', async function () {
  124. await expectRevert(
  125. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  126. 'from the zero address',
  127. );
  128. });
  129. });
  130. });
  131. describe('approve', function () {
  132. shouldBehaveLikeERC20Approve(errorPrefix, initialHolder, recipient, initialSupply,
  133. function (owner, spender, amount) {
  134. return this.token.approve(spender, amount, { from: owner });
  135. },
  136. );
  137. });
  138. }
  139. function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer) {
  140. describe('when the recipient is not the zero address', function () {
  141. describe('when the sender does not have enough balance', function () {
  142. const amount = balance.addn(1);
  143. it('reverts', async function () {
  144. await expectRevert(transfer.call(this, from, to, amount),
  145. `${errorPrefix}: transfer amount exceeds balance`,
  146. );
  147. });
  148. });
  149. describe('when the sender transfers all balance', function () {
  150. const amount = balance;
  151. it('transfers the requested amount', async function () {
  152. await transfer.call(this, from, to, amount);
  153. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  154. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  155. });
  156. it('emits a transfer event', async function () {
  157. expectEvent(
  158. await transfer.call(this, from, to, amount),
  159. 'Transfer',
  160. { from, to, value: amount },
  161. );
  162. });
  163. });
  164. describe('when the sender transfers zero tokens', function () {
  165. const amount = new BN('0');
  166. it('transfers the requested amount', async function () {
  167. await transfer.call(this, from, to, amount);
  168. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  169. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  170. });
  171. it('emits a transfer event', async function () {
  172. expectEvent(
  173. await transfer.call(this, from, to, amount),
  174. 'Transfer',
  175. { from, to, value: amount },
  176. );
  177. });
  178. });
  179. });
  180. describe('when the recipient is the zero address', function () {
  181. it('reverts', async function () {
  182. await expectRevert(transfer.call(this, from, ZERO_ADDRESS, balance),
  183. `${errorPrefix}: transfer to the zero address`,
  184. );
  185. });
  186. });
  187. }
  188. function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, approve) {
  189. describe('when the spender is not the zero address', function () {
  190. describe('when the sender has enough balance', function () {
  191. const amount = supply;
  192. it('emits an approval event', async function () {
  193. expectEvent(
  194. await approve.call(this, owner, spender, amount),
  195. 'Approval',
  196. { owner: owner, spender: spender, value: amount },
  197. );
  198. });
  199. describe('when there was no approved amount before', function () {
  200. it('approves the requested amount', async function () {
  201. await approve.call(this, owner, spender, amount);
  202. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  203. });
  204. });
  205. describe('when the spender had an approved amount', function () {
  206. beforeEach(async function () {
  207. await approve.call(this, owner, spender, new BN(1));
  208. });
  209. it('approves the requested amount and replaces the previous one', async function () {
  210. await approve.call(this, owner, spender, amount);
  211. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  212. });
  213. });
  214. });
  215. describe('when the sender does not have enough balance', function () {
  216. const amount = supply.addn(1);
  217. it('emits an approval event', async function () {
  218. expectEvent(
  219. await approve.call(this, owner, spender, amount),
  220. 'Approval',
  221. { owner: owner, spender: spender, value: amount },
  222. );
  223. });
  224. describe('when there was no approved amount before', function () {
  225. it('approves the requested amount', async function () {
  226. await approve.call(this, owner, spender, amount);
  227. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  228. });
  229. });
  230. describe('when the spender had an approved amount', function () {
  231. beforeEach(async function () {
  232. await approve.call(this, owner, spender, new BN(1));
  233. });
  234. it('approves the requested amount and replaces the previous one', async function () {
  235. await approve.call(this, owner, spender, amount);
  236. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  237. });
  238. });
  239. });
  240. });
  241. describe('when the spender is the zero address', function () {
  242. it('reverts', async function () {
  243. await expectRevert(approve.call(this, owner, ZERO_ADDRESS, supply),
  244. `${errorPrefix}: approve to the zero address`,
  245. );
  246. });
  247. });
  248. }
  249. module.exports = {
  250. shouldBehaveLikeERC20,
  251. shouldBehaveLikeERC20Transfer,
  252. shouldBehaveLikeERC20Approve,
  253. };