ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { ZERO_ADDRESS, MAX_UINT256 } = constants;
  4. function shouldBehaveLikeERC20(errorPrefix, initialSupply, initialHolder, recipient, anotherAccount) {
  5. describe('total supply', function () {
  6. it('returns the total amount of tokens', async function () {
  7. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  8. });
  9. });
  10. describe('balanceOf', function () {
  11. describe('when the requested account has no tokens', function () {
  12. it('returns zero', async function () {
  13. expect(await this.token.balanceOf(anotherAccount)).to.be.bignumber.equal('0');
  14. });
  15. });
  16. describe('when the requested account has some tokens', function () {
  17. it('returns the total amount of tokens', async function () {
  18. expect(await this.token.balanceOf(initialHolder)).to.be.bignumber.equal(initialSupply);
  19. });
  20. });
  21. });
  22. describe('transfer', function () {
  23. shouldBehaveLikeERC20Transfer(errorPrefix, initialHolder, recipient, initialSupply, function (from, to, value) {
  24. return this.token.transfer(to, value, { from });
  25. });
  26. });
  27. describe('transfer from', function () {
  28. const spender = recipient;
  29. describe('when the token owner is not the zero address', function () {
  30. const tokenOwner = initialHolder;
  31. describe('when the recipient is not the zero address', function () {
  32. const to = anotherAccount;
  33. describe('when the spender has enough allowance', function () {
  34. beforeEach(async function () {
  35. await this.token.approve(spender, initialSupply, { from: initialHolder });
  36. });
  37. describe('when the token owner has enough balance', function () {
  38. const amount = initialSupply;
  39. it('transfers the requested amount', async function () {
  40. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  41. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('0');
  42. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  43. });
  44. it('decreases the spender allowance', async function () {
  45. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  46. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal('0');
  47. });
  48. it('emits a transfer event', async function () {
  49. expectEvent(await this.token.transferFrom(tokenOwner, to, amount, { from: spender }), 'Transfer', {
  50. from: tokenOwner,
  51. to: to,
  52. value: amount,
  53. });
  54. });
  55. it('emits an approval event', async function () {
  56. expectEvent(await this.token.transferFrom(tokenOwner, to, amount, { from: spender }), 'Approval', {
  57. owner: tokenOwner,
  58. spender: spender,
  59. value: await this.token.allowance(tokenOwner, spender),
  60. });
  61. });
  62. });
  63. describe('when the token owner does not have enough balance', function () {
  64. const amount = initialSupply;
  65. beforeEach('reducing balance', async function () {
  66. await this.token.transfer(to, 1, { from: tokenOwner });
  67. });
  68. it('reverts', async function () {
  69. await expectRevert(
  70. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  71. `${errorPrefix}: transfer amount exceeds balance`,
  72. );
  73. });
  74. });
  75. });
  76. describe('when the spender does not have enough allowance', function () {
  77. const allowance = initialSupply.subn(1);
  78. beforeEach(async function () {
  79. await this.token.approve(spender, allowance, { from: tokenOwner });
  80. });
  81. describe('when the token owner has enough balance', function () {
  82. const amount = initialSupply;
  83. it('reverts', async function () {
  84. await expectRevert(
  85. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  86. `${errorPrefix}: insufficient allowance`,
  87. );
  88. });
  89. });
  90. describe('when the token owner does not have enough balance', function () {
  91. const amount = allowance;
  92. beforeEach('reducing balance', async function () {
  93. await this.token.transfer(to, 2, { from: tokenOwner });
  94. });
  95. it('reverts', async function () {
  96. await expectRevert(
  97. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  98. `${errorPrefix}: transfer amount exceeds balance`,
  99. );
  100. });
  101. });
  102. });
  103. describe('when the spender has unlimited allowance', function () {
  104. beforeEach(async function () {
  105. await this.token.approve(spender, MAX_UINT256, { from: initialHolder });
  106. });
  107. it('does not decrease the spender allowance', async function () {
  108. await this.token.transferFrom(tokenOwner, to, 1, { from: spender });
  109. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal(MAX_UINT256);
  110. });
  111. it('does not emit an approval event', async function () {
  112. expectEvent.notEmitted(await this.token.transferFrom(tokenOwner, to, 1, { from: spender }), 'Approval');
  113. });
  114. });
  115. });
  116. describe('when the recipient is the zero address', function () {
  117. const amount = initialSupply;
  118. const to = ZERO_ADDRESS;
  119. beforeEach(async function () {
  120. await this.token.approve(spender, amount, { from: tokenOwner });
  121. });
  122. it('reverts', async function () {
  123. await expectRevert(
  124. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  125. `${errorPrefix}: transfer to the zero address`,
  126. );
  127. });
  128. });
  129. });
  130. describe('when the token owner is the zero address', function () {
  131. const amount = 0;
  132. const tokenOwner = ZERO_ADDRESS;
  133. const to = recipient;
  134. it('reverts', async function () {
  135. await expectRevert(this.token.transferFrom(tokenOwner, to, amount, { from: spender }), 'from the zero address');
  136. });
  137. });
  138. });
  139. describe('approve', function () {
  140. shouldBehaveLikeERC20Approve(
  141. errorPrefix,
  142. initialHolder,
  143. recipient,
  144. initialSupply,
  145. function (owner, spender, amount) {
  146. return this.token.approve(spender, amount, { from: owner });
  147. },
  148. );
  149. });
  150. }
  151. function shouldBehaveLikeERC20Transfer(errorPrefix, from, to, balance, transfer) {
  152. describe('when the recipient is not the zero address', function () {
  153. describe('when the sender does not have enough balance', function () {
  154. const amount = balance.addn(1);
  155. it('reverts', async function () {
  156. await expectRevert(transfer.call(this, from, to, amount), `${errorPrefix}: transfer amount exceeds balance`);
  157. });
  158. });
  159. describe('when the sender transfers all balance', function () {
  160. const amount = balance;
  161. it('transfers the requested amount', async function () {
  162. await transfer.call(this, from, to, amount);
  163. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  164. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  165. });
  166. it('emits a transfer event', async function () {
  167. expectEvent(await transfer.call(this, from, to, amount), 'Transfer', { from, to, value: amount });
  168. });
  169. });
  170. describe('when the sender transfers zero tokens', function () {
  171. const amount = new BN('0');
  172. it('transfers the requested amount', async function () {
  173. await transfer.call(this, from, to, amount);
  174. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  175. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  176. });
  177. it('emits a transfer event', async function () {
  178. expectEvent(await transfer.call(this, from, to, amount), 'Transfer', { from, to, value: amount });
  179. });
  180. });
  181. });
  182. describe('when the recipient is the zero address', function () {
  183. it('reverts', async function () {
  184. await expectRevert(
  185. transfer.call(this, from, ZERO_ADDRESS, balance),
  186. `${errorPrefix}: transfer to the zero address`,
  187. );
  188. });
  189. });
  190. }
  191. function shouldBehaveLikeERC20Approve(errorPrefix, owner, spender, supply, approve) {
  192. describe('when the spender is not the zero address', function () {
  193. describe('when the sender has enough balance', function () {
  194. const amount = supply;
  195. it('emits an approval event', async function () {
  196. expectEvent(await approve.call(this, owner, spender, amount), 'Approval', {
  197. owner: owner,
  198. spender: spender,
  199. value: amount,
  200. });
  201. });
  202. describe('when there was no approved amount before', function () {
  203. it('approves the requested amount', async function () {
  204. await approve.call(this, owner, spender, amount);
  205. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  206. });
  207. });
  208. describe('when the spender had an approved amount', function () {
  209. beforeEach(async function () {
  210. await approve.call(this, owner, spender, new BN(1));
  211. });
  212. it('approves the requested amount and replaces the previous one', async function () {
  213. await approve.call(this, owner, spender, amount);
  214. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  215. });
  216. });
  217. });
  218. describe('when the sender does not have enough balance', function () {
  219. const amount = supply.addn(1);
  220. it('emits an approval event', async function () {
  221. expectEvent(await approve.call(this, owner, spender, amount), 'Approval', {
  222. owner: owner,
  223. spender: spender,
  224. value: amount,
  225. });
  226. });
  227. describe('when there was no approved amount before', function () {
  228. it('approves the requested amount', async function () {
  229. await approve.call(this, owner, spender, amount);
  230. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  231. });
  232. });
  233. describe('when the spender had an approved amount', function () {
  234. beforeEach(async function () {
  235. await approve.call(this, owner, spender, new BN(1));
  236. });
  237. it('approves the requested amount and replaces the previous one', async function () {
  238. await approve.call(this, owner, spender, amount);
  239. expect(await this.token.allowance(owner, spender)).to.be.bignumber.equal(amount);
  240. });
  241. });
  242. });
  243. });
  244. describe('when the spender is the zero address', function () {
  245. it('reverts', async function () {
  246. await expectRevert(
  247. approve.call(this, owner, ZERO_ADDRESS, supply),
  248. `${errorPrefix}: approve to the zero address`,
  249. );
  250. });
  251. });
  252. }
  253. module.exports = {
  254. shouldBehaveLikeERC20,
  255. shouldBehaveLikeERC20Transfer,
  256. shouldBehaveLikeERC20Approve,
  257. };