// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {IERC3156FlashBorrower} from "@openzeppelin/contracts/interfaces/IERC3156FlashBorrower.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/**
 * @title MinimalERC3156FlashBorrower
 * @notice Repays vault via ERC-20 transfer in `onFlashLoan`. Pre-fund this contract with at least `flashFee` of the loan token before calling `flashLoan` (vault sends `amount` only before the callback).
 */
contract MinimalERC3156FlashBorrower is IERC3156FlashBorrower {
    bytes32 private constant _RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");

    address public immutable trustedLender;

    error UntrustedLender();

    constructor(address trustedLender_) {
        trustedLender = trustedLender_;
    }

    function onFlashLoan(
        address,
        address token,
        uint256 amount,
        uint256 fee,
        bytes calldata
    ) external override returns (bytes32) {
        if (msg.sender != trustedLender) revert UntrustedLender();
        IERC20(token).transfer(msg.sender, amount + fee);
        return _RETURN_VALUE;
    }
}
