Add customizable fee receiver to ERC20FlashMint (#3327)

Co-authored-by: Mazen Khalil <mazen@immunityledger.org>
Co-authored-by: Francisco Giordano <frangio.1@gmail.com>
This commit is contained in:
Mazen Khalil
2022-05-07 00:46:23 +03:00
committed by GitHub
parent 07b1b472c0
commit 3b9381dfb1
4 changed files with 101 additions and 2 deletions

View File

@ -8,7 +8,7 @@ const ERC20FlashMintMock = artifacts.require('ERC20FlashMintMock');
const ERC3156FlashBorrowerMock = artifacts.require('ERC3156FlashBorrowerMock');
contract('ERC20FlashMint', function (accounts) {
const [ initialHolder, other ] = accounts;
const [ initialHolder, other, anotherAccount ] = accounts;
const name = 'My Token';
const symbol = 'MTKN';
@ -40,6 +40,12 @@ contract('ERC20FlashMint', function (accounts) {
});
});
describe('flashFeeReceiver', function () {
it('default receiver', async function () {
expect(await this.token.flashFeeReceiver()).to.be.eq(ZERO_ADDRESS);
});
});
describe('flashLoan', function () {
it('success', async function () {
const receiver = await ERC3156FlashBorrowerMock.new(true, true);
@ -86,5 +92,53 @@ contract('ERC20FlashMint', function (accounts) {
// _mint overflow reverts using a panic code. No reason string.
await expectRevert.unspecified(this.token.flashLoan(receiver.address, this.token.address, MAX_UINT256, data));
});
describe('custom flash fee & custom fee receiver', function () {
const receiverInitialBalance = new BN(200000);
const flashFee = new BN(5000);
beforeEach('init reciever balance & set flash fee',async function () {
this.receiver = await ERC3156FlashBorrowerMock.new(true, true);
const receipt = await this.token.mint(this.receiver.address, receiverInitialBalance);
await expectEvent(receipt, 'Transfer', { from: ZERO_ADDRESS, to: this.receiver.address, value: receiverInitialBalance });
expect(await this.token.balanceOf(this.receiver.address)).to.be.bignumber.equal(receiverInitialBalance);
await this.token.setFlashFee(flashFee);
expect(await this.token.flashFee(this.token.address, loanAmount)).to.be.bignumber.equal(flashFee);
});
it('default flash fee receiver', async function () {
const { tx } = await this.token.flashLoan(this.receiver.address, this.token.address, loanAmount, '0x');
await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: ZERO_ADDRESS, to: this.receiver.address, value: loanAmount });
await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.receiver.address, to: ZERO_ADDRESS, value: loanAmount.add (flashFee)});
await expectEvent.inTransaction(tx, this.receiver, 'BalanceOf', { token: this.token.address, account: this.receiver.address, value: receiverInitialBalance.add(loanAmount) });
await expectEvent.inTransaction(tx, this.receiver, 'TotalSupply', { token: this.token.address, value: initialSupply.add (receiverInitialBalance).add(loanAmount) });
expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply.add(receiverInitialBalance).sub(flashFee));
expect(await this.token.balanceOf(this.receiver.address)).to.be.bignumber.equal(receiverInitialBalance.sub(flashFee));
expect(await this.token.balanceOf(await this.token.flashFeeReceiver())).to.be.bignumber.equal('0');
expect(await this.token.allowance(this.receiver.address, this.token.address)).to.be.bignumber.equal('0');
});
it('custom flash fee receiver', async function () {
const flashFeeReceiverAddress = anotherAccount;
await this.token.setFlashFeeReceiver(flashFeeReceiverAddress);
expect(await this.token.flashFeeReceiver()).to.be.eq(flashFeeReceiverAddress);
expect(await this.token.balanceOf(flashFeeReceiverAddress)).to.be.bignumber.equal('0');
const { tx } = await this.token.flashLoan(this.receiver.address, this.token.address, loanAmount, '0x');
await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: ZERO_ADDRESS, to: this.receiver.address, value: loanAmount });
await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.receiver.address, to: ZERO_ADDRESS, value: loanAmount });
await expectEvent.inTransaction(tx, this.token, 'Transfer', { from: this.receiver.address, to: flashFeeReceiverAddress, value: flashFee });
await expectEvent.inTransaction(tx, this.receiver, 'BalanceOf', { token: this.token.address, account: this.receiver.address, value: receiverInitialBalance.add(loanAmount) });
await expectEvent.inTransaction(tx, this.receiver, 'TotalSupply', { token: this.token.address, value: initialSupply.add (receiverInitialBalance).add(loanAmount) });
expect(await this.token.totalSupply()).to.be.bignumber.equal(initialSupply.add(receiverInitialBalance));
expect(await this.token.balanceOf(this.receiver.address)).to.be.bignumber.equal(receiverInitialBalance.sub(flashFee));
expect(await this.token.balanceOf(flashFeeReceiverAddress)).to.be.bignumber.equal(flashFee);
expect(await this.token.allowance(this.receiver.address, flashFeeReceiverAddress)).to.be.bignumber.equal('0');
});
});
});
});