|
| 1 | +// @title Complex Flashloans Test Script |
| 2 | +// @author Aave |
| 3 | +// @notice Script to test complex flashloans functionality in the Aave protocol |
| 4 | +script { |
| 5 | + // imports |
| 6 | + // std |
| 7 | + use std::signer; |
| 8 | + use std::vector; |
| 9 | + use aptos_std::debug::print; |
| 10 | + use aptos_std::string_utils::format1; |
| 11 | + use aave_pool::fungible_asset_manager::Self; |
| 12 | + // locals |
| 13 | + use aave_pool::flashloan_logic::Self; |
| 14 | + |
| 15 | + // Constants |
| 16 | + // @notice Success code for successful execution |
| 17 | + const SUCCESS: u64 = 1; |
| 18 | + |
| 19 | + // @notice Failure code for failed execution |
| 20 | + const FAILURE: u64 = 2; |
| 21 | + |
| 22 | + /// @notice Test script for executing a complex flashloan |
| 23 | + /// @param borrower The signer account executing the script (must be the borrower) |
| 24 | + /// @param asset The address of the asset to be borrowed in the flashloan |
| 25 | + /// @param amount The amount of the asset to be borrowed in the flashloan |
| 26 | + fun flashloan_complex( |
| 27 | + borrower: &signer, asset: address, amount: u256 |
| 28 | + ) { |
| 29 | + |
| 30 | + let flashloan_borrower_address = signer::address_of(borrower); |
| 31 | + let flashloan_asset = asset; |
| 32 | + let flashloan_amount = amount; |
| 33 | + |
| 34 | + // check the balance of the borrower before the flashloan is taken |
| 35 | + let flashloaner_initial_balance = |
| 36 | + fungible_asset_manager::balance_of( |
| 37 | + flashloan_borrower_address, flashloan_asset |
| 38 | + ); |
| 39 | + print(&format1(&b"Flashloaner initial balance: {}", flashloaner_initial_balance)); |
| 40 | + |
| 41 | + // user prepares to take a flashloan |
| 42 | + let flashloan_receipts = |
| 43 | + flashloan_logic::flash_loan( |
| 44 | + borrower, |
| 45 | + flashloan_borrower_address, |
| 46 | + vector[flashloan_asset], |
| 47 | + vector[flashloan_amount], |
| 48 | + vector[2], |
| 49 | + flashloan_borrower_address, |
| 50 | + 0 // no referral code |
| 51 | + ); |
| 52 | + |
| 53 | + // assert that the flashloan receipts is not empty and it has only 1 item |
| 54 | + assert!(vector::length(&flashloan_receipts) == 1, SUCCESS); |
| 55 | + |
| 56 | + let first_flashloan_receipt = vector::borrow(&flashloan_receipts, 0); |
| 57 | + |
| 58 | + // check the balance of the borrower after the flashloan |
| 59 | + let flashloaner_postloan_balance = |
| 60 | + fungible_asset_manager::balance_of( |
| 61 | + flashloan_borrower_address, flashloan_asset |
| 62 | + ); |
| 63 | + print( |
| 64 | + &format1(&b"Flashloaner post-loan balance: {}", flashloaner_postloan_balance) |
| 65 | + ); |
| 66 | + |
| 67 | + assert!( |
| 68 | + (flashloaner_postloan_balance as u256) |
| 69 | + == (flashloaner_initial_balance as u256) + flashloan_amount, |
| 70 | + SUCCESS |
| 71 | + ); |
| 72 | + |
| 73 | + // do assertion to check if the flashloan was successful |
| 74 | + assert!( |
| 75 | + flashloan_logic::get_complex_flashloan_current_amount(first_flashloan_receipt) == |
| 76 | + flashloan_amount, |
| 77 | + SUCCESS |
| 78 | + ); |
| 79 | + assert!( |
| 80 | + flashloan_logic::get_complex_flashloan_current_asset(first_flashloan_receipt) == |
| 81 | + flashloan_asset, |
| 82 | + SUCCESS |
| 83 | + ); |
| 84 | + assert!( |
| 85 | + flashloan_logic::get_complex_flashloan_on_behalf_of(first_flashloan_receipt) |
| 86 | + == flashloan_borrower_address, |
| 87 | + SUCCESS |
| 88 | + ); |
| 89 | + assert!( |
| 90 | + flashloan_logic::get_complex_flashloan_receipt_index(first_flashloan_receipt) == |
| 91 | + 0, |
| 92 | + SUCCESS |
| 93 | + ); |
| 94 | + assert!( |
| 95 | + flashloan_logic::get_complex_flashloan_receipt_receiver( |
| 96 | + first_flashloan_receipt |
| 97 | + ) == flashloan_borrower_address, |
| 98 | + SUCCESS |
| 99 | + ); |
| 100 | + assert!( |
| 101 | + flashloan_logic::get_complex_flashloan_referral_code(first_flashloan_receipt) == |
| 102 | + 0, |
| 103 | + SUCCESS |
| 104 | + ); |
| 105 | + assert!( |
| 106 | + flashloan_logic::get_complex_flashloan_receipt_sender(first_flashloan_receipt) == |
| 107 | + flashloan_borrower_address, |
| 108 | + SUCCESS |
| 109 | + ); |
| 110 | + |
| 111 | + print( |
| 112 | + &format1( |
| 113 | + &b"flashloan_premium_total: {}", |
| 114 | + flashloan_logic::get_complex_flashloan_premium_total( |
| 115 | + first_flashloan_receipt |
| 116 | + ) |
| 117 | + ) |
| 118 | + ); |
| 119 | + print( |
| 120 | + &format1( |
| 121 | + &b"flashloan_premium_to_protocol: {}", |
| 122 | + flashloan_logic::get_complex_flashloan_premium_to_protocol( |
| 123 | + first_flashloan_receipt |
| 124 | + ) |
| 125 | + ) |
| 126 | + ); |
| 127 | + print( |
| 128 | + &format1( |
| 129 | + &b"flashloan_total_premium: {}", |
| 130 | + flashloan_logic::get_complex_flashloan_total_premium( |
| 131 | + first_flashloan_receipt |
| 132 | + ) |
| 133 | + ) |
| 134 | + ); |
| 135 | + |
| 136 | + // pay back the flashloans |
| 137 | + flashloan_logic::pay_flash_loan_complex(borrower, flashloan_receipts); |
| 138 | + |
| 139 | + // check the balance of the borrower after the flashloans have been paid back |
| 140 | + let flashloaner_postrepay_balance = |
| 141 | + fungible_asset_manager::balance_of( |
| 142 | + flashloan_borrower_address, flashloan_asset |
| 143 | + ); |
| 144 | + print( |
| 145 | + &format1( |
| 146 | + &b"Flashloaner post-repay balance: {}", flashloaner_postrepay_balance |
| 147 | + ) |
| 148 | + ); |
| 149 | + } |
| 150 | +} |
0 commit comments