Verified contractPartialMIT
FluidAdapter
Address0x4d7bd7a052a82efd16e131d05187ddf4f8399b8c
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.30
- Same code
2 more contracts
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 1,000 runs
- EVM version
- osaka
- Via IR
- yes
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,290,356
- Creation transaction
- 0x49fcb73aaa…e87f618afc
5 files
src/adapters/FluidAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import { IAdapter } from "src/interfaces/IAdapter.sol";
import { IFluidDexT1 } from "src/interfaces/venues/IFluidDexT1.sol";
import { IWETH9 } from "src/interfaces/venues/IWETH9.sol";
import { SafeERC20Min } from "src/libraries/SafeERC20Min.sol";
/// @notice Instadapp Fluid DEX (`FluidDexT1`) adapter for `Router` id 101 (`FLUID`).
/// Stateless: the `Router` transfers `amountIn` of `tokenIn` here, calls `swap`, and the
/// whole output is paid back to `msg.sender` within the call.
///
/// The direction comes from the pool's own `constantsView()`: `{tokenIn, tokenOut}` must
/// equal `{token0, token1}`, otherwise the call reverts before any value moves. A pool
/// side spelled `0xEeee…EEeE` (native ETH) is bridged in WETH terms: a native input is
/// unwrapped from the WETH held and sent as `msg.value`; a native output is wrapped
/// before it is paid out. An ERC20 input is pulled by the pool through an exact allowance,
/// cleared afterwards.
///
/// `amountOutMin` is 0: the settler's floor on the route's end-to-end output is the
/// guarantee, a per-leg minimum would only add a second, weaker check.
/// Output is measured as a balance difference, never from the return value.
contract FluidAdapter is IAdapter {
using SafeERC20Min for address;
address public immutable WETH;
/// @dev The placeholder Fluid uses for native ETH in `token0` / `token1`.
address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @dev Transient slot holding the called pool's Liquidity Layer (`constantsView().liquidity`),
/// the only non-WETH address `receive` accepts ETH from, and only during the swap: Fluid
/// pays native output from the shared Liquidity Layer, not from the pool.
bytes32 private constant _PAYER_SLOT = keccak256("router-contracts.v1.FluidAdapter.payer");
error NotPool();
error TokenNotInPool();
error NoOutput();
error ApproveFailed();
constructor(address weth) {
WETH = weth;
}
/// @dev ETH arrives only from unwrapping WETH or as the Liquidity Layer's native payout.
receive() external payable {
address payer;
bytes32 slot = _PAYER_SLOT;
assembly ("memory-safe") {
payer := tload(slot)
}
if (msg.sender != WETH && (payer == address(0) || msg.sender != payer)) revert NotPool();
}
/// @inheritdoc IAdapter
function swap(
address pool,
uint8,
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256,
uint8
) external returns (uint256 amountOut) {
(bool zeroForOne, bool nativeIn, bool nativeOut, address liquidity) =
_direction(pool, tokenIn, tokenOut);
if (nativeIn) IWETH9(WETH).withdraw(amountIn);
else _approveExact(tokenIn, pool, amountIn);
uint256 before = nativeOut ? address(this).balance : tokenOut.balanceOf(address(this));
_setPayer(liquidity);
_swapIn(pool, zeroForOne, amountIn, nativeIn);
_setPayer(address(0));
if (nativeOut) {
amountOut = address(this).balance - before;
if (amountOut == 0) revert NoOutput();
IWETH9(WETH).deposit{ value: amountOut }();
} else {
amountOut = tokenOut.balanceOf(address(this)) - before;
if (amountOut == 0) revert NoOutput();
}
tokenOut.safeTransfer(msg.sender, amountOut);
if (!nativeIn) _revokeAllowance(tokenIn, pool);
}
/// @dev Bind `{tokenIn, tokenOut}` to the pool's `{token0, token1}` and derive the direction.
function _direction(address pool, address tokenIn, address tokenOut)
private
view
returns (bool zeroForOne, bool nativeIn, bool nativeOut, address liquidity)
{
IFluidDexT1.ConstantViews memory cv = IFluidDexT1(pool).constantsView();
if (_is(cv.token0, tokenIn) && _is(cv.token1, tokenOut)) {
return (true, cv.token0 == ETH, cv.token1 == ETH, cv.liquidity);
}
if (_is(cv.token1, tokenIn) && _is(cv.token0, tokenOut)) {
return (false, cv.token1 == ETH, cv.token0 == ETH, cv.liquidity);
}
revert TokenNotInPool();
}
/// @dev `coin` is `token`, or the native placeholder with `token == WETH`.
function _is(address coin, address token) private view returns (bool) {
return coin == token || (coin == ETH && token == WETH);
}
/// @dev Raw call so a pool revert is bubbled verbatim.
function _swapIn(address pool, bool zeroForOne, uint256 amountIn, bool nativeIn) private {
(bool ok, bytes memory ret) = pool.call{ value: nativeIn ? amountIn : 0 }(
abi.encodeCall(IFluidDexT1.swapIn, (zeroForOne, amountIn, 0, address(this)))
);
if (!ok) {
if (ret.length == 0) revert NoOutput();
assembly ("memory-safe") {
revert(add(ret, 32), mload(ret))
}
}
}
function _setPayer(address payer) private {
bytes32 slot = _PAYER_SLOT;
assembly ("memory-safe") {
tstore(slot, payer)
}
}
/// @dev Grant `spender` exactly `amount` in one `approve` on the happy path; a token that
/// refuses a non-zero to non-zero write (USDT) takes the `approve(0)` fallback.
function _approveExact(address token, address spender, uint256 amount) private {
bytes memory data = abi.encodeWithSignature("approve(address,uint256)", spender, amount);
(bool ok, bytes memory ret) = token.call(data);
if (!ok || (ret.length != 0 && !abi.decode(ret, (bool)))) {
_approveRaw(token, abi.encodeWithSignature("approve(address,uint256)", spender, 0));
_approveRaw(token, data);
}
}
/// @dev Clear a leftover allowance only if there is one.
function _revokeAllowance(address token, address spender) private {
(bool ok, bytes memory ret) = token.staticcall(
abi.encodeWithSignature("allowance(address,address)", address(this), spender)
);
if (ok && ret.length == 32 && abi.decode(ret, (uint256)) != 0) {
_approveRaw(token, abi.encodeWithSignature("approve(address,uint256)", spender, 0));
}
}
function _approveRaw(address token, bytes memory data) private {
(bool ok, bytes memory ret) = token.call(data);
if (!ok || (ret.length != 0 && !abi.decode(ret, (bool)))) revert ApproveFailed();
}
}
src/interfaces/IAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice A non-core leg's executor (RFC "Router": adapter families and the pAMM adapter). The
/// `Router` reaches it by `call`, never `delegatecall`, at an address that is immutable
/// in the `Router`'s code.
///
/// Funding: the `Router` transfers `amountIn` of `tokenIn` to the adapter before the call.
/// The adapter pays the whole output to `msg.sender` within the call and holds nothing
/// after it. The `Router` measures its own `tokenOut` balance across the call and uses
/// that, not the return value.
interface IAdapter {
/// @param pool The pool or venue the leg carries (Curve, Fluid, pAMM ids), or
/// `address(0)` for ids whose adapter holds its own cores (Ekubo).
/// @param protocolId The leg's 8-bit id, so one adapter can serve several ids of one family.
/// @param params The leg's right-aligned params field: Ekubo's raw 256-bit pool config,
/// 0 for ids that carry none.
/// @param flags The leg's 8 flag bits; their meaning is per family (Curve: i 4 | j 4).
function swap(
address pool,
uint8 protocolId,
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256 params,
uint8 flags
) external returns (uint256 amountOut);
}
src/interfaces/venues/IFluidDexT1.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice Slice of an Instadapp Fluid DEX pool (`FluidDexT1`): the token pair and the
/// exact-input swap.
interface IFluidDexT1 {
struct Implementations {
address shift;
address admin;
address colOperations;
address debtOperations;
address perfectOperationsAndSwapOut;
}
/// @dev Static struct, so the return decodes as flat words; only `token0`/`token1` are read.
/// Native ETH is spelled `0xEeee…EEeE`.
struct ConstantViews {
uint256 dexId;
address liquidity;
address factory;
Implementations implementations;
address deployerContract;
address token0;
address token1;
bytes32 supplyToken0Slot;
bytes32 borrowToken0Slot;
bytes32 supplyToken1Slot;
bytes32 borrowToken1Slot;
bytes32 exchangePriceToken0Slot;
bytes32 exchangePriceToken1Slot;
uint256 oracleMapping;
}
function constantsView() external view returns (ConstantViews memory);
/// @dev Exact-input swap. `swap0to1` swaps token0 for token1. An ERC20 input is pulled from
/// `msg.sender` by allowance; a native input is `msg.value`. Output goes to `to`.
function swapIn(bool swap0to1, uint256 amountIn, uint256 amountOutMin, address to)
external
payable
returns (uint256 amountOut);
}
src/interfaces/venues/IWETH9.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice Canonical WETH: wrap and unwrap, for V4 pools whose WETH side is native ETH.
interface IWETH9 {
function deposit() external payable;
function withdraw(uint256 amount) external;
}
src/libraries/SafeERC20Min.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice Minimal ERC20 surface for `SafeERC20Min`: empty returndata is success (USDT), a
/// returned word is the bool it claims.
interface IERC20Min {
function balanceOf(address) external view returns (uint256);
function transferFrom(address, address, uint256) external returns (bool);
function transfer(address, uint256) external returns (bool);
}
library SafeERC20Min {
function safeTransferFrom(address token, address from, address to, uint256 amount) internal {
_call(token, abi.encodeWithSelector(IERC20Min.transferFrom.selector, from, to, amount));
}
function safeTransfer(address token, address to, uint256 amount) internal {
_call(token, abi.encodeWithSelector(IERC20Min.transfer.selector, to, amount));
}
function balanceOf(address token, address who) internal view returns (uint256) {
return IERC20Min(token).balanceOf(who);
}
function _call(address token, bytes memory data) private {
(bool ok, bytes memory ret) = token.call(data);
require(ok && (ret.length == 0 || abi.decode(ret, (bool))), "erc20 call failed");
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"appendCBOR": false,
"bytecodeHash": "none",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 1000,
"enabled": true
},
"evmVersion": "osaka",
"remappings": [
"forge-std/=lib/forge-std/src/",
"priority-update-registry/=vendor/priority-update-registry/src/",
"solady/=lib/priority-update-registry/lib/solady/src/",
"@openzeppelin/=lib/priority-update-registry/lib/openzeppelin-contracts/",
"erc4626-tests/=lib/priority-update-registry/lib/openzeppelin-contracts/lib/erc4626-tests/",
"halmos-cheatcodes/=lib/priority-update-registry/lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
"openzeppelin-contracts/=lib/priority-update-registry/lib/openzeppelin-contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.