ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { ZERO_ADDRESS, MAX_UINT256 } = 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}: insufficient 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. describe('when the spender has unlimited allowance', function () {
  106. beforeEach(async function () {
  107. await this.token.approve(spender, MAX_UINT256, { from: initialHolder });
  108. });
  109. it('does not decrease the spender allowance', async function () {
  110. await this.token.transferFrom(tokenOwner, to, 1, { from: spender });
  111. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal(MAX_UINT256);
  112. });
  113. it('does not emit an approval event', async function () {
  114. expectEvent.notEmitted(
  115. await this.token.transferFrom(tokenOwner, to, 1, { from: spender }),
  116. 'Approval',
  117. );
  118. });
  119. });
  120. });
  121. describe('when the recipient is the zero address', function () {
  122. const amount = initialSupply;
  123. const to = ZERO_ADDRESS;
  124. beforeEach(async function () {
  125. await this.token.approve(spender, amount, { from: tokenOwner });
  126. });
  127. it('reverts', async function () {
  128. await expectRevert(
  129. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  130. `${errorPrefix}: transfer to the zero address`,
  131. );
  132. });
  133. });
  134. });
  135. describe('when the token owner is the zero address', function () {
  136. const amount = 0;
  137. const tokenOwner = ZERO_ADDRESS;
  138. const to = recipient;
  139. it('reverts', async function () {
  140. await expectRevert(
  141. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  142. 'from the zero address',
  143. );
  144. });
  145. });
  146. });
  147. describe('approve', function () {
  148. shouldBehaveLikeERC20Approve(errorPrefix, initialHolder, recipient, initialSupply,
  149. function (owner, spender, amount) {
  150. return this.token.approve(spender, amount, { from: owner });
  151. },
  152. );
  153. });
  154. }
  155. function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer) {
  156. describe('when the recipient is not the zero address', function () {
  157. describe('when the sender does not have enough balance', function () {
  158. const amount = balance.addn(1);
  159. it('reverts', async function () {
  160. await expectRevert(transfer.call(this, from, to, amount),
  161. `${errorPrefix}: transfer amount exceeds balance`,
  162. );
  163. });
  164. });
  165. describe('when the sender transfers all balance', function () {
  166. const amount = balance;
  167. it('transfers the requested amount', async function () {
  168. await transfer.call(this, from, to, amount);
  169. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  170. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  171. });
  172. it('emits a transfer event', async function () {
  173. expectEvent(
  174. await transfer.call(this, from, to, amount),
  175. 'Transfer',
  176. { from, to, value: amount },
  177. );
  178. });
  179. });
  180. describe('when the sender transfers zero tokens', function () {
  181. const amount = new BN('0');
  182. it('transfers the requested amount', async function () {
  183. await transfer.call(this, from, to, amount);
  184. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  185. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  186. });
  187. it('emits a transfer event', async function () {
  188. expectEvent(
  189. await transfer.call(this, from, to, amount),
  190. 'Transfer',
  191. { from, to, value: amount },
  192. );
  193. });
  194. });
  195. });
  196. }
  197. function shouldBehaveLikeERC20Approve (errorPrefix, owner, spender, supply, approve) {
  198. describe('when the spender is not the zero address', function () {
  199. describe('when the sender has enough balance', function () {
  200. const amount = supply;
  201. it('emits an approval event', async function () {
  202. expectEvent(
  203. await approve.call(this, owner, spender, amount),
  204. 'Approval',
  205. { owner: owner, spender: spender, value: amount },
  206. );
  207. });
  208. describe('when there was no approved amount before', function () {
  209. it('approves the requested amount', 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. describe('when the spender had an approved amount', function () {
  215. beforeEach(async function () {
  216. await approve.call(this, owner, spender, new BN(1));
  217. });
  218. it('approves the requested amount and replaces the previous one', async function () {
  219. await approve.call(this, owner, spender, amount);
  220. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  221. });
  222. });
  223. });
  224. describe('when the sender does not have enough balance', function () {
  225. const amount = supply.addn(1);
  226. it('emits an approval event', async function () {
  227. expectEvent(
  228. await approve.call(this, owner, spender, amount),
  229. 'Approval',
  230. { owner: owner, spender: spender, value: amount },
  231. );
  232. });
  233. describe('when there was no approved amount before', function () {
  234. it('approves the requested amount', 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. describe('when the spender had an approved amount', function () {
  240. beforeEach(async function () {
  241. await approve.call(this, owner, spender, new BN(1));
  242. });
  243. it('approves the requested amount and replaces the previous one', async function () {
  244. await approve.call(this, owner, spender, amount);
  245. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  246. });
  247. });
  248. });
  249. });
  250. describe('when the spender is the zero address', function () {
  251. it('reverts', async function () {
  252. await expectRevert(approve.call(this, owner, ZERO_ADDRESS, supply),
  253. `${errorPrefix}: approve to the zero address`,
  254. );
  255. });
  256. });
  257. }
  258. module.exports = {
  259. shouldBehaveLikeERC20,
  260. shouldBehaveLikeERC20Transfer,
  261. shouldBehaveLikeERC20Approve,
  262. };