ERC20.behavior.js 10 KB

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