// SPDX-License-Identifier: ekubo-license-v1.eth pragma solidity =0.8.33; import {NATIVE_TOKEN_ADDRESS} from "../math/constants.sol"; import {IFlashAccountant} from "../interfaces/IFlashAccountant.sol"; import {Locker} from "../types/locker.sol"; /// @title FlashAccountant /// @notice Abstract contract that provides flash loan accounting functionality using transient storage /// @dev This contract manages debt tracking for flash loans, allowing users to borrow tokens temporarily /// and ensuring all debts are settled before the transaction completes. Uses transient storage /// for gas-efficient temporary state management within a single transaction. abstract contract FlashAccountant is IFlashAccountant { // These offsets are selected so that they do not accidentally overlap with any other base contract's use of transient storage /// @dev Transient storage slot for tracking the current locker ID and address /// @dev The stored ID is kept as id + 1 to facilitate the NotLocked check (zero means unlocked) /// @dev Generated using: cast keccak "FlashAccountant#CURRENT_LOCKER_SLOT" uint256 private constant _CURRENT_LOCKER_SLOT = 0x07cc7f5195d862f505d6b095c82f92e00cfc1766f5bca4383c28dc5fca1555fd; /// @dev Transient storage offset for tracking token debts for each locker /// @dev Generated using: cast keccak "FlashAccountant#_DEBT_LOCKER_TOKEN_ADDRESS_OFFSET" uint256 private constant _DEBT_LOCKER_TOKEN_ADDRESS_OFFSET = 0x753dfe4b4dfb3ff6c11bbf6a97f3c094e91c003ce904a55cc5662fbad220f599; /// @dev Transient storage offset for tracking the count of tokens with non-zero debt for each locker /// @dev Generated using: cast keccak "FlashAccountant#NONZERO_DEBT_COUNT_OFFSET" uint256 private constant _NONZERO_DEBT_COUNT_OFFSET = 0x7772acfd7e0f66ebb20a058830296c3dc1301b111d23348e1c961d324223190d; /// @dev Transient storage offset for tracking token balances during payment operations /// @dev Generated using: cast keccak "FlashAccountant#_PAYMENT_TOKEN_ADDRESS_OFFSET" uint256 private constant _PAYMENT_TOKEN_ADDRESS_OFFSET = 0x6747da56dbd05b26a7ecd2a0106781585141cf07098ad54c0e049e4e86dccb8c; /// @notice Gets the current locker information from transient storage /// @dev Reverts with NotLocked() if no lock is currently active /// @return locker The current locker containing both id and address function _getLocker() internal view returns (Locker locker) { assembly ("memory-safe") { locker := tload(_CURRENT_LOCKER_SLOT) if iszero(locker) { // cast sig "NotLocked()" mstore(0, shl(224, 0x1834e265)) revert(0, 4) } } } /// @notice Gets the current locker information and ensures the caller is the locker /// @dev Reverts with LockerOnly() if the caller is not the current locker /// @return locker The current locker containing both id and address (address must be msg.sender) function _requireLocker() internal view returns (Locker locker) { locker = _getLocker(); if (locker.addr() != msg.sender) revert LockerOnly(); } /// @notice Updates the debt tracking for a specific locker and token /// @dev We assume debtChange cannot exceed a 128 bits value, even though it uses a int256 container. /// This must be enforced at the places it is called for this contract's safety. /// Negative values erase debt, positive values add debt. /// Updates the non-zero debt count when debt transitions between zero and non-zero states. /// @param id The locker ID to update debt for /// @param token The token address to update debt for /// @param debtChange The change in debt (negative to reduce, positive to increase) function _accountDebt(uint256 id, address token, int256 debtChange) internal { assembly ("memory-safe") { let deltaSlot := add(_DEBT_LOCKER_TOKEN_ADDRESS_OFFSET, add(shl(160, id), token)) let current := tload(deltaSlot) // we know this never overflows because debtChange is only ever derived from 128 bit values in inheriting contracts let next := add(current, debtChange) let countChange := sub(iszero(current), iszero(next)) if countChange { let nzdCountSlot := add(id, _NONZERO_DEBT_COUNT_OFFSET) tstore(nzdCountSlot, add(tload(nzdCountSlot), countChange)) } tstore(deltaSlot, next) } } /// @notice Updates the debt tracking for a specific locker and pair of tokens in a single operation /// @dev Optimized version that updates both tokens' debts and performs a single tload/tstore on the non-zero debt count. /// Individual token debt slots are still updated separately, but the non-zero debt count is only loaded/stored once. /// We assume debtChange values cannot exceed 128 bits. This must be enforced at the places it is called for this contract's safety. /// Negative values erase debt, positive values add debt. /// @param id The locker ID to update debt for /// @param tokenA The first token address /// @param tokenB The second token address /// @param debtChangeA The change in debt for tokenA (negative to reduce, positive to increase) /// @param debtChangeB The change in debt for tokenB (negative to reduce, positive to increase) function _updatePairDebt(uint256 id, address tokenA, address tokenB, int256 debtChangeA, int256 debtChangeB) internal { assembly ("memory-safe") { let nzdCountChange := 0 // Update token0 debt if there's a change if debtChangeA { let deltaSlotA := add(_DEBT_LOCKER_TOKEN_ADDRESS_OFFSET, add(shl(160, id), tokenA)) let currentA := tload(deltaSlotA) let nextA := add(currentA, debtChangeA) nzdCountChange := sub(iszero(currentA), iszero(nextA)) tstore(deltaSlotA, nextA) } if debtChangeB { let deltaSlotB := add(_DEBT_LOCKER_TOKEN_ADDRESS_OFFSET, add(shl(160, id), tokenB)) let currentB := tload(deltaSlotB) let nextB := add(currentB, debtChangeB) nzdCountChange := add(nzdCountChange, sub(iszero(currentB), iszero(nextB))) tstore(deltaSlotB, nextB) } // Update non-zero debt count only if it changed if nzdCountChange { let nzdCountSlot := add(id, _NONZERO_DEBT_COUNT_OFFSET) tstore(nzdCountSlot, add(tload(nzdCountSlot), nzdCountChange)) } } } /// @inheritdoc IFlashAccountant function updateDebt() external { if (msg.data.length != 20) { revert UpdateDebtMessageLength(); } uint256 id = _getLocker().id(); int256 delta; assembly ("memory-safe") { delta := signextend(15, shr(128, calldataload(4))) } _accountDebt(id, msg.sender, delta); } /// @inheritdoc IFlashAccountant function lock() external { assembly ("memory-safe") { let current := tload(_CURRENT_LOCKER_SLOT) let id := shr(160, current) // store the count tstore(_CURRENT_LOCKER_SLOT, or(shl(160, add(id, 1)), caller())) let free := mload(0x40) // Prepare call to locked_(uint256) -> selector 0 mstore(free, 0) mstore(add(free, 4), id) // ID argument calldatacopy(add(free, 36), 4, sub(calldatasize(), 4)) // Call the original caller with the packed data let success := call(gas(), caller(), 0, free, add(calldatasize(), 32), 0, 0) // Pass through the error on failure if iszero(success) { returndatacopy(free, 0, returndatasize()) revert(free, returndatasize()) } // Undo the "locker" state changes tstore(_CURRENT_LOCKER_SLOT, current) // Check if something is nonzero let nonzeroDebtCount := tload(add(_NONZERO_DEBT_COUNT_OFFSET, id)) if nonzeroDebtCount { // cast sig "DebtsNotZeroed(uint256)" mstore(0x00, 0x9731ba37) mstore(0x20, id) revert(0x1c, 0x24) } // Directly return whatever the subcall returned returndatacopy(free, 0, returndatasize()) return(free, returndatasize()) } } /// @inheritdoc IFlashAccountant function forward(address to) external { Locker locker = _requireLocker(); // update this lock's locker to the forwarded address for the duration of the forwarded // call, meaning only the forwarded address can update state assembly ("memory-safe") { tstore(_CURRENT_LOCKER_SLOT, or(shl(160, shr(160, locker)), to)) let free := mload(0x40) // Prepare call to forwarded_2374103877(bytes32) -> selector 0x01 mstore(free, shl(224, 1)) mstore(add(free, 4), locker) calldatacopy(add(free, 36), 36, sub(calldatasize(), 36)) // Call the forwardee with the packed data let success := call(gas(), to, 0, free, calldatasize(), 0, 0) // Pass through the error on failure if iszero(success) { returndatacopy(free, 0, returndatasize()) revert(free, returndatasize()) } tstore(_CURRENT_LOCKER_SLOT, locker) // Directly return whatever the subcall returned returndatacopy(free, 0, returndatasize()) return(free, returndatasize()) } } /// @inheritdoc IFlashAccountant function startPayments() external { assembly ("memory-safe") { // 0-52 are used for the balanceOf calldata mstore(20, address()) // Store the `account` argument. mstore(0, 0x70a08231000000000000000000000000) // `balanceOf(address)`. let free := mload(0x40) for { let i := 4 } lt(i, calldatasize()) { i := add(i, 32) } { // clean upper 96 bits of the token argument at i let token := shr(96, shl(96, calldataload(i))) let returnLocation := add(free, sub(i, 4)) let success := staticcall(gas(), token, 0x10, 0x24, returnLocation, 0x20) let tokenBalance := mul( mload(returnLocation), and( gt(returndatasize(), 0x1f), // At least 32 bytes returned. success ) ) tstore(add(_PAYMENT_TOKEN_ADDRESS_OFFSET, token), add(tokenBalance, success)) } return(free, sub(calldatasize(), 4)) } } /// @inheritdoc IFlashAccountant function completePayments() external { uint256 id = _getLocker().id(); assembly ("memory-safe") { let paymentAmounts := mload(0x40) let nzdCountChange := 0 for { let i := 4 } lt(i, calldatasize()) { i := add(i, 32) } { let token := shr(96, shl(96, calldataload(i))) let offset := add(_PAYMENT_TOKEN_ADDRESS_OFFSET, token) let lastBalance := tload(offset) tstore(offset, 0) mstore(20, address()) // Store the `account` argument. mstore(0, 0x70a08231000000000000000000000000) // `balanceOf(address)`. let currentBalance := mul( // The arguments of `mul` are evaluated from right to left. mload(0), and( // The arguments of `and` are evaluated from right to left. gt(returndatasize(), 0x1f), // At least 32 bytes returned. staticcall(gas(), token, 0x10, 0x24, 0, 0x20) ) ) let payment := mul( and(gt(lastBalance, 0), not(lt(currentBalance, lastBalance))), sub(currentBalance, sub(lastBalance, 1)) ) // We never expect tokens to have this much total supply if shr(128, payment) { // cast sig "PaymentOverflow()" mstore(0x00, 0x9cac58ca) revert(0x1c, 4) } mstore(add(paymentAmounts, mul(16, div(i, 32))), shl(128, payment)) if payment { let deltaSlot := add(_DEBT_LOCKER_TOKEN_ADDRESS_OFFSET, add(shl(160, id), token)) let current := tload(deltaSlot) // never overflows because of the payment overflow check that bounds payment to 128 bits let next := sub(current, payment) nzdCountChange := add(nzdCountChange, sub(iszero(current), iszero(next))) tstore(deltaSlot, next) } } // Update nzdCountSlot only once if there were any changes if nzdCountChange { let nzdCountSlot := add(id, _NONZERO_DEBT_COUNT_OFFSET) tstore(nzdCountSlot, add(tload(nzdCountSlot), nzdCountChange)) } return(paymentAmounts, mul(16, div(calldatasize(), 32))) } } /// @inheritdoc IFlashAccountant function withdraw() external { uint256 id = _requireLocker().id(); assembly ("memory-safe") { let nzdCountChange := 0 // Process each withdrawal entry for { let i := 4 } lt(i, calldatasize()) { i := add(i, 56) } { let token := shr(96, calldataload(i)) let recipient := shr(96, calldataload(add(i, 20))) let amount := shr(128, calldataload(add(i, 40))) if amount { // Update debt tracking without updating nzdCountSlot yet let deltaSlot := add(_DEBT_LOCKER_TOKEN_ADDRESS_OFFSET, add(shl(160, id), token)) let current := tload(deltaSlot) let next := add(current, amount) nzdCountChange := add(nzdCountChange, sub(iszero(current), iszero(next))) tstore(deltaSlot, next) // Perform the transfer of the withdrawn asset // Note that these calls can re-enter and even relock with the same ID // However the nzdCountChange is always applied as a delta at the end, meaning we load the latest value before updating it, // so it's safe from re-entry switch token case 0 { let success := call(gas(), recipient, amount, 0, 0, 0, 0) if iszero(success) { // cast sig "ETHTransferFailed()" mstore(0x00, 0xb12d13eb) revert(0x1c, 4) } } default { mstore(0x14, recipient) mstore(0x34, amount) mstore(0x00, 0xa9059cbb000000000000000000000000) let success := call(gas(), token, 0, 0x10, 0x44, 0x00, 0x20) if iszero(and(eq(mload(0x00), 1), success)) { if iszero(lt(or(iszero(extcodesize(token)), returndatasize()), success)) { mstore(0x00, 0x90b8ec18) // `TransferFailed()`. revert(0x1c, 0x04) } } } } } // Update nzdCountSlot only once if there were any changes if nzdCountChange { let nzdCountSlot := add(id, _NONZERO_DEBT_COUNT_OFFSET) tstore(nzdCountSlot, add(tload(nzdCountSlot), nzdCountChange)) } // we return from assembly so as to prevent solidity from accessing the free memory pointer after we have written into it return(0, 0) } } /// @inheritdoc IFlashAccountant receive() external payable { uint256 id = _getLocker().id(); // Note because we use msg.value here, this contract can never be multicallable, i.e. it should never expose the ability // to delegatecall itself more than once in a single call unchecked { // We assume msg.value will never exceed type(uint128).max, so this should never cause an overflow/underflow of debt _accountDebt(id, NATIVE_TOKEN_ADDRESS, -int256(msg.value)); } } }