ERC20FlashMint.test.js 7.8 KB

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