ERC20.behavior.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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,
  24. function (from, to, value) {
  25. return this.token.transfer(to, value, { from });
  26. },
  27. );
  28. });
  29. describe('transfer from', function () {
  30. const spender = recipient;
  31. describe('when the token owner is not the zero address', function () {
  32. const tokenOwner = initialHolder;
  33. describe('when the recipient is not the zero address', function () {
  34. const to = anotherAccount;
  35. describe('when the spender has enough allowance', function () {
  36. beforeEach(async function () {
  37. await this.token.approve(spender, initialSupply, { from: initialHolder });
  38. });
  39. describe('when the token owner has enough balance', function () {
  40. const amount = initialSupply;
  41. it('transfers the requested amount', async function () {
  42. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  43. expect(await this.token.balanceOf(tokenOwner)).to.be.bignumber.equal('0');
  44. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  45. });
  46. it('decreases the spender allowance', async function () {
  47. await this.token.transferFrom(tokenOwner, to, amount, { from: spender });
  48. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal('0');
  49. });
  50. it('emits a transfer event', async function () {
  51. expectEvent(
  52. await this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  53. 'Transfer',
  54. { from: tokenOwner, to: to, value: amount },
  55. );
  56. });
  57. it('emits an approval event', async function () {
  58. expectEvent(
  59. await this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  60. 'Approval',
  61. { owner: tokenOwner, spender: spender, value: await this.token.allowance(tokenOwner, spender) },
  62. );
  63. });
  64. });
  65. describe('when the token owner does not have enough balance', function () {
  66. const amount = initialSupply;
  67. beforeEach('reducing balance', async function () {
  68. await this.token.transfer(to, 1, { from: tokenOwner });
  69. });
  70. it('reverts', async function () {
  71. await expectRevert(
  72. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  73. `${errorPrefix}: transfer amount exceeds balance`,
  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 expectRevert(
  87. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  88. `${errorPrefix}: insufficient allowance`,
  89. );
  90. });
  91. });
  92. describe('when the token owner does not have enough balance', function () {
  93. const amount = allowance;
  94. beforeEach('reducing balance', async function () {
  95. await this.token.transfer(to, 2, { from: tokenOwner });
  96. });
  97. it('reverts', async function () {
  98. await expectRevert(
  99. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  100. `${errorPrefix}: transfer amount exceeds balance`,
  101. );
  102. });
  103. });
  104. });
  105. describe('when the spender has unlimited allowance', function () {
  106. beforeEach(async function () {
  107. await this.token.approve(spender, MAX_UINT256, { from: initialHolder });
  108. });
  109. it('does not decrease the spender allowance', async function () {
  110. await this.token.transferFrom(tokenOwner, to, 1, { from: spender });
  111. expect(await this.token.allowance(tokenOwner, spender)).to.be.bignumber.equal(MAX_UINT256);
  112. });
  113. it('does not emit an approval event', async function () {
  114. expectEvent.notEmitted(
  115. await this.token.transferFrom(tokenOwner, to, 1, { from: spender }),
  116. 'Approval',
  117. );
  118. });
  119. });
  120. });
  121. describe('when the recipient is the zero address', function () {
  122. const amount = initialSupply;
  123. const to = ZERO_ADDRESS;
  124. beforeEach(async function () {
  125. await this.token.approve(spender, amount, { from: tokenOwner });
  126. });
  127. it('reverts', async function () {
  128. await expectRevert(this.token.transferFrom(
  129. tokenOwner, to, amount, { from: spender }), `${errorPrefix}: transfer to the zero address`,
  130. );
  131. });
  132. });
  133. });
  134. describe('when the token owner is the zero address', function () {
  135. const amount = 0;
  136. const tokenOwner = ZERO_ADDRESS;
  137. const to = recipient;
  138. it('reverts', async function () {
  139. await expectRevert(
  140. this.token.transferFrom(tokenOwner, to, amount, { from: spender }),
  141. 'from the zero address',
  142. );
  143. });
  144. });
  145. });
  146. describe('approve', function () {
  147. shouldBehaveLikeERC20Approve(errorPrefix, initialHolder, recipient, initialSupply,
  148. function (owner, spender, amount) {
  149. return this.token.approve(spender, amount, { from: owner });
  150. },
  151. );
  152. });
  153. }
  154. function shouldBehaveLikeERC20Transfer (errorPrefix, from, to, balance, transfer) {
  155. describe('when the recipient is not the zero address', function () {
  156. describe('when the sender does not have enough balance', function () {
  157. const amount = balance.addn(1);
  158. it('reverts', async function () {
  159. await expectRevert(transfer.call(this, from, to, amount),
  160. `${errorPrefix}: transfer amount exceeds balance`,
  161. );
  162. });
  163. });
  164. describe('when the sender transfers all balance', function () {
  165. const amount = balance;
  166. it('transfers the requested amount', async function () {
  167. await transfer.call(this, from, to, amount);
  168. expect(await this.token.balanceOf(from)).to.be.bignumber.equal('0');
  169. expect(await this.token.balanceOf(to)).to.be.bignumber.equal(amount);
  170. });
  171. it('emits a transfer event', async function () {
  172. expectEvent(
  173. await transfer.call(this, from, to, amount),
  174. 'Transfer',
  175. { from, to, value: amount },
  176. );
  177. });
  178. });
  179. describe('when the sender transfers zero tokens', function () {
  180. const amount = new BN('0');
  181. it('transfers the requested amount', async function () {
  182. await transfer.call(this, from, to, amount);
  183. expect(await this.token.balanceOf(from)).to.be.bignumber.equal(balance);
  184. expect(await this.token.balanceOf(to)).to.be.bignumber.equal('0');
  185. });
  186. it('emits a transfer event', async function () {
  187. expectEvent(
  188. await transfer.call(this, from, to, amount),
  189. 'Transfer',
  190. { from, to, value: amount },
  191. );
  192. });
  193. });
  194. });
  195. describe('when the recipient is the zero address', function () {
  196. it('reverts', async function () {
  197. await expectRevert(transfer.call(this, from, ZERO_ADDRESS, balance),
  198. `${errorPrefix}: transfer to the 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(
  209. await approve.call(this, owner, spender, amount),
  210. 'Approval',
  211. { owner: owner, spender: spender, 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(
  234. await approve.call(this, owner, spender, amount),
  235. 'Approval',
  236. { owner: owner, spender: spender, 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 expectRevert(approve.call(this, owner, ZERO_ADDRESS, supply),
  259. `${errorPrefix}: approve to the zero address`,
  260. );
  261. });
  262. });
  263. }
  264. module.exports = {
  265. shouldBehaveLikeERC20,
  266. shouldBehaveLikeERC20Transfer,
  267. shouldBehaveLikeERC20Approve,
  268. };