Skip to content

Commit

Permalink
test: add happy flow cases with present/missing fee & fee receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Dec 20, 2024
1 parent 4538ebc commit 987d3a3
Showing 1 changed file with 127 additions and 11 deletions.
138 changes: 127 additions & 11 deletions examples/erc20-flash-mint/tests/erc20-flash-mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,123 @@ async fn flash_fee_reverts_on_unsupported_token(alice: Account) -> Result<()> {
Ok(())
}

#[e2e::test]
async fn flash_loan_with_fee(alice: Account) -> Result<()> {
let erc20_addr = alice
.as_deployer()
.with_constructor(ctr(Address::ZERO, FLASH_FEE_AMOUNT))
.deploy()
.await?
.address()?;
let erc20 = Erc20FlashMint::new(erc20_addr, &alice.wallet);

let borrower_addr = borrower::deploy(&alice.wallet, true, true).await?;
let _ = watch!(erc20.mint(borrower_addr, FLASH_FEE_AMOUNT))?;
let loan_amount = uint!(1_000_000_U256);

let borrower_balance = erc20.balanceOf(borrower_addr).call().await?.balance;
let total_supply = erc20.totalSupply().call().await?.totalSupply;

assert_eq!(FLASH_FEE_AMOUNT, borrower_balance);
assert_eq!(FLASH_FEE_AMOUNT, total_supply);

let receipt = receipt!(erc20.flashLoan(
borrower_addr,
erc20_addr,
loan_amount,
vec![].into()
))?;

assert!(receipt.emits(Erc20FlashMint::Transfer {
from: Address::ZERO,
to: borrower_addr,
value: loan_amount,
}));
assert!(receipt.emits(ERC3156FlashBorrowerMock::BalanceOf {
token: erc20_addr,
account: borrower_addr,
value: loan_amount + FLASH_FEE_AMOUNT,
}));
assert!(receipt.emits(ERC3156FlashBorrowerMock::TotalSupply {
token: erc20_addr,
value: loan_amount + FLASH_FEE_AMOUNT,
}));
assert!(receipt.emits(Erc20FlashMint::Transfer {
from: borrower_addr,
to: Address::ZERO,
value: loan_amount + FLASH_FEE_AMOUNT,
}));

let borrower_balance = erc20.balanceOf(borrower_addr).call().await?.balance;
let total_supply = erc20.totalSupply().call().await?.totalSupply;

assert_eq!(U256::ZERO, borrower_balance);
assert_eq!(U256::ZERO, total_supply);

Ok(())
}

#[e2e::test]
async fn flash_loan_with_fee_receiver(alice: Account) -> Result<()> {
let erc20_addr = alice
.as_deployer()
.with_constructor(ctr(FEE_RECEIVER, U256::ZERO))
.deploy()
.await?
.address()?;
let erc20 = Erc20FlashMint::new(erc20_addr, &alice.wallet);

let borrower_addr = borrower::deploy(&alice.wallet, true, true).await?;
let loan_amount = uint!(1_000_000_U256);

let borrower_balance = erc20.balanceOf(borrower_addr).call().await?.balance;
let fee_receiver_balance =
erc20.balanceOf(FEE_RECEIVER).call().await?.balance;
let total_supply = erc20.totalSupply().call().await?.totalSupply;

assert_eq!(U256::ZERO, borrower_balance);
assert_eq!(U256::ZERO, fee_receiver_balance);
assert_eq!(U256::ZERO, total_supply);

let receipt = receipt!(erc20.flashLoan(
borrower_addr,
erc20_addr,
loan_amount,
vec![].into()
))?;

assert!(receipt.emits(Erc20FlashMint::Transfer {
from: Address::ZERO,
to: borrower_addr,
value: loan_amount,
}));
assert!(receipt.emits(ERC3156FlashBorrowerMock::BalanceOf {
token: erc20_addr,
account: borrower_addr,
value: loan_amount,
}));
assert!(receipt.emits(ERC3156FlashBorrowerMock::TotalSupply {
token: erc20_addr,
value: loan_amount,
}));
assert!(receipt.emits(Erc20FlashMint::Transfer {
from: borrower_addr,
to: Address::ZERO,
value: loan_amount,
}));

let borrower_balance = erc20.balanceOf(borrower_addr).call().await?.balance;
let fee_receiver_balance =
erc20.balanceOf(FEE_RECEIVER).call().await?.balance;
let total_supply = erc20.totalSupply().call().await?.totalSupply;

assert_eq!(U256::ZERO, borrower_balance);
assert_eq!(U256::ZERO, fee_receiver_balance);
assert_eq!(U256::ZERO, total_supply);

Ok(())
}

#[e2e::test]
async fn flash_loan_with_fee_and_fee_receiver(alice: Account) -> Result<()> {
let erc20_addr = alice
Expand All @@ -195,6 +312,7 @@ async fn flash_loan_with_fee_and_fee_receiver(alice: Account) -> Result<()> {

let borrower_addr = borrower::deploy(&alice.wallet, true, true).await?;
let _ = watch!(erc20.mint(borrower_addr, FLASH_FEE_AMOUNT))?;
let loan_amount = uint!(1_000_000_U256);

let borrower_balance = erc20.balanceOf(borrower_addr).call().await?.balance;
let fee_receiver_balance =
Expand All @@ -205,11 +323,6 @@ async fn flash_loan_with_fee_and_fee_receiver(alice: Account) -> Result<()> {
assert_eq!(U256::ZERO, fee_receiver_balance);
assert_eq!(FLASH_FEE_AMOUNT, total_supply);

let loan_amount = uint!(1_000_000_U256);
let max_loan = erc20.maxFlashLoan(erc20_addr).call().await?.maxLoan;

assert!(max_loan > loan_amount);

let receipt = receipt!(erc20.flashLoan(
borrower_addr,
erc20_addr,
Expand All @@ -222,7 +335,6 @@ async fn flash_loan_with_fee_and_fee_receiver(alice: Account) -> Result<()> {
to: borrower_addr,
value: loan_amount,
}));

assert!(receipt.emits(ERC3156FlashBorrowerMock::BalanceOf {
token: erc20_addr,
account: borrower_addr,
Expand All @@ -232,11 +344,15 @@ async fn flash_loan_with_fee_and_fee_receiver(alice: Account) -> Result<()> {
token: erc20_addr,
value: loan_amount + FLASH_FEE_AMOUNT,
}));

assert!(receipt.emits(Erc20FlashMint::Approval {
owner: borrower_addr,
spender: erc20_addr,
value: loan_amount + FLASH_FEE_AMOUNT,
assert!(receipt.emits(Erc20FlashMint::Transfer {
from: borrower_addr,
to: Address::ZERO,
value: loan_amount,
}));
assert!(receipt.emits(Erc20FlashMint::Transfer {
from: borrower_addr,
to: FEE_RECEIVER,
value: FLASH_FEE_AMOUNT,
}));

let borrower_balance = erc20.balanceOf(borrower_addr).call().await?.balance;
Expand Down

0 comments on commit 987d3a3

Please sign in to comment.