Skip to content

Commit

Permalink
ref: loan_return fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNeshi committed Dec 12, 2024
1 parent 8ff508c commit 2600073
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions contracts/src/token/erc20/extensions/flash_mint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,46 +176,49 @@ impl IERC3156FlashLender for Erc20FlashMint {
&mut self,
receiver: Address,
token: Address,
value: U256,
amount: U256,
data: Bytes,
erc20: &mut Erc20,
) -> Result<bool, Self::Error> {
let max_loan = self.max_flash_loan(token, erc20);
if value > max_loan {
if amount > max_loan {
return Err(Error::ExceededMaxLoan(ERC3156ExceededMaxLoan {
max_loan,
}));
}

let fee = self.flash_fee(token, value)?;
erc20._mint(receiver, value)?;
let fee = self.flash_fee(token, amount)?;
erc20._mint(receiver, amount)?;
let loan_receiver = IERC3156FlashBorrower::new(receiver);
if Address::has_code(&loan_receiver) {
return Err(Error::InvalidReceiver(ERC3156InvalidReceiver {
receiver,
}));
}
let loan_return = loan_receiver.on_flash_loan(
Call::new(),
msg::sender(),
token,
value,
fee,
data.to_vec().into(),
);
if loan_return.is_err() || loan_return.ok() != Some(RETURN_VALUE.into())
{
let loan_return = loan_receiver
.on_flash_loan(
Call::new(),
msg::sender(),
token,
amount,
fee,
data.to_vec().into(),
)
.map_err(|_| {
Error::InvalidReceiver(ERC3156InvalidReceiver { receiver })
})?;
if loan_return != RETURN_VALUE {
return Err(Error::InvalidReceiver(ERC3156InvalidReceiver {
receiver,
}));
}

let flash_fee_receiver = self.flash_fee_receiver_address.get();
erc20._spend_allowance(receiver, msg::sender(), value + fee)?;
erc20._spend_allowance(receiver, msg::sender(), amount + fee)?;
if fee.is_zero() || flash_fee_receiver.is_zero() {
erc20._burn(receiver, value + fee)?;
erc20._burn(receiver, amount + fee)?;
} else {
erc20._burn(receiver, value)?;
erc20._burn(receiver, amount)?;
erc20._transfer(receiver, flash_fee_receiver, fee)?;
}

Expand Down

0 comments on commit 2600073

Please sign in to comment.