ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. const { BN, constants, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { ZERO_ADDRESS, MAX_UINT256 } = constants;
  4. const { expectRevertCustomError } = require('../../helpers/customError');
  5. function shouldBehaveLikeERC20(initialSupply, initialHolder, recipient, anotherAccount) {
  6. describe('total supply', function () {
  7. it('returns the total amount of tokens', async function () {
  8. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  9. });
  10. });
  11. describe('balanceOf', function () {
  12. describe('when the requested account has no tokens', function () {
  13. it('returns zero', async function () {
  14. expect(await this.token.balanceOf(anotherAccount)).to.be.bignumber.equal('0');
  15. });
  16. });
  17. describe('when the requested account has some tokens', function () {
  18. it('returns the total amount of tokens', async function () {
  19. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(initialSupply);
  20. });
  21. });
  22. });
  23. describe('transfer', function () {
  24. shouldBehaveLikeERC20Transfer(initialHolder, recipient, initialSupply, function (from, to, value) {
  25. return this.token.transfer(to, value, { from });
  26. });
  27. });
  28. describe('transfer from', function () {
  29. const spender = recipient;
  30. describe('when the token owner is not the zero address', function () {
  31. const tokenOwner = initialHolder;
  32. describe('when the recipient is not the zero address', function () {
  33. const to = anotherAccount;
  34. describe('when the spender has enough allowance', function () {
  35. beforeEach(async function () {
  36. await this.token.approve(spender, initialSupply, { from: initialHolder });
  37. });
  38. describe('when the token owner has enough balance', function () {
  39. const amount = initialSupply;
  40. it('transfers the requested amount', async function () {
  41. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  42. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('0');
  43. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  44. });
  45. it('decreases the spender allowance', async function () {
  46. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  47. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal('0');
  48. });
  49. it('emits a transfer event', async function () {
  50. expectEvent(await this.token.transferFrom(tokenOwner, to, amount, { from: spender }), 'Transfer', {
  51. from: tokenOwner,
  52. to: to,
  53. value: amount,
  54. });
  55. });
  56. it('emits an approval event', async function () {
  57. expectEvent(await this.token.transferFrom(tokenOwner, to, amount, { from: spender }), 'Approval', {
  58. owner: tokenOwner,
  59. spender: spender,
  60. value: await this.token.allowance(tokenOwner, spender),
  61. });
  62. });
  63. });
  64. describe('when the token owner does not have enough balance', function () {
  65. const amount = initialSupply;
  66. beforeEach('reducing balance', async function () {
  67. await this.token.transfer(to, 1, { from: tokenOwner });
  68. });
  69. it('reverts', async function () {
  70. await expectRevertCustomError(
  71. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  72. 'ERC20InsufficientBalance',
  73. [tokenOwner, amount - 1, amount],
  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 expectRevertCustomError(
  87. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  88. 'ERC20InsufficientAllowance',
  89. [spender, allowance, amount],
  90. );
  91. });
  92. });
  93. describe('when the token owner does not have enough balance', function () {
  94. const amount = allowance;
  95. beforeEach('reducing balance', async function () {
  96. await this.token.transfer(to, 2, { from: tokenOwner });
  97. });
  98. it('reverts', async function () {
  99. await expectRevertCustomError(
  100. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  101. 'ERC20InsufficientBalance',
  102. [tokenOwner, amount - 1, amount],
  103. );
  104. });
  105. });
  106. });
  107. describe('when the spender has unlimited allowance', function () {
  108. beforeEach(async function () {
  109. await this.token.approve(spender, MAX_UINT256, { from: initialHolder });
  110. });
  111. it('does not decrease the spender allowance', async function () {
  112. await this.token.transferFrom(tokenOwner, to, 1, { from: spender });
  113. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal(MAX_UINT256);
  114. });
  115. it('does not emit an approval event', async function () {
  116. expectEvent.notEmitted(await this.token.transferFrom(tokenOwner, to, 1, { from: spender }), 'Approval');
  117. });
  118. });
  119. });
  120. describe('when the recipient is the zero address', function () {
  121. const amount = initialSupply;
  122. const to = ZERO_ADDRESS;
  123. beforeEach(async function () {
  124. await this.token.approve(spender, amount, { from: tokenOwner });
  125. });
  126. it('reverts', async function () {
  127. await expectRevertCustomError(
  128. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  129. 'ERC20InvalidReceiver',
  130. [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 expectRevertCustomError(
  141. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  142. 'ERC20InvalidApprover',
  143. [ZERO_ADDRESS],
  144. );
  145. });
  146. });
  147. });
  148. describe('approve', function () {
  149. shouldBehaveLikeERC20Approve(initialHolder, recipient, initialSupply, function (owner, spender, amount) {
  150. return this.token.approve(spender, amount, { from: owner });
  151. });
  152. });
  153. }
  154. function shouldBehaveLikeERC20Transfer(from, to, balance, transfer) {
  155. describe('when the recipient is not the zero address', function () {
  156. describe('when the sender does not have enough balance', function () {
  157. const amount = balance.addn(1);
  158. it('reverts', async function () {
  159. await expectRevertCustomError(transfer.call(this, from, to, amount), 'ERC20InsufficientBalance', [
  160. from,
  161. balance,
  162. amount,
  163. ]);
  164. });
  165. });
  166. describe('when the sender transfers all balance', function () {
  167. const amount = balance;
  168. it('transfers the requested amount', async function () {
  169. await transfer.call(this, from, to, amount);
  170. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  171. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  172. });
  173. it('emits a transfer event', async function () {
  174. expectEvent(await transfer.call(this, from, to, amount), 'Transfer', { from, to, value: amount });
  175. });
  176. });
  177. describe('when the sender transfers zero tokens', function () {
  178. const amount = new BN('0');
  179. it('transfers the requested amount', async function () {
  180. await transfer.call(this, from, to, amount);
  181. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  182. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  183. });
  184. it('emits a transfer event', async function () {
  185. expectEvent(await transfer.call(this, from, to, amount), 'Transfer', { from, to, value: amount });
  186. });
  187. });
  188. });
  189. describe('when the recipient is the zero address', function () {
  190. it('reverts', async function () {
  191. await expectRevertCustomError(transfer.call(this, from, ZERO_ADDRESS, balance), 'ERC20InvalidReceiver', [
  192. ZERO_ADDRESS,
  193. ]);
  194. });
  195. });
  196. }
  197. function shouldBehaveLikeERC20Approve(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(await approve.call(this, owner, spender, amount), 'Approval', {
  203. owner: owner,
  204. spender: spender,
  205. 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(await approve.call(this, owner, spender, amount), 'Approval', {
  228. owner: owner,
  229. spender: spender,
  230. 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 expectRevertCustomError(approve.call(this, owner, ZERO_ADDRESS, supply), `ERC20InvalidSpender`, [
  253. ZERO_ADDRESS,
  254. ]);
  255. });
  256. });
  257. }
  258. module.exports = {
  259. shouldBehaveLikeERC20,
  260. shouldBehaveLikeERC20Transfer,
  261. shouldBehaveLikeERC20Approve,
  262. };