Verified contractPartialMIT
CurveAdapter
Address0x8a1d3cd57c5c277707fa4488937251c55bb622f0
- 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,355
- Creation transaction
- 0x267787e79c…690a69c302
5 files
src/adapters/CurveAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import { IAdapter } from "src/interfaces/IAdapter.sol";
import { ICurvePool } from "src/interfaces/venues/ICurvePool.sol";
import { IWETH9 } from "src/interfaces/venues/IWETH9.sol";
import { SafeERC20Min } from "src/libraries/SafeERC20Min.sol";
interface IERC20Approvals {
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
}
/// @notice Curve StableSwap adapter for `Router` ids 96 (`CURVE_NG`) and 97 (`CURVE_LEGACY`).
/// Stateless: the `Router` transfers `amountIn` of `tokenIn` here, calls `swap`, and the
/// whole output is paid back to `msg.sender` within the call.
///
/// Both families use `exchange(int128,int128,uint256,uint256)`; `flags` = `i << 4 | j`.
/// A legacy pool with a native-ETH coin (`coins(k) == 0xEeee…EEeE`, the stETH/ETH pool)
/// 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.
///
/// `min_dy` 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 CurveAdapter is IAdapter {
using SafeERC20Min for address;
address public immutable WETH;
/// @dev The placeholder Curve uses for native ETH in `coins`.
address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
/// @dev Transient slot holding the pool being called, the only non-WETH address `receive` accepts ETH from.
bytes32 private constant _POOL_SLOT = keccak256("router-contracts.v1.CurveAdapter.pool");
error NotPool();
error CoinMismatch(uint256 index);
error ExchangeFailed();
error ApproveFailed();
error NoOutput();
constructor(address weth) {
WETH = weth;
}
/// @dev ETH arrives only from unwrapping WETH or as the called pool's native payout.
receive() external payable {
address pool;
bytes32 slot = _POOL_SLOT;
assembly ("memory-safe") {
pool := tload(slot)
}
if (msg.sender != WETH && (pool == address(0) || msg.sender != pool)) revert NotPool();
}
/// @inheritdoc IAdapter
function swap(
address pool,
uint8,
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256,
uint8 flags
) external returns (uint256 amountOut) {
uint256 i = flags >> 4;
uint256 j = flags & 0xF;
// Bind the indices to the tokens: a wrong index (or token) reverts here, before any
// value moves.
bool nativeIn = _checkCoin(pool, i, tokenIn);
bool nativeOut = _checkCoin(pool, j, tokenOut);
// A rebasing token (stETH) can land 1-2 wei short of `amountIn`; swap what is held.
uint256 dx = tokenIn.balanceOf(address(this));
if (dx > amountIn) dx = amountIn;
if (nativeIn) IWETH9(WETH).withdraw(dx);
else _approveExact(tokenIn, pool, dx);
uint256 before = nativeOut ? address(this).balance : tokenOut.balanceOf(address(this));
_setPool(pool);
_exchange(pool, i, j, dx, nativeIn);
_setPool(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 `coins(k)` must be `token`, or the native placeholder with `token == WETH`.
/// Reverts if `k` is out of range (the pool's own `coins` reverts).
function _checkCoin(address pool, uint256 k, address token) private view returns (bool native) {
address coin = ICurvePool(pool).coins(k);
if (coin == ETH) {
if (token != WETH) revert CoinMismatch(k);
return true;
}
if (coin != token) revert CoinMismatch(k);
}
/// @dev Raw call, return data discarded (3pool returns nothing); a real revert is bubbled.
function _exchange(address pool, uint256 i, uint256 j, uint256 dx, bool nativeIn) private {
(bool ok, bytes memory ret) = pool.call{ value: nativeIn ? dx : 0 }(
abi.encodeWithSelector(
ICurvePool.exchange.selector, int128(uint128(i)), int128(uint128(j)), dx, 0
)
);
if (!ok) {
if (ret.length == 0) revert ExchangeFailed();
assembly ("memory-safe") {
revert(add(ret, 32), mload(ret))
}
}
}
function _setPool(address pool) private {
bytes32 slot = _POOL_SLOT;
assembly ("memory-safe") {
tstore(slot, pool)
}
}
/// @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.encodeWithSelector(IERC20Approvals.approve.selector, spender, amount);
(bool ok, bytes memory ret) = token.call(data);
if (!ok || (ret.length != 0 && !abi.decode(ret, (bool)))) {
_approveRaw(token, abi.encodeWithSelector(IERC20Approvals.approve.selector, spender, 0));
_approveRaw(token, data);
}
}
/// @dev Clear a leftover allowance only if there is one.
function _revokeAllowance(address token, address spender) private {
if (IERC20Approvals(token).allowance(address(this), spender) != 0) {
_approveRaw(token, abi.encodeWithSelector(IERC20Approvals.approve.selector, 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/ICurvePool.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice The slice of a Curve StableSwap pool (legacy and stableswap-NG) the adapter touches.
/// `exchange` is declared here for its selector only: the adapter issues it raw, because
/// the oldest pools (3pool, Vyper 0.2.x) return nothing and a typed call would revert
/// decoding empty returndata.
interface ICurvePool {
function coins(uint256 k) external view returns (address);
function get_dy(int128 i, int128 j, uint256 dx) external view returns (uint256);
function exchange(int128 i, int128 j, uint256 dx, uint256 minDy) external payable;
}
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.