Verified contractPartialMIT
PammAdapter
Address0xd044d130813bc700deb8a60ce29960239dd6ee2f
- 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,358
- Creation transaction
- 0x9b3fe07461…91f48880c3
5 files
src/adapters/PammAdapter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import { IAdapter } from "src/interfaces/IAdapter.sol";
import { IFermiSwapper } from "src/interfaces/venues/IFermiSwapper.sol";
import { IPropAMM } from "src/interfaces/venues/IPropAMM.sol";
import { SafeERC20Min } from "src/libraries/SafeERC20Min.sol";
/// @notice Proprietary-AMM adapter for `Router` ids 224 (`FERMI`), 225 (`TEMPEST`) and
/// 226 (`BEBOP`). The leg carries its venue (`pool`, RFC Q26), kept inside the published
/// universe off chain; `params` is 0. Stateless: the `Router` transfers `amountIn` here,
/// calls `swap`, and the whole output is paid back to `msg.sender` within the call.
///
/// - 224 Fermi: PULL. Exact approval to the swapper, `fermiSwapWithAllowances`, approval
/// revoked, any unspent input returned to `msg.sender`.
/// - 225 Tempest, 226 Bebop v2: PUSH. `amountIn` is transferred to the venue, then
/// `IPropAMM.swap` is called with `deadline = block.timestamp` (inclusive; a route is
/// priced against the block it lands in).
///
/// Bebop v2 (`0xB09AAA89…`, PUR v2 registry) answers the same `IPropAMM.swap` selector,
/// push-then-call order and inclusive deadline as v1 (`0xB09AaA56…`): verified on a
/// mainnet fork at a real v2 swap's pre-state (`PammAdapterForkTest`) and against
/// `MockBebopV2`.
///
/// The venue-side `minOut` is 1 (a nonzero floor, so a venue paying nothing reverts at
/// the venue): 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. Every lane is stale-gated by the
/// venue (`0x666a2814`), which is bubbled verbatim.
contract PammAdapter is IAdapter {
using SafeERC20Min for address;
uint8 internal constant FERMI = 224;
uint8 internal constant TEMPEST = 225;
uint8 internal constant BEBOP = 226;
/// @dev Venue-side floor; see the contract NatSpec.
uint256 internal constant VENUE_MIN_OUT = 1;
error UnknownProtocol(uint8 protocolId);
error NoOutput();
error AmountTooLarge();
error ApproveFailed();
/// @inheritdoc IAdapter
function swap(
address pool,
uint8 protocolId,
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256,
uint8
) external returns (uint256 amountOut) {
if (protocolId != FERMI && protocolId != TEMPEST && protocolId != BEBOP) {
revert UnknownProtocol(protocolId);
}
uint256 outBefore = tokenOut.balanceOf(address(this));
if (protocolId == FERMI) {
// forge-lint: disable-next-line(unsafe-typecast)
if (amountIn > uint256(type(int256).max)) revert AmountTooLarge();
uint256 inBefore = tokenIn.balanceOf(address(this)) - amountIn;
_approveExact(tokenIn, pool, amountIn);
// forge-lint: disable-next-line(unsafe-typecast)
int256 amount = int256(amountIn); // <= int256.max, checked above
_call(
pool,
abi.encodeCall(
IFermiSwapper.fermiSwapWithAllowances,
(tokenIn, tokenOut, amount, VENUE_MIN_OUT, address(this))
)
);
_revokeAllowance(tokenIn, pool);
uint256 unspent = tokenIn.balanceOf(address(this)) - inBefore;
if (unspent != 0) tokenIn.safeTransfer(msg.sender, unspent);
} else {
tokenIn.safeTransfer(pool, amountIn);
_call(
pool,
abi.encodeCall(
IPropAMM.swap,
(tokenIn, tokenOut, amountIn, VENUE_MIN_OUT, address(this), block.timestamp)
)
);
}
amountOut = tokenOut.balanceOf(address(this)) - outBefore;
if (amountOut == 0) revert NoOutput();
tokenOut.safeTransfer(msg.sender, amountOut);
}
/// @dev Raw call so a venue revert is bubbled verbatim.
function _call(address venue, bytes memory data) private {
(bool ok, bytes memory ret) = venue.call(data);
if (!ok) {
if (ret.length == 0) revert NoOutput();
assembly ("memory-safe") {
revert(add(ret, 32), mload(ret))
}
}
}
/// @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/IFermiSwapper.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice Fermi pAMM swapper (v2 `0x5979458912F80B96d30D4220af8E2e4925A33320`). PULL settlement:
/// the caller approves the swapper on `tokenIn`; the swapper `transferFrom`s the input
/// and sends `tokenOut` to `recipient`. Reverts `StaleUpdate` (`0x666a2814`) unless
/// `block.timestamp` equals the `PrioUpdateRegistry` lane's stored timestamp, so a leg is
/// executable only top-of-block.
interface IFermiSwapper {
/// @param tokenIn Input token.
/// @param tokenOut Output token.
/// @param amount Exact-in amount; a positive `int256` (negative reads as exact-out).
/// @param minOut Venue-enforced minimum output.
/// @param recipient Receiver of `tokenOut`.
function fermiSwapWithAllowances(
address tokenIn,
address tokenOut,
int256 amount,
uint256 minOut,
address recipient
) external;
}
src/interfaces/venues/IPropAMM.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
/// @notice The `IPropAMM` entry point of the push-settled pAMM venues (Tempest
/// `0x00000003f1ec2379e79F58E12EC6C4F51Ee92149`, Bebop v2
/// `0xB09AAA8933626d7E4C48D65dAd2D77021CFBCA9a`). Selector `0x9908fc8b`, recovered by
/// tracing (the venues are unverified).
///
/// PUSH settlement: the caller transfers `amountIn` of `tokenIn` to the venue first,
/// then calls `swap`; the venue pays out of the balance it was handed and never calls
/// `transferFrom(msg.sender, ...)`. Anything pushed and not consumed is lost. Not
/// caller-gated; `recipient` is free. The price is read from the shared
/// `PrioUpdateRegistry`, which reverts `0x666a2814` when the lane is stale (Tempest
/// tolerates a few seconds, Bebop none), so a leg is executable only in or just after
/// the block its quote was published into.
interface IPropAMM {
/// @param tokenIn Token already pushed to the venue.
/// @param tokenOut Token the maker pays out.
/// @param amountIn The pushed amount.
/// @param minAmountOut Venue-enforced minimum output.
/// @param recipient Receiver of `tokenOut`.
/// @param deadline Inclusive: `block.timestamp` passes.
/// @return amountOut The realised output.
function swap(
address tokenIn,
address tokenOut,
uint256 amountIn,
uint256 minAmountOut,
address recipient,
uint256 deadline
) external returns (uint256 amountOut);
/// @notice Realised output for `amountIn`; reverts `0x666a2814` when the lane is stale.
function quote(address tokenIn, address tokenOut, uint256 amountIn)
external
view
returns (uint256);
}
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.