ERC20.behavior.js 10 KB

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