Flash loans
Sward Core supports both Aave V3 flash-loan entry points:
flashLoanSimpleborrows one reserve and always charges the configured premium.flashLoancan borrow multiple reserves and can optionally open debt when the caller has the required collateral or credit delegation.
Flash loans are a developer primitive, not a retail wallet action. The Pool sends assets to a receiver contract, calls it, and pulls the borrowed amount plus premium before the transaction ends. If any condition is unmet, the entire transaction reverts.
Start from the tested receiver
The Core repository contains
SwardSimpleFlashLoanReceiver.sol
and its
Hardhat test.
The example deliberately does no trading. It demonstrates the security boundary:
- only the owner can request a loan;
- only the configured Pool can invoke the callback;
- the callback verifies that the receiver initiated the loan;
- repayment approval is exact and refreshed for each operation;
- the transaction fails unless the receiver holds principal plus premium.
Implement one explicit strategy in _execute. Avoid a generic arbitrary-call executor: it turns a
small receiver into a much larger authorization and reentrancy surface.
function _execute(
address asset,
uint256 amount,
uint256 premium,
bytes calldata params
) internal override {
// Decode a fixed, versioned strategy payload.
// Execute only allowlisted protocol calls.
// Enforce minimum output >= amount + premium + minimumProfit.
}
Request a simple loan
Resolve the Pool through the
PoolAddressesProvider, prefund the receiver for any premium its strategy
does not earn, and call:
POOL.flashLoanSimple(
address(receiver),
asset,
amount,
encodedStrategyParameters,
0 // referral code
);
Read FLASHLOAN_PREMIUM_TOTAL() immediately before quoting. Governance can change the premium, and
the realized strategy output can change before inclusion.
Production checklist
Before deploying a receiver that can hold value:
- Use immutable or tightly governed allowlists for external targets, assets, and function selectors.
- Validate every callback field and bind parameters to the initiating request.
- Set deadlines, minimum outputs, maximum price impact, and minimum profit on-chain.
- Reject leftover approvals and arbitrary calldata; use exact allowances where practical.
- Model fee-on-transfer, rebasing, callback, and non-standard ERC-20 behavior—or reject those assets explicitly.
- Test malicious callback attempts, insufficient premium, adverse prices, paused reserves, reentrancy, and Pool upgrades.
- Simulate against the current testnet state, run invariant/fuzz tests, and obtain an independent audit before handling production capital.
Fee-free flash borrowing is an ACL-managed protocol role. Sward does not grant it to general integrators; normal receivers must remain profitable after the premium and gas.