ERC20.behavior.js 10 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.unspecified(this.token.transferFrom(
  71. tokenOwner, to, amount, { from: spender }),
  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.unspecified(this.token.transferFrom(
  84. tokenOwner, to, amount, { from: spender }),
  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.unspecified(this.token.transferFrom(
  92. tokenOwner, to, amount, { from: spender }),
  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.unspecified(transfer.call(this, from, to, amount),
  136. );
  137. });
  138. });
  139. describe('when the sender transfers all balance', function () {
  140. const amount = balance;
  141. it('transfers the requested amount', async function () {
  142. await transfer.call(this, from, to, amount);
  143. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  144. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  145. });
  146. it('emits a transfer event', async function () {
  147. const { logs } = await transfer.call(this, from, to, amount);
  148. expectEvent.inLogs(logs, 'Transfer', {
  149. from,
  150. to,
  151. value: amount,
  152. });
  153. });
  154. });
  155. describe('when the sender transfers zero tokens', function () {
  156. const amount = new BN('0');
  157. it('transfers the requested amount', async function () {
  158. await transfer.call(this, from, to, amount);
  159. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  160. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  161. });
  162. it('emits a transfer event', async function () {
  163. const { logs } = await transfer.call(this, from, to, amount);
  164. expectEvent.inLogs(logs, 'Transfer', {
  165. from,
  166. to,
  167. value: amount,
  168. });
  169. });
  170. });
  171. });
  172. describe('when the recipient is the zero address', function () {
  173. it('reverts', async function () {
  174. await expectRevert(transfer.call(this, from, ZERO_ADDRESS, balance),
  175. `${errorPrefix}: transfer to the zero address`,
  176. );
  177. });
  178. });
  179. }
  180. function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, approve) {
  181. describe('when the spender is not the zero address', function () {
  182. describe('when the sender has enough balance', function () {
  183. const amount = supply;
  184. it('emits an approval event', async function () {
  185. const { logs } = await approve.call(this, owner, spender, amount);
  186. expectEvent.inLogs(logs, 'Approval', {
  187. owner: owner,
  188. spender: spender,
  189. value: amount,
  190. });
  191. });
  192. describe('when there was no approved amount before', function () {
  193. it('approves the requested amount', async function () {
  194. await approve.call(this, owner, spender, amount);
  195. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  196. });
  197. });
  198. describe('when the spender had an approved amount', function () {
  199. beforeEach(async function () {
  200. await approve.call(this, owner, spender, new BN(1));
  201. });
  202. it('approves the requested amount and replaces the previous one', async function () {
  203. await approve.call(this, owner, spender, amount);
  204. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  205. });
  206. });
  207. });
  208. describe('when the sender does not have enough balance', function () {
  209. const amount = supply.addn(1);
  210. it('emits an approval event', async function () {
  211. const { logs } = await approve.call(this, owner, spender, amount);
  212. expectEvent.inLogs(logs, 'Approval', {
  213. owner: owner,
  214. spender: spender,
  215. value: amount,
  216. });
  217. });
  218. describe('when there was no approved amount before', function () {
  219. it('approves the requested amount', async function () {
  220. await approve.call(this, owner, spender, amount);
  221. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  222. });
  223. });
  224. describe('when the spender had an approved amount', function () {
  225. beforeEach(async function () {
  226. await approve.call(this, owner, spender, new BN(1));
  227. });
  228. it('approves the requested amount and replaces the previous one', async function () {
  229. await approve.call(this, owner, spender, amount);
  230. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  231. });
  232. });
  233. });
  234. });
  235. describe('when the spender is the zero address', function () {
  236. it('reverts', async function () {
  237. await expectRevert(approve.call(this, owner, ZERO_ADDRESS, supply),
  238. `${errorPrefix}: approve to the zero address`,
  239. );
  240. });
  241. });
  242. }
  243. module.exports = {
  244. shouldBehaveLikeERC20,
  245. shouldBehaveLikeERC20Transfer,
  246. shouldBehaveLikeERC20Approve,
  247. };