ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. function shouldBehaveLikeERC20(initialSupply, opts = {}) {
  4. const { forcedApproval } = opts;
  5. it('total supply: returns the total token value', async function () {
  6. expect(await this.token.totalSupply()).to.equal(initialSupply);
  7. });
  8. describe('balanceOf', function () {
  9. it('returns zero when the requested account has no tokens', async function () {
  10. expect(await this.token.balanceOf(this.anotherAccount)).to.equal(0n);
  11. });
  12. it('returns the total token value when the requested account has some tokens', async function () {
  13. expect(await this.token.balanceOf(this.initialHolder)).to.equal(initialSupply);
  14. });
  15. });
  16. describe('transfer', function () {
  17. beforeEach(function () {
  18. this.transfer = (from, to, value) => this.token.connect(from).transfer(to, value);
  19. });
  20. shouldBehaveLikeERC20Transfer(initialSupply);
  21. });
  22. describe('transfer from', function () {
  23. describe('when the token owner is not the zero address', function () {
  24. describe('when the recipient is not the zero address', function () {
  25. describe('when the spender has enough allowance', function () {
  26. beforeEach(async function () {
  27. await this.token.connect(this.initialHolder).approve(this.recipient, initialSupply);
  28. });
  29. describe('when the token owner has enough balance', function () {
  30. const value = initialSupply;
  31. beforeEach(async function () {
  32. this.tx = await this.token
  33. .connect(this.recipient)
  34. .transferFrom(this.initialHolder, this.anotherAccount, value);
  35. });
  36. it('transfers the requested value', async function () {
  37. await expect(this.tx).to.changeTokenBalances(
  38. this.token,
  39. [this.initialHolder, this.anotherAccount],
  40. [-value, value],
  41. );
  42. });
  43. it('decreases the spender allowance', async function () {
  44. expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(0n);
  45. });
  46. it('emits a transfer event', async function () {
  47. await expect(this.tx)
  48. .to.emit(this.token, 'Transfer')
  49. .withArgs(this.initialHolder.address, this.anotherAccount.address, value);
  50. });
  51. if (forcedApproval) {
  52. it('emits an approval event', async function () {
  53. await expect(this.tx)
  54. .to.emit(this.token, 'Approval')
  55. .withArgs(
  56. this.initialHolder.address,
  57. this.recipient.address,
  58. await this.token.allowance(this.initialHolder, this.recipient),
  59. );
  60. });
  61. } else {
  62. it('does not emit an approval event', async function () {
  63. await expect(this.tx).to.not.emit(this.token, 'Approval');
  64. });
  65. }
  66. });
  67. it('reverts when the token owner does not have enough balance', async function () {
  68. const value = initialSupply;
  69. await this.token.connect(this.initialHolder).transfer(this.anotherAccount, 1n);
  70. await expect(
  71. this.token.connect(this.recipient).transferFrom(this.initialHolder, this.anotherAccount, value),
  72. )
  73. .to.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  74. .withArgs(this.initialHolder.address, value - 1n, value);
  75. });
  76. });
  77. describe('when the spender does not have enough allowance', function () {
  78. const allowance = initialSupply - 1n;
  79. beforeEach(async function () {
  80. await this.token.connect(this.initialHolder).approve(this.recipient, allowance);
  81. });
  82. it('reverts when the token owner has enough balance', async function () {
  83. const value = initialSupply;
  84. await expect(
  85. this.token.connect(this.recipient).transferFrom(this.initialHolder, this.anotherAccount, value),
  86. )
  87. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientAllowance')
  88. .withArgs(this.recipient.address, allowance, value);
  89. });
  90. it('reverts when the token owner does not have enough balance', async function () {
  91. const value = allowance;
  92. await this.token.connect(this.initialHolder).transfer(this.anotherAccount, 2);
  93. await expect(
  94. this.token.connect(this.recipient).transferFrom(this.initialHolder, this.anotherAccount, value),
  95. )
  96. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  97. .withArgs(this.initialHolder.address, value - 1n, value);
  98. });
  99. });
  100. describe('when the spender has unlimited allowance', function () {
  101. beforeEach(async function () {
  102. await this.token.connect(this.initialHolder).approve(this.recipient, ethers.MaxUint256);
  103. this.tx = await this.token
  104. .connect(this.recipient)
  105. .transferFrom(this.initialHolder, this.anotherAccount, 1n);
  106. });
  107. it('does not decrease the spender allowance', async function () {
  108. expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(ethers.MaxUint256);
  109. });
  110. it('does not emit an approval event', async function () {
  111. await expect(this.tx).to.not.emit(this.token, 'Approval');
  112. });
  113. });
  114. });
  115. it('reverts when the recipient is the zero address', async function () {
  116. const value = initialSupply;
  117. await this.token.connect(this.initialHolder).approve(this.recipient, value);
  118. await expect(this.token.connect(this.recipient).transferFrom(this.initialHolder, ethers.ZeroAddress, value))
  119. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  120. .withArgs(ethers.ZeroAddress);
  121. });
  122. });
  123. it('reverts when the token owner is the zero address', async function () {
  124. const value = 0n;
  125. await expect(this.token.connect(this.recipient).transferFrom(ethers.ZeroAddress, this.recipient, value))
  126. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidApprover')
  127. .withArgs(ethers.ZeroAddress);
  128. });
  129. });
  130. describe('approve', function () {
  131. beforeEach(function () {
  132. this.approve = (owner, spender, value) => this.token.connect(owner).approve(spender, value);
  133. });
  134. shouldBehaveLikeERC20Approve(initialSupply);
  135. });
  136. }
  137. function shouldBehaveLikeERC20Transfer(balance) {
  138. describe('when the recipient is not the zero address', function () {
  139. it('reverts when the sender does not have enough balance', async function () {
  140. const value = balance + 1n;
  141. await expect(this.transfer(this.initialHolder, this.recipient, value))
  142. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  143. .withArgs(this.initialHolder.address, balance, value);
  144. });
  145. describe('when the sender transfers all balance', function () {
  146. const value = balance;
  147. beforeEach(async function () {
  148. this.tx = await this.transfer(this.initialHolder, this.recipient, value);
  149. });
  150. it('transfers the requested value', async function () {
  151. await expect(this.tx).to.changeTokenBalances(this.token, [this.initialHolder, this.recipient], [-value, value]);
  152. });
  153. it('emits a transfer event', async function () {
  154. await expect(this.tx)
  155. .to.emit(this.token, 'Transfer')
  156. .withArgs(this.initialHolder.address, this.recipient.address, value);
  157. });
  158. });
  159. describe('when the sender transfers zero tokens', function () {
  160. const value = 0n;
  161. beforeEach(async function () {
  162. this.tx = await this.transfer(this.initialHolder, this.recipient, value);
  163. });
  164. it('transfers the requested value', async function () {
  165. await expect(this.tx).to.changeTokenBalances(this.token, [this.initialHolder, this.recipient], [0n, 0n]);
  166. });
  167. it('emits a transfer event', async function () {
  168. await expect(this.tx)
  169. .to.emit(this.token, 'Transfer')
  170. .withArgs(this.initialHolder.address, this.recipient.address, value);
  171. });
  172. });
  173. });
  174. it('reverts when the recipient is the zero address', async function () {
  175. await expect(this.transfer(this.initialHolder, ethers.ZeroAddress, balance))
  176. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  177. .withArgs(ethers.ZeroAddress);
  178. });
  179. }
  180. function shouldBehaveLikeERC20Approve(supply) {
  181. describe('when the spender is not the zero address', function () {
  182. describe('when the sender has enough balance', function () {
  183. const value = supply;
  184. it('emits an approval event', async function () {
  185. await expect(this.approve(this.initialHolder, this.recipient, value))
  186. .to.emit(this.token, 'Approval')
  187. .withArgs(this.initialHolder.address, this.recipient.address, value);
  188. });
  189. it('approves the requested value when there was no approved value before', async function () {
  190. await this.approve(this.initialHolder, this.recipient, value);
  191. expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(value);
  192. });
  193. it('approves the requested value and replaces the previous one when the spender had an approved value', async function () {
  194. await this.approve(this.initialHolder, this.recipient, 1n);
  195. await this.approve(this.initialHolder, this.recipient, value);
  196. expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(value);
  197. });
  198. });
  199. describe('when the sender does not have enough balance', function () {
  200. const value = supply + 1n;
  201. it('emits an approval event', async function () {
  202. await expect(this.approve(this.initialHolder, this.recipient, value))
  203. .to.emit(this.token, 'Approval')
  204. .withArgs(this.initialHolder.address, this.recipient.address, value);
  205. });
  206. it('approves the requested value when there was no approved value before', async function () {
  207. await this.approve(this.initialHolder, this.recipient, value);
  208. expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(value);
  209. });
  210. it('approves the requested value and replaces the previous one when the spender had an approved value', async function () {
  211. await this.approve(this.initialHolder, this.recipient, 1n);
  212. await this.approve(this.initialHolder, this.recipient, value);
  213. expect(await this.token.allowance(this.initialHolder, this.recipient)).to.equal(value);
  214. });
  215. });
  216. });
  217. it('reverts when the spender is the zero address', async function () {
  218. await expect(this.approve(this.initialHolder, ethers.ZeroAddress, supply))
  219. .to.be.revertedWithCustomError(this.token, `ERC20InvalidSpender`)
  220. .withArgs(ethers.ZeroAddress);
  221. });
  222. }
  223. module.exports = {
  224. shouldBehaveLikeERC20,
  225. shouldBehaveLikeERC20Transfer,
  226. shouldBehaveLikeERC20Approve,
  227. };