ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // transferFrom does a spendAllowance before moving the assets
  112. // - default behavior (ERC20) is to always update the approval using `_approve`. This will fail because the
  113. // approver (owner) is address(0). This happens even if the amount transferred is zero, and the approval update
  114. // is not actually necessary.
  115. // - in ERC20TemporaryAllowance, transfer of 0 value will not update allowance (temporary or persistent)
  116. // therefore the spendAllowance does not revert. However, the transfer of asset will revert because the sender
  117. // is address(0)
  118. const errorName = this.token.temporaryApprove ? 'ERC20InvalidSender' : 'ERC20InvalidApprover';
  119. const value = 0n;
  120. await expect(this.token.connect(this.recipient).transferFrom(ethers.ZeroAddress, this.recipient, value))
  121. .to.be.revertedWithCustomError(this.token, errorName)
  122. .withArgs(ethers.ZeroAddress);
  123. });
  124. });
  125. describe('approve', function () {
  126. beforeEach(function () {
  127. this.approve = (owner, spender, value) => this.token.connect(owner).approve(spender, value);
  128. });
  129. shouldBehaveLikeERC20Approve(initialSupply);
  130. });
  131. }
  132. function shouldBehaveLikeERC20Transfer(balance) {
  133. describe('when the recipient is not the zero address', function () {
  134. it('reverts when the sender does not have enough balance', async function () {
  135. const value = balance + 1n;
  136. await expect(this.transfer(this.holder, this.recipient, value))
  137. .to.be.revertedWithCustomError(this.token, 'ERC20InsufficientBalance')
  138. .withArgs(this.holder, balance, value);
  139. });
  140. describe('when the sender transfers all balance', function () {
  141. const value = balance;
  142. beforeEach(async function () {
  143. this.tx = await this.transfer(this.holder, this.recipient, value);
  144. });
  145. it('transfers the requested value', async function () {
  146. await expect(this.tx).to.changeTokenBalances(this.token, [this.holder, this.recipient], [-value, value]);
  147. });
  148. it('emits a transfer event', async function () {
  149. await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.recipient, value);
  150. });
  151. });
  152. describe('when the sender transfers zero tokens', function () {
  153. const value = 0n;
  154. beforeEach(async function () {
  155. this.tx = await this.transfer(this.holder, this.recipient, value);
  156. });
  157. it('transfers the requested value', async function () {
  158. await expect(this.tx).to.changeTokenBalances(this.token, [this.holder, this.recipient], [0n, 0n]);
  159. });
  160. it('emits a transfer event', async function () {
  161. await expect(this.tx).to.emit(this.token, 'Transfer').withArgs(this.holder, this.recipient, value);
  162. });
  163. });
  164. });
  165. it('reverts when the recipient is the zero address', async function () {
  166. await expect(this.transfer(this.holder, ethers.ZeroAddress, balance))
  167. .to.be.revertedWithCustomError(this.token, 'ERC20InvalidReceiver')
  168. .withArgs(ethers.ZeroAddress);
  169. });
  170. }
  171. function shouldBehaveLikeERC20Approve(supply) {
  172. describe('when the spender is not the zero address', function () {
  173. describe('when the sender has enough balance', function () {
  174. const value = supply;
  175. it('emits an approval event', async function () {
  176. await expect(this.approve(this.holder, this.recipient, value))
  177. .to.emit(this.token, 'Approval')
  178. .withArgs(this.holder, this.recipient, value);
  179. });
  180. it('approves the requested value when there was no approved value before', async function () {
  181. await this.approve(this.holder, this.recipient, value);
  182. expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
  183. });
  184. it('approves the requested value and replaces the previous one when the spender had an approved value', async function () {
  185. await this.approve(this.holder, this.recipient, 1n);
  186. await this.approve(this.holder, this.recipient, value);
  187. expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
  188. });
  189. });
  190. describe('when the sender does not have enough balance', function () {
  191. const value = supply + 1n;
  192. it('emits an approval event', async function () {
  193. await expect(this.approve(this.holder, this.recipient, value))
  194. .to.emit(this.token, 'Approval')
  195. .withArgs(this.holder, this.recipient, value);
  196. });
  197. it('approves the requested value when there was no approved value before', async function () {
  198. await this.approve(this.holder, this.recipient, value);
  199. expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
  200. });
  201. it('approves the requested value and replaces the previous one when the spender had an approved value', async function () {
  202. await this.approve(this.holder, this.recipient, 1n);
  203. await this.approve(this.holder, this.recipient, value);
  204. expect(await this.token.allowance(this.holder, this.recipient)).to.equal(value);
  205. });
  206. });
  207. });
  208. it('reverts when the spender is the zero address', async function () {
  209. await expect(this.approve(this.holder, ethers.ZeroAddress, supply))
  210. .to.be.revertedWithCustomError(this.token, `ERC20InvalidSpender`)
  211. .withArgs(ethers.ZeroAddress);
  212. });
  213. }
  214. module.exports = {
  215. shouldBehaveLikeERC20,
  216. shouldBehaveLikeERC20Transfer,
  217. shouldBehaveLikeERC20Approve,
  218. };