ERC20Wrapper.test.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { ZERO_ADDRESS, MAX_UINT256 } = constants;
  4. const { shouldBehaveLikeERC20 } = require('../ERC20.behavior');
  5. const ERC20Mock = artifacts.require('ERC20Mock');
  6. const ERC20WrapperMock = artifacts.require('ERC20WrapperMock');
  7. contract('ERC20', function (accounts) {
  8. const [ initialHolder, recipient, anotherAccount ] = accounts;
  9. const name = 'My Token';
  10. const symbol = 'MTKN';
  11. const initialSupply = new BN(100);
  12. beforeEach(async function () {
  13. this.underlying = await ERC20Mock.new(name, symbol, initialHolder, initialSupply);
  14. this.token = await ERC20WrapperMock.new(this.underlying.address, `Wrapped ${name}`, `W${symbol}`);
  15. });
  16. afterEach(async function () {
  17. expect(await this.underlying.balanceOf(this.token.address)).to.be.bignumber.equal(await this.token.totalSupply());
  18. });
  19. it('has a name', async function () {
  20. expect(await this.token.name()).to.equal(`Wrapped ${name}`);
  21. });
  22. it('has a symbol', async function () {
  23. expect(await this.token.symbol()).to.equal(`W${symbol}`);
  24. });
  25. it('has 18 decimals', async function () {
  26. expect(await this.token.decimals()).to.be.bignumber.equal('18');
  27. });
  28. it('has underlying', async function () {
  29. expect(await this.token.underlying()).to.be.bignumber.equal(this.underlying.address);
  30. });
  31. describe('deposit', function () {
  32. it('valid', async function () {
  33. await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });
  34. const { tx } = await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });
  35. expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  36. from: initialHolder,
  37. to: this.token.address,
  38. value: initialSupply,
  39. });
  40. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  41. from: ZERO_ADDRESS,
  42. to: initialHolder,
  43. value: initialSupply,
  44. });
  45. });
  46. it('missing approval', async function () {
  47. await expectRevert(
  48. this.token.depositFor(initialHolder, initialSupply, { from: initialHolder }),
  49. 'ERC20: transfer amount exceeds allowance',
  50. );
  51. });
  52. it('missing balance', async function () {
  53. await this.underlying.approve(this.token.address, MAX_UINT256, { from: initialHolder });
  54. await expectRevert(
  55. this.token.depositFor(initialHolder, MAX_UINT256, { from: initialHolder }),
  56. 'ERC20: transfer amount exceeds balance',
  57. );
  58. });
  59. it('to other account', async function () {
  60. await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });
  61. const { tx } = await this.token.depositFor(anotherAccount, initialSupply, { from: initialHolder });
  62. expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  63. from: initialHolder,
  64. to: this.token.address,
  65. value: initialSupply,
  66. });
  67. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  68. from: ZERO_ADDRESS,
  69. to: anotherAccount,
  70. value: initialSupply,
  71. });
  72. });
  73. });
  74. describe('withdraw', function () {
  75. beforeEach(async function () {
  76. await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });
  77. await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });
  78. });
  79. it('missing balance', async function () {
  80. await expectRevert(
  81. this.token.withdrawTo(initialHolder, MAX_UINT256, { from: initialHolder }),
  82. 'ERC20: burn amount exceeds balance',
  83. );
  84. });
  85. it('valid', async function () {
  86. const value = new BN(42);
  87. const { tx } = await this.token.withdrawTo(initialHolder, value, { from: initialHolder });
  88. expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  89. from: this.token.address,
  90. to: initialHolder,
  91. value: value,
  92. });
  93. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  94. from: initialHolder,
  95. to: ZERO_ADDRESS,
  96. value: value,
  97. });
  98. });
  99. it('entire balance', async function () {
  100. const { tx } = await this.token.withdrawTo(initialHolder, initialSupply, { from: initialHolder });
  101. expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  102. from: this.token.address,
  103. to: initialHolder,
  104. value: initialSupply,
  105. });
  106. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  107. from: initialHolder,
  108. to: ZERO_ADDRESS,
  109. value: initialSupply,
  110. });
  111. });
  112. it('to other account', async function () {
  113. const { tx } = await this.token.withdrawTo(anotherAccount, initialSupply, { from: initialHolder });
  114. expectEvent.inTransaction(tx, this.underlying, 'Transfer', {
  115. from: this.token.address,
  116. to: anotherAccount,
  117. value: initialSupply,
  118. });
  119. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  120. from: initialHolder,
  121. to: ZERO_ADDRESS,
  122. value: initialSupply,
  123. });
  124. });
  125. });
  126. describe('recover', function () {
  127. it('nothing to recover', async function () {
  128. await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });
  129. await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });
  130. const { tx } = await this.token.recover(anotherAccount);
  131. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  132. from: ZERO_ADDRESS,
  133. to: anotherAccount,
  134. value: '0',
  135. });
  136. });
  137. it('something to recover', async function () {
  138. await this.underlying.transfer(this.token.address, initialSupply, { from: initialHolder });
  139. const { tx } = await this.token.recover(anotherAccount);
  140. expectEvent.inTransaction(tx, this.token, 'Transfer', {
  141. from: ZERO_ADDRESS,
  142. to: anotherAccount,
  143. value: initialSupply,
  144. });
  145. });
  146. });
  147. describe('erc20 behaviour', function () {
  148. beforeEach(async function () {
  149. await this.underlying.approve(this.token.address, initialSupply, { from: initialHolder });
  150. await this.token.depositFor(initialHolder, initialSupply, { from: initialHolder });
  151. });
  152. shouldBehaveLikeERC20('ERC20', initialSupply, initialHolder, recipient, anotherAccount);
  153. });
  154. });