ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 approved balance', 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. const { logs } = await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  52. expectEvent.inLogs(logs, 'Transfer', {
  53. from: tokenOwner,
  54. to: to,
  55. value: amount,
  56. });
  57. });
  58. it('emits an approval event', async function () {
  59. const { logs } = await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  60. expectEvent.inLogs(logs, 'Approval', {
  61. owner: tokenOwner,
  62. spender: spender,
  63. value: await this.token.allowance(tokenOwner, spender),
  64. });
  65. });
  66. });
  67. describe('when the token owner does not have enough balance', function () {
  68. const amount = initialSupply.addn(1);
  69. it('reverts', async function () {
  70. await expectRevert(this.token.transferFrom(
  71. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer amount exceeds balance`,
  72. );
  73. });
  74. });
  75. });
  76. describe('when the spender does not have enough approved balance', function () {
  77. beforeEach(async function () {
  78. await this.token.approve(spender, initialSupply.subn(1), { from: tokenOwner });
  79. });
  80. describe('when the token owner has enough balance', function () {
  81. const amount = initialSupply;
  82. it('reverts', async function () {
  83. await expectRevert(this.token.transferFrom(
  84. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer amount exceeds allowance`,
  85. );
  86. });
  87. });
  88. describe('when the token owner does not have enough balance', function () {
  89. const amount = initialSupply.addn(1);
  90. it('reverts', async function () {
  91. await expectRevert(this.token.transferFrom(
  92. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer amount exceeds balance`,
  93. );
  94. });
  95. });
  96. });
  97. });
  98. describe('when the recipient is the zero address', function () {
  99. const amount = initialSupply;
  100. const to = ZERO_ADDRESS;
  101. beforeEach(async function () {
  102. await this.token.approve(spender, amount, { from: tokenOwner });
  103. });
  104. it('reverts', async function () {
  105. await expectRevert(this.token.transferFrom(
  106. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer to the zero address`,
  107. );
  108. });
  109. });
  110. });
  111. describe('when the token owner is the zero address', function () {
  112. const amount = 0;
  113. const tokenOwner = ZERO_ADDRESS;
  114. const to = recipient;
  115. it('reverts', async function () {
  116. await expectRevert(this.token.transferFrom(
  117. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer from the zero address`,
  118. );
  119. });
  120. });
  121. });
  122. describe('approve', function () {
  123. shouldBehaveLikeERC20Approve(errorPrefix, initialHolder, recipient, initialSupply,
  124. function (owner, spender, amount) {
  125. return this.token.approve(spender, amount, { from: owner });
  126. },
  127. );
  128. });
  129. }
  130. function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer) {
  131. describe('when the recipient is not the zero address', function () {
  132. describe('when the sender does not have enough balance', function () {
  133. const amount = balance.addn(1);
  134. it('reverts', async function () {
  135. await expectRevert(transfer.call(this, from, to, amount),
  136. `${errorPrefix}: transfer amount exceeds balance`,
  137. );
  138. });
  139. });
  140. describe('when the sender transfers all balance', function () {
  141. const amount = balance;
  142. it('transfers the requested amount', async function () {
  143. await transfer.call(this, from, to, amount);
  144. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  145. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  146. });
  147. it('emits a transfer event', async function () {
  148. const { logs } = await transfer.call(this, from, to, amount);
  149. expectEvent.inLogs(logs, 'Transfer', {
  150. from,
  151. to,
  152. value: amount,
  153. });
  154. });
  155. });
  156. describe('when the sender transfers zero tokens', function () {
  157. const amount = new BN('0');
  158. it('transfers the requested amount', async function () {
  159. await transfer.call(this, from, to, amount);
  160. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  161. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  162. });
  163. it('emits a transfer event', async function () {
  164. const { logs } = await transfer.call(this, from, to, amount);
  165. expectEvent.inLogs(logs, 'Transfer', {
  166. from,
  167. to,
  168. value: amount,
  169. });
  170. });
  171. });
  172. });
  173. describe('when the recipient is the zero address', function () {
  174. it('reverts', async function () {
  175. await expectRevert(transfer.call(this, from, ZERO_ADDRESS, balance),
  176. `${errorPrefix}: transfer to the zero address`,
  177. );
  178. });
  179. });
  180. }
  181. function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, approve) {
  182. describe('when the spender is not the zero address', function () {
  183. describe('when the sender has enough balance', function () {
  184. const amount = supply;
  185. it('emits an approval event', async function () {
  186. const { logs } = await approve.call(this, owner, spender, amount);
  187. expectEvent.inLogs(logs, 'Approval', {
  188. owner: owner,
  189. spender: spender,
  190. value: amount,
  191. });
  192. });
  193. describe('when there was no approved amount before', function () {
  194. it('approves the requested amount', async function () {
  195. await approve.call(this, owner, spender, amount);
  196. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  197. });
  198. });
  199. describe('when the spender had an approved amount', function () {
  200. beforeEach(async function () {
  201. await approve.call(this, owner, spender, new BN(1));
  202. });
  203. it('approves the requested amount and replaces the previous one', async function () {
  204. await approve.call(this, owner, spender, amount);
  205. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  206. });
  207. });
  208. });
  209. describe('when the sender does not have enough balance', function () {
  210. const amount = supply.addn(1);
  211. it('emits an approval event', async function () {
  212. const { logs } = await approve.call(this, owner, spender, amount);
  213. expectEvent.inLogs(logs, 'Approval', {
  214. owner: owner,
  215. spender: spender,
  216. value: amount,
  217. });
  218. });
  219. describe('when there was no approved amount before', function () {
  220. it('approves the requested amount', async function () {
  221. await approve.call(this, owner, spender, amount);
  222. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  223. });
  224. });
  225. describe('when the spender had an approved amount', function () {
  226. beforeEach(async function () {
  227. await approve.call(this, owner, spender, new BN(1));
  228. });
  229. it('approves the requested amount and replaces the previous one', async function () {
  230. await approve.call(this, owner, spender, amount);
  231. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  232. });
  233. });
  234. });
  235. });
  236. describe('when the spender is the zero address', function () {
  237. it('reverts', async function () {
  238. await expectRevert(approve.call(this, owner, ZERO_ADDRESS, supply),
  239. `${errorPrefix}: approve to the zero address`,
  240. );
  241. });
  242. });
  243. }
  244. module.exports = {
  245. shouldBehaveLikeERC20,
  246. shouldBehaveLikeERC20Transfer,
  247. shouldBehaveLikeERC20Approve,
  248. };