|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity >=0.8.27; |
| 3 | + |
| 4 | +import {IERC20} from "@oz/token/ERC20/IERC20.sol"; |
| 5 | +import {SafeERC20} from "@oz/token/ERC20/utils/SafeERC20.sol"; |
| 6 | + |
| 7 | +import {IRegistry} from "@aztec/governance/interfaces/IRegistry.sol"; |
| 8 | +import {IInbox} from "@aztec/core/interfaces/messagebridge/IInbox.sol"; |
| 9 | +import {IOutbox} from "@aztec/core/interfaces/messagebridge/IOutbox.sol"; |
| 10 | +import {IRollup} from "@aztec/core/interfaces/IRollup.sol"; |
| 11 | +import {DataStructures} from "@aztec/core/libraries/DataStructures.sol"; |
| 12 | +import {Hash} from "@aztec/core/libraries/crypto/Hash.sol"; |
| 13 | +import {Epoch} from "@aztec/core/libraries/TimeLib.sol"; |
| 14 | + |
| 15 | +// docs:start:portal_setup |
| 16 | +interface IAavePool { |
| 17 | + function supply(address asset, uint256 amount, address onBehalfOf, uint16 referralCode) external; |
| 18 | + function withdraw(address asset, uint256 amount, address to) external returns (uint256); |
| 19 | +} |
| 20 | + |
| 21 | +contract AavePortal { |
| 22 | + using SafeERC20 for IERC20; |
| 23 | + |
| 24 | + IRegistry public registry; |
| 25 | + IERC20 public underlying; |
| 26 | + IERC20 public aToken; |
| 27 | + IAavePool public aavePool; |
| 28 | + bytes32 public l2Bridge; |
| 29 | + |
| 30 | + IRollup public rollup; |
| 31 | + IOutbox public outbox; |
| 32 | + IInbox public inbox; |
| 33 | + uint256 public rollupVersion; |
| 34 | + |
| 35 | + bool private _initialized; |
| 36 | + |
| 37 | + function initialize( |
| 38 | + address _registry, |
| 39 | + address _underlying, |
| 40 | + address _aToken, |
| 41 | + address _aavePool, |
| 42 | + bytes32 _l2Bridge |
| 43 | + ) external { |
| 44 | + require(!_initialized, "Already initialized"); |
| 45 | + _initialized = true; |
| 46 | + |
| 47 | + registry = IRegistry(_registry); |
| 48 | + underlying = IERC20(_underlying); |
| 49 | + aToken = IERC20(_aToken); |
| 50 | + aavePool = IAavePool(_aavePool); |
| 51 | + l2Bridge = _l2Bridge; |
| 52 | + |
| 53 | + rollup = IRollup(address(registry.getCanonicalRollup())); |
| 54 | + outbox = rollup.getOutbox(); |
| 55 | + inbox = rollup.getInbox(); |
| 56 | + rollupVersion = rollup.getVersion(); |
| 57 | + } |
| 58 | + // docs:end:portal_setup |
| 59 | + |
| 60 | + // docs:start:portal_deposit_to_aave |
| 61 | + /// @notice Consume an L2->L1 withdraw message and deposit the underlying tokens into Aave |
| 62 | + /// @dev The content hash must match what the L2 bridge emits via get_withdraw_content_hash |
| 63 | + function depositToAave( |
| 64 | + address _recipient, |
| 65 | + uint256 _amount, |
| 66 | + bool _withCaller, |
| 67 | + Epoch _epoch, |
| 68 | + uint256 _leafIndex, |
| 69 | + bytes32[] calldata _path |
| 70 | + ) external { |
| 71 | + // Reconstruct the L2->L1 message (must match the L2 bridge's exit_to_l1_public/private) |
| 72 | + DataStructures.L2ToL1Msg memory message = DataStructures.L2ToL1Msg({ |
| 73 | + sender: DataStructures.L2Actor(l2Bridge, rollupVersion), |
| 74 | + recipient: DataStructures.L1Actor(address(this), block.chainid), |
| 75 | + content: Hash.sha256ToField( |
| 76 | + abi.encodeWithSignature( |
| 77 | + "withdraw(address,uint256,address)", |
| 78 | + _recipient, |
| 79 | + _amount, |
| 80 | + _withCaller ? msg.sender : address(0) |
| 81 | + ) |
| 82 | + ) |
| 83 | + }); |
| 84 | + |
| 85 | + // Consume the message from the outbox (verifies merkle proof) |
| 86 | + outbox.consume(message, _epoch, _leafIndex, _path); |
| 87 | + |
| 88 | + // Deposit into Aave instead of sending tokens to the recipient. |
| 89 | + // The portal must already hold the underlying tokens (pre-funded or bridged separately). |
| 90 | + underlying.approve(address(aavePool), _amount); |
| 91 | + aavePool.supply(address(underlying), _amount, address(this), 0); |
| 92 | + } |
| 93 | + // docs:end:portal_deposit_to_aave |
| 94 | + |
| 95 | + // docs:start:portal_claim_public |
| 96 | + /// @notice Withdraw from Aave and send an L1->L2 message to mint tokens publicly on L2 |
| 97 | + function claimFromAavePublic( |
| 98 | + uint256 _aTokenAmount, |
| 99 | + bytes32 _to, |
| 100 | + bytes32 _secretHash |
| 101 | + ) external returns (bytes32, uint256) { |
| 102 | + // Withdraw from Aave (returns underlying + yield) |
| 103 | + aToken.approve(address(aavePool), _aTokenAmount); |
| 104 | + uint256 withdrawn = aavePool.withdraw(address(underlying), _aTokenAmount, address(this)); |
| 105 | + |
| 106 | + // Send L1->L2 message with the total withdrawn amount (including yield) |
| 107 | + DataStructures.L2Actor memory actor = DataStructures.L2Actor(l2Bridge, rollupVersion); |
| 108 | + bytes32 contentHash = |
| 109 | + Hash.sha256ToField(abi.encodeWithSignature("mint_to_public(bytes32,uint256)", _to, withdrawn)); |
| 110 | + |
| 111 | + (bytes32 key, uint256 index) = inbox.sendL2Message(actor, contentHash, _secretHash); |
| 112 | + return (key, index); |
| 113 | + } |
| 114 | + // docs:end:portal_claim_public |
| 115 | + |
| 116 | + // docs:start:portal_claim_private |
| 117 | + /// @notice Withdraw from Aave and send an L1->L2 message to mint tokens privately on L2 |
| 118 | + function claimFromAavePrivate( |
| 119 | + uint256 _aTokenAmount, |
| 120 | + bytes32 _secretHash |
| 121 | + ) external returns (bytes32, uint256) { |
| 122 | + // Withdraw from Aave (returns underlying + yield) |
| 123 | + aToken.approve(address(aavePool), _aTokenAmount); |
| 124 | + uint256 withdrawn = aavePool.withdraw(address(underlying), _aTokenAmount, address(this)); |
| 125 | + |
| 126 | + // Send L1->L2 message for private minting |
| 127 | + DataStructures.L2Actor memory actor = DataStructures.L2Actor(l2Bridge, rollupVersion); |
| 128 | + bytes32 contentHash = |
| 129 | + Hash.sha256ToField(abi.encodeWithSignature("mint_to_private(uint256)", withdrawn)); |
| 130 | + |
| 131 | + (bytes32 key, uint256 index) = inbox.sendL2Message(actor, contentHash, _secretHash); |
| 132 | + return (key, index); |
| 133 | + } |
| 134 | + // docs:end:portal_claim_private |
| 135 | +} |
0 commit comments