### Quick Start Setup - Bash Source: https://github.com/bitfinding/multiplexer/blob/main/README.md Provides the necessary bash commands to clone the Multiplexer repository, build the project (including contract compilation), and run tests using a mainnet fork RPC URL. ```bash git clone https://github.com/BitFinding/multiplexer.git cd multiplexer cargo build ETH_RPC_URL=https://eth-mainnet.alchemyapi.io/v2/YOUR_API_KEY cargo test ``` -------------------------------- ### Example Bytecode Sequence Source: https://github.com/bitfinding/multiplexer/blob/main/README.md An example sequence of opcodes and parameters demonstrating how operations are encoded for the multiplexer contract. This sequence clears data, sets target address and value, sets function selector data, enables failure revert, executes a call, and ends. ```Bytecode 0x01 0x0040 # CLEARDATA: Allocate 64 bytes for calldata 0x03 # SETADDR: Set target contract address 0x04 # SETVALUE: Set ETH value to send (1 ether) 0x02 0x0000 0x0004 # SETDATA: Set function selector at offset 0, length 4 aabbccdd # ↳ Function selector bytes 0x0A # SETFAIL: Enable revert on failure 0x06 # CALL: Execute the call 0x00 # EOF: End sequence ``` -------------------------------- ### Constructing Morpho Flash Loan Bytecode (Rust) Source: https://github.com/bitfinding/multiplexer/blob/main/README.md Demonstrates how to use the Rust `FlowBuilder` to generate the bytecode for a Morpho flash loan. It builds an inner flow for approving WETH repayment within the flash loan callback and an outer flow to initiate the flash loan call to Morpho, including setting failure handling and callback address. ```Rust use alloy::{ primitives::{address, uint, Address, Bytes, U256, hex}, sol, sol_types::{SolCall}, }; use multiplexer::FlowBuilder; // Define necessary constants const ONEHUNDRED_ETH: U256 = uint!(100000000000000000000_U256); // 100e18 const WETH9: Address = address!("c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); const MORPHO: Address = address!("BBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb"); // Simplified Sol definitions for the example sol! { interface IERC20 { function approve(address spender, uint256 value) external returns (bool); } interface IMorpho { function flashLoan(address token, uint256 assets, bytes calldata data) external; } } /// Generates the bytecode for a Morpho flash loan flow. /// The flow borrows 100 WETH and sets up the callback to approve repayment. fn generate_morpho_flashloan_flow() -> Vec { // 1. Prepare the inner flow (callback data): Approve WETH repayment. // This flow will be executed by the executor when Morpho calls back into it. // It needs to ensure Morpho can pull the funds back. let approve_calldata = IERC20::approveCall { spender: MORPHO, value: ONEHUNDRED_ETH, // Approve the exact loan amount. // Note: A real flash loan requires approving amount + fee. // The executor must hold sufficient WETH *before* this approval runs. }.abi_encode(); let inner_flow_bytes = FlowBuilder::empty() .call(WETH9, &approve_calldata, U256::ZERO) // Call WETH9.approve(MORPHO, amount) .optimize() .build_raw(); // Get the raw bytecode for the inner flow. // `build_raw()` produces *only* the sequence of action opcodes. // 2. Prepare the outer flow: Initiate the flash loan. // This is the main flow sent to the executor contract transaction. let flashloan_calldata = IMorpho::flashLoanCall { token: WETH9, // Asset to borrow assets: ONEHUNDRED_ETH, // Amount to borrow data: inner_flow_bytes.into(), // Pass the repayment flow as callback data }.abi_encode(); let main_flow_bytes = FlowBuilder::empty() .set_fail() // Revert the entire transaction if any subsequent call fails (including the callback) .set_callback(MORPHO) // Set Morpho as the expected callback address. The executor will only // execute the callback data if msg.sender matches this address. .call(MORPHO, &flashloan_calldata, U256::ZERO) // Call Morpho.flashLoan(...) .optimize() // Apply peephole optimizations .build(); // Build the final bytecode sequence. // `build()` prepends the `executeActions()` function selector (0xc94f554d) // to the raw action bytecode generated by `build_raw()`. main_flow_bytes } // --- How to use the generated bytecode --- fn main() { let flow_bytecode = generate_morpho_flashloan_flow(); // `flow_bytecode` now contains the sequence: // SETFAIL -> SETCALLBACK(MORPHO) -> CALL(MORPHO, flashLoan(...)) // This `flow_bytecode` would be used as the `data` field in an Ethereum transaction // sent to your deployed Executor contract instance. // Important Considerations for Execution: // 1. Funding: The Executor contract must possess enough WETH *after* the flash loan // is granted but *before* the callback completes to successfully execute the // `inner_flow_bytes` (the WETH approval) and allow Morpho to reclaim the funds + fee. // This usually means the Executor needs some initial WETH or the operations *within* // the flash loan (not shown in this basic example) must generate the required WETH. // 2. Gas: Ensure sufficient gas is provided for the main transaction and the callback execution. // 3. Permissions: The transaction sender must be the owner of the Executor contract. println!("Generated Flow Bytecode: 0x{}", hex::encode(&flow_bytecode)); } ``` -------------------------------- ### Morpho Flash Loan Callback - Solidity Source: https://github.com/bitfinding/multiplexer/blob/main/README.md The function signature for the callback expected by the Morpho flash loan protocol. The multiplexer contract implements this function to receive flash loan funds and execute the provided bytecode. ```Solidity function onMorphoFlashLoan(uint256 amount, bytes calldata data) ``` -------------------------------- ### Aave Flash Loan Callback - Solidity Source: https://github.com/bitfinding/multiplexer/blob/main/README.md The function signature for the callback expected by the Aave flash loan protocol (specifically the `executeOperation` interface). The multiplexer contract implements this function to receive flash loan funds and execute operations. ```Solidity function executeOperation( address asset, uint256 amount, uint256 premium, address initiator, bytes calldata params ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.