ERC20FlashMint.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 ] = 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('flashLoan', function () {
  33. it('success', async function () {
  34. const receiver = await ERC3156FlashBorrowerMock.new(true, true);
  35. const { tx } = await this.token.flashLoan(receiver.address, this.token.address, loanAmount, '0x');
  36. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: ZERO_ADDRESS, to: receiver.address, value: loanAmount });
  37. await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: receiver.address, to: ZERO_ADDRESS, value: loanAmount });
  38. await expectEvent.inTransaction(tx, receiver, 'BalanceOf', { token: this.token.address, account: receiver.address, value: loanAmount });
  39. await expectEvent.inTransaction(tx, receiver, 'TotalSupply', { token: this.token.address, value: initialSupply.add(loanAmount) });
  40. expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply);
  41. expect(await this.token.balanceOf(receiver.address)).to.be.bignumber.equal('0');
  42. expect(await this.token.allowance(receiver.address, this.token.address)).to.be.bignumber.equal('0');
  43. });
  44. it ('missing return value', async function () {
  45. const receiver = await ERC3156FlashBorrowerMock.new(false, true);
  46. await expectRevert(
  47. this.token.flashLoan(receiver.address, this.token.address, loanAmount, '0x'),
  48. 'ERC20FlashMint: invalid return value',
  49. );
  50. });
  51. it ('missing approval', async function () {
  52. const receiver = await ERC3156FlashBorrowerMock.new(true, false);
  53. await expectRevert(
  54. this.token.flashLoan(receiver.address, this.token.address, loanAmount, '0x'),
  55. 'ERC20: insufficient allowance',
  56. );
  57. });
  58. it ('unavailable funds', async function () {
  59. const receiver = await ERC3156FlashBorrowerMock.new(true, true);
  60. const data = this.token.contract.methods.transfer(other, 10).encodeABI();
  61. await expectRevert(
  62. this.token.flashLoan(receiver.address, this.token.address, loanAmount, data),
  63. 'ERC20: burn amount exceeds balance',
  64. );
  65. });
  66. it ('more than maxFlashLoan', async function () {
  67. const receiver = await ERC3156FlashBorrowerMock.new(true, true);
  68. const data = this.token.contract.methods.transfer(other, 10).encodeABI();
  69. // _mint overflow reverts using a panic code. No reason string.
  70. await expectRevert.unspecified(this.token.flashLoan(receiver.address, this.token.address, MAX_UINT256, data));
  71. });
  72. });
  73. });