ERC20.behavior.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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(errorPrefix, 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(errorPrefix, 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. `${errorPrefix}InsufficientBalance`,
  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. `${errorPrefix}InsufficientAllowance`,
  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. `${errorPrefix}InsufficientBalance`,
  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. `${errorPrefix}InvalidReceiver`,
  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. `${errorPrefix}InvalidApprover`,
  143. [ZERO_ADDRESS],
  144. );
  145. });
  146. });
  147. });
  148. describe('approve', function () {
  149. shouldBehaveLikeERC20Approve(
  150. errorPrefix,
  151. initialHolder,
  152. recipient,
  153. initialSupply,
  154. function (owner, spender, amount) {
  155. return this.token.approve(spender, amount, { from: owner });
  156. },
  157. );
  158. });
  159. }
  160. function shouldBehaveLikeERC20Transfer(errorPrefix, from, to, balance, transfer) {
  161. describe('when the recipient is not the zero address', function () {
  162. describe('when the sender does not have enough balance', function () {
  163. const amount = balance.addn(1);
  164. it('reverts', async function () {
  165. await expectRevertCustomError(transfer.call(this, from, to, amount), `${errorPrefix}InsufficientBalance`, [
  166. from,
  167. balance,
  168. amount,
  169. ]);
  170. });
  171. });
  172. describe('when the sender transfers all balance', function () {
  173. const amount = balance;
  174. it('transfers the requested amount', async function () {
  175. await transfer.call(this, from, to, amount);
  176. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  177. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  178. });
  179. it('emits a transfer event', async function () {
  180. expectEvent(await transfer.call(this, from, to, amount), 'Transfer', { from, to, value: amount });
  181. });
  182. });
  183. describe('when the sender transfers zero tokens', function () {
  184. const amount = new BN('0');
  185. it('transfers the requested amount', async function () {
  186. await transfer.call(this, from, to, amount);
  187. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  188. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  189. });
  190. it('emits a transfer event', async function () {
  191. expectEvent(await transfer.call(this, from, to, amount), 'Transfer', { from, to, value: amount });
  192. });
  193. });
  194. });
  195. describe('when the recipient is the zero address', function () {
  196. it('reverts', async function () {
  197. await expectRevertCustomError(transfer.call(this, from, ZERO_ADDRESS, balance), `${errorPrefix}InvalidReceiver`, [
  198. ZERO_ADDRESS,
  199. ]);
  200. });
  201. });
  202. }
  203. function shouldBehaveLikeERC20Approve(errorPrefix, owner, spender, supply, approve) {
  204. describe('when the spender is not the zero address', function () {
  205. describe('when the sender has enough balance', function () {
  206. const amount = supply;
  207. it('emits an approval event', async function () {
  208. expectEvent(await approve.call(this, owner, spender, amount), 'Approval', {
  209. owner: owner,
  210. spender: spender,
  211. value: amount,
  212. });
  213. });
  214. describe('when there was no approved amount before', function () {
  215. it('approves the requested amount', async function () {
  216. await approve.call(this, owner, spender, amount);
  217. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  218. });
  219. });
  220. describe('when the spender had an approved amount', function () {
  221. beforeEach(async function () {
  222. await approve.call(this, owner, spender, new BN(1));
  223. });
  224. it('approves the requested amount and replaces the previous one', async function () {
  225. await approve.call(this, owner, spender, amount);
  226. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  227. });
  228. });
  229. });
  230. describe('when the sender does not have enough balance', function () {
  231. const amount = supply.addn(1);
  232. it('emits an approval event', async function () {
  233. expectEvent(await approve.call(this, owner, spender, amount), 'Approval', {
  234. owner: owner,
  235. spender: spender,
  236. value: amount,
  237. });
  238. });
  239. describe('when there was no approved amount before', function () {
  240. it('approves the requested amount', async function () {
  241. await approve.call(this, owner, spender, amount);
  242. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  243. });
  244. });
  245. describe('when the spender had an approved amount', function () {
  246. beforeEach(async function () {
  247. await approve.call(this, owner, spender, new BN(1));
  248. });
  249. it('approves the requested amount and replaces the previous one', async function () {
  250. await approve.call(this, owner, spender, amount);
  251. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  252. });
  253. });
  254. });
  255. });
  256. describe('when the spender is the zero address', function () {
  257. it('reverts', async function () {
  258. await expectRevertCustomError(approve.call(this, owner, ZERO_ADDRESS, supply), `ERC20InvalidSpender`, [
  259. ZERO_ADDRESS,
  260. ]);
  261. });
  262. });
  263. }
  264. module.exports = {
  265. shouldBehaveLikeERC20,
  266. shouldBehaveLikeERC20Transfer,
  267. shouldBehaveLikeERC20Approve,
  268. };