Skip to content

flash loans #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1ddb124
copying kiplet's code
0xripleys Jul 22, 2022
0b9fd66
make flash loans more conservative
0xripleys Jul 22, 2022
881259a
happy case test
0xripleys Jul 25, 2022
6452238
adding more tests, fixed a bug
0xripleys Jul 26, 2022
029fdd6
moar tests
0xripleys Jul 26, 2022
7159325
clippy lints
0xripleys Jul 26, 2022
76ef3f6
fix assert
0xripleys Jul 26, 2022
ddd1662
remove clock
0xripleys Jul 26, 2022
c1be59b
remove old flash loan instruction, but keep the instruction packing code
0xripleys Jul 26, 2022
69d6be3
saner flash loan fee calculation
0xripleys Jul 26, 2022
55a156e
add flash borrow ix arg to flash repay
0xripleys Jul 27, 2022
901f969
cargo fmt
0xripleys Jul 27, 2022
baa1c1e
add cpi repay test
0xripleys Jul 27, 2022
667a225
bump compute units, no idea why github actions is failing
0xripleys Jul 27, 2022
e1cd85a
test for malicious use case
0xripleys Jul 28, 2022
9a85b30
remove unused variable
0xripleys Jul 29, 2022
f632317
fix coverage, check reserve borrow limit in flash borrow
0xripleys Aug 1, 2022
6a44638
mark reserves as stale in flash borrow and repay
0xripleys Aug 1, 2022
2b98769
revert back to old nightly version, install grcov with stable
0xripleys Aug 1, 2022
0b68bf7
fix for [repay, borrow, repay]
0xripleys Aug 2, 2022
fd385b9
refactor CPI
0xripleys Aug 9, 2022
df2be24
recheck stuff in flash repay
0xripleys Aug 9, 2022
70370a2
explicitly check for out of bounds in flash borrow loop
0xripleys Aug 9, 2022
7bfea26
0xripleys remove clock (#99)
0xripleys Aug 12, 2022
7b944e6
Merge branch 'upcoming' into 0xripleys_flash_loan
0xripleys Aug 19, 2022
67510c6
add stack height check
0xripleys Aug 19, 2022
c108e99
fmt
0xripleys Aug 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ci/install-program-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ set -x
cargo --version
cargo install rustfilt || true
cargo install honggfuzz --version=0.5.52 --force || true
cargo install grcov --force
cargo +"$rust_stable" install grcov --force

cargo +"$rust_stable" build-bpf --version
2 changes: 1 addition & 1 deletion coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ for program in ${programs[@]}; do
(
set -ex
cd $program
cargo +"$rust_nightly" test --features test-bpf --target-dir $here/target/cov
cargo +"$rust_nightly" test --features test-bpf --target-dir $here/target/cov -- --skip fail_repay_from_diff_reserve
)
done

Expand Down
20 changes: 20 additions & 0 deletions token-lending/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,26 @@ pub enum LendingError {
/// Insufficent protocol fees to redeem or no liquidity availible to process redeem
#[error("Insufficent protocol fees to claim or no liquidity availible")]
InsufficientProtocolFeesToRedeem,
/// No cpi flash borrows allowed
#[error("No cpi flash borrows allowed")]
FlashBorrowCpi,
/// No corresponding repay found for flash borrow
#[error("No corresponding repay found for flash borrow")]
NoFlashRepayFound,
/// Invalid flash repay found for borrow
#[error("Invalid repay found")]
InvalidFlashRepay,

// 50
/// No cpi flash repays allowed
#[error("No cpi flash repays allowed")]
FlashRepayCpi,
/// Multiple flash borrows not allowed in the same transaction
#[error("Multiple flash borrows not allowed in the same transaction")]
MultipleFlashBorrows,
/// Flash loans are disabled for this reserve
#[error("Flash loans are disabled for this reserve")]
FlashLoansDisabled,
/// Deprecated instruction
#[error("Instruction is deprecated")]
DeprecatedInstruction,
Expand Down
211 changes: 145 additions & 66 deletions token-lending/program/src/instruction.rs

Large diffs are not rendered by default.

686 changes: 480 additions & 206 deletions token-lending/program/src/processor.rs

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions token-lending/program/src/state/reserve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,11 +733,16 @@ impl ReserveFees {
&self,
flash_loan_amount: Decimal,
) -> Result<(u64, u64), ProgramError> {
self.calculate_fees(
let (total_fees, host_fee) = self.calculate_fees(
flash_loan_amount,
self.flash_loan_fee_wad,
FeeCalculation::Exclusive,
)
)?;

let origination_fee = total_fees
.checked_sub(host_fee)
.ok_or(LendingError::MathOverflow)?;
Ok((origination_fee, host_fee))
}

fn calculate_fees(
Expand Down Expand Up @@ -1330,25 +1335,22 @@ mod test {
flash_loan_fee_wad,
host_fee_percentage,
};
let (total_fee, host_fee) = fees.calculate_flash_loan_fees(Decimal::from(borrow_amount))?;
let (origination_fee, host_fee) = fees.calculate_flash_loan_fees(Decimal::from(borrow_amount))?;

// The total fee can't be greater than the amount borrowed, as long
// as amount borrowed is greater than 2.
// At a borrow amount of 2, we can get a total fee of 2 if a host
// fee is also specified.
assert!(total_fee <= borrow_amount);

// the host fee can't be greater than the total fee
assert!(host_fee <= total_fee);
assert!(origination_fee + host_fee <= borrow_amount);

// for all fee rates greater than 0, we must have some fee
if borrow_fee_wad > 0 {
assert!(total_fee > 0);
assert!(origination_fee + host_fee > 0);
}

if host_fee_percentage == 100 {
// if the host fee percentage is maxed at 100%, it should get all the fee
assert_eq!(host_fee, total_fee);
assert_eq!(origination_fee, 0);
}

// if there's a host fee and some borrow fee, host fee must be greater than 0
Expand Down
Loading