ERC20FlashMint.test.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* eslint-disable */
  2. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const { MAX_UINT256, ZERO_ADDRESS } = constants;
  5. const ERC20FlashMintMock = artifacts.require('$ERC20FlashMintMock');
  6. const ERC3156FlashBorrowerMock = artifacts.require('ERC3156FlashBorrowerMock');
  7. contract('ERC20FlashMint', function (accounts) {
  8. const [initialHolder, other, anotherAccount] = accounts;
  9. const name = 'My Token';
  10. const symbol = 'MTKN';
  11. const initialSupply = new BN(100);
  12. const loanAmount = new BN(10000000000000);
  13. beforeEach(async function () {
  14. this.token = await ERC20FlashMintMock.new(name, symbol);
  15. await this.token.$_mint(initialHolder, initialSupply);
  16. });
  17. describe('maxFlashLoan', function () {
  18. it('token match', async function () {
  19. expect(await this.token.maxFlashLoan(this.token.address)).to.be.bignumber.equal(MAX_UINT256.sub(initialSupply));
  20. });
  21. it('token mismatch', async function () {
  22. expect(await this.token.maxFlashLoan(ZERO_ADDRESS)).to.be.bignumber.equal('0');
  23. });
  24. });
  25. describe('flashFee', function () {
  26. it('token match', async function () {
  27. expect(await this.token.flashFee(this.token.address, loanAmount)).to.be.bignumber.equal('0');
  28. });
  29. it('token mismatch', async function () {
  30. await expectRevert(this.token.flashFee(ZERO_ADDRESS, loanAmount), 'ERC20FlashMint: wrong token');
  31. });
  32. });
  33. describe('flashFeeReceiver', function () {
  34. it('default receiver', async function () {
  35. expect(await this.token.$_flashFeeReceiver()).to.be.eq(ZERO_ADDRESS);
  36. });
  37. });
  38. describe('flashLoan', function () {
  39. it('success', async function () {
  40. const receiver = await ERC3156FlashBorrowerMock.new(true, true);
  41. const { tx } = await this.token.flashLoan(receiver.address, this.token.address, loanAmount, '0x');
  42. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: ZERO_ADDRESS, to: receiver.address, value: loanAmount });
  43. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: receiver.address, to: ZERO_ADDRESS, value: loanAmount });
  44. await expectEvent.inTransaction(tx, receiver, 'BalanceOf', { token: this.token.address, account: receiver.address, value: loanAmount });
  45. await expectEvent.inTransaction(tx, receiver, 'TotalSupply', { token: this.token.address, value: initialSupply.add(loanAmount) });
  46. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  47. expect(await this.token.balanceOf(receiver.address)).to.be.bignumber.equal('0');
  48. expect(await this.token.allowance(receiver.address, this.token.address)).to.be.bignumber.equal('0');
  49. });
  50. it('missing return value', async function () {
  51. const receiver = await ERC3156FlashBorrowerMock.new(false, true);
  52. await expectRevert(
  53. this.token.flashLoan(receiver.address, this.token.address, loanAmount, '0x'),
  54. 'ERC20FlashMint: invalid return value',
  55. );
  56. });
  57. it('missing approval', async function () {
  58. const receiver = await ERC3156FlashBorrowerMock.new(true, false);
  59. await expectRevert(
  60. this.token.flashLoan(receiver.address, this.token.address, loanAmount, '0x'),
  61. 'ERC20: insufficient allowance',
  62. );
  63. });
  64. it('unavailable funds', async function () {
  65. const receiver = await ERC3156FlashBorrowerMock.new(true, true);
  66. const data = this.token.contract.methods.transfer(other, 10).encodeABI();
  67. await expectRevert(
  68. this.token.flashLoan(receiver.address, this.token.address, loanAmount, data),
  69. 'ERC20: burn amount exceeds balance',
  70. );
  71. });
  72. it('more than maxFlashLoan', async function () {
  73. const receiver = await ERC3156FlashBorrowerMock.new(true, true);
  74. const data = this.token.contract.methods.transfer(other, 10).encodeABI();
  75. // _mint overflow reverts using a panic code. No reason string.
  76. await expectRevert.unspecified(this.token.flashLoan(receiver.address, this.token.address, MAX_UINT256, data));
  77. });
  78. describe('custom flash fee & custom fee receiver', function () {
  79. const receiverInitialBalance = new BN(200000);
  80. const flashFee = new BN(5000);
  81. beforeEach('init receiver balance & set flash fee', async function () {
  82. this.receiver = await ERC3156FlashBorrowerMock.new(true, true);
  83. const receipt = await this.token.$_mint(this.receiver.address, receiverInitialBalance);
  84. await expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: this.receiver.address, value: receiverInitialBalance });
  85. expect(await this.token.balanceOf(this.receiver.address)).to.be.bignumber.equal(receiverInitialBalance);
  86. await this.token.setFlashFee(flashFee);
  87. expect(await this.token.flashFee(this.token.address, loanAmount)).to.be.bignumber.equal(flashFee);
  88. });
  89. it('default flash fee receiver', async function () {
  90. const { tx } = await this.token.flashLoan(this.receiver.address, this.token.address, loanAmount, '0x');
  91. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: ZERO_ADDRESS, to: this.receiver.address, value: loanAmount });
  92. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.receiver.address, to: ZERO_ADDRESS, value: loanAmount.add(flashFee) });
  93. await expectEvent.inTransaction(tx, this.receiver, 'BalanceOf', { token: this.token.address, account: this.receiver.address, value: receiverInitialBalance.add(loanAmount) });
  94. await expectEvent.inTransaction(tx, this.receiver, 'TotalSupply', { token: this.token.address, value: initialSupply.add(receiverInitialBalance).add(loanAmount) });
  95. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply.add(receiverInitialBalance).sub(flashFee));
  96. expect(await this.token.balanceOf(this.receiver.address)).to.be.bignumber.equal(receiverInitialBalance.sub(flashFee));
  97. expect(await this.token.balanceOf(await this.token.$_flashFeeReceiver())).to.be.bignumber.equal('0');
  98. expect(await this.token.allowance(this.receiver.address, this.token.address)).to.be.bignumber.equal('0');
  99. });
  100. it('custom flash fee receiver', async function () {
  101. const flashFeeReceiverAddress = anotherAccount;
  102. await this.token.setFlashFeeReceiver(flashFeeReceiverAddress);
  103. expect(await this.token.$_flashFeeReceiver()).to.be.eq(flashFeeReceiverAddress);
  104. expect(await this.token.balanceOf(flashFeeReceiverAddress)).to.be.bignumber.equal('0');
  105. const { tx } = await this.token.flashLoan(this.receiver.address, this.token.address, loanAmount, '0x');
  106. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: ZERO_ADDRESS, to: this.receiver.address, value: loanAmount });
  107. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.receiver.address, to: ZERO_ADDRESS, value: loanAmount });
  108. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.receiver.address, to: flashFeeReceiverAddress, value: flashFee });
  109. await expectEvent.inTransaction(tx, this.receiver, 'BalanceOf', { token: this.token.address, account: this.receiver.address, value: receiverInitialBalance.add(loanAmount) });
  110. await expectEvent.inTransaction(tx, this.receiver, 'TotalSupply', { token: this.token.address, value: initialSupply.add(receiverInitialBalance).add(loanAmount) });
  111. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply.add(receiverInitialBalance));
  112. expect(await this.token.balanceOf(this.receiver.address)).to.be.bignumber.equal(receiverInitialBalance.sub(flashFee));
  113. expect(await this.token.balanceOf(flashFeeReceiverAddress)).to.be.bignumber.equal(flashFee);
  114. expect(await this.token.allowance(this.receiver.address, flashFeeReceiverAddress)).to.be.bignumber.equal('0');
  115. });
  116. });
  117. });
  118. });