Verified contractExactMIT
OSwapWEthStEth
Address0x548cafad304e51b2e213653aa3a261db6a63e36d
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.23
- Same code
Only this contract
Details
- Compiler
- solc 0.8.23+commit.f704f362
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- paris
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 18,972,819
- Creation transaction
- 0xd5cdfa5d37…7e8574d433
6 files
- src/OSwapWEthStEth.solmain423 B
- src/Interfaces.sol3.46 KB
- src/LiquidityManagerStEth.sol3.47 KB
- src/OSwapBase.sol10.6 KB
- src/Ownable.sol1.35 KB
- src/OwnableOperable.sol1.46 KB
src/OSwapWEthStEth.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import {OSwapBase} from "./OSwapBase.sol";
import {LiquidityManagerStEth} from "./LiquidityManagerStEth.sol";
contract OSwapWEthStEth is OSwapBase, LiquidityManagerStEth {
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address constant STETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
constructor() OSwapBase(WETH, STETH) {}
}
src/Interfaces.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
function decimals() external view returns (uint8);
}
interface IWEth is IERC20 {
function deposit() external;
function withdraw(uint256 wad) external;
}
interface IOSwap {
function token0() external returns (address);
function token1() external returns (address);
function owner() external returns (address);
function swapExactTokensForTokens(IERC20, IERC20, uint256, uint256, address) external;
function swapExactTokensForTokens(uint256, uint256, address[] calldata, address, uint256)
external
returns (uint256[] memory);
function swapTokensForExactTokens(IERC20, IERC20, uint256, uint256, address) external;
function swapTokensForExactTokens(uint256, uint256, address[] calldata, address, uint256)
external
returns (uint256[] memory);
function setOwner(address newOwner) external;
function setTraderates(uint256 _traderate0, uint256 _traderate1) external;
function transferToken(address token, address to, uint256 amount) external;
}
interface IOSwapEth {
function initialize() external;
function swapExactETHForTokens(uint256 amountOutMin, address to) external payable;
function swapTokensForExactETH(uint256 amountOut, uint256 amountInMax, address to) external;
function swapExactTokensForETH(uint256 amountIn, uint256 amountOutMin, address to) external;
function swapETHForExactTokens(uint256 amountOut, address to) external payable;
function setOwner(address newOwner) external;
function setTraderates(uint256 _traderate0, uint256 _traderate1) external;
function transferToken(address tokenOut, address to, uint256 amount) external;
function transferEth(address to, uint256 amount) external;
}
interface ILiquidityManagerStEth {
function approveStETH() external;
function depositETHForStETH(uint256 amount) external;
function depositWETHForStETH(uint256 amount) external;
function requestStETHWithdrawalForETH(uint256[] memory amounts) external returns (uint256[] memory requestIds);
function claimStETHWithdrawalForETH(uint256[] memory requestIds) external;
function claimStETHWithdrawalForWETH(uint256[] memory requestIds) external;
function setOperator(address _operator) external;
}
interface IStETHWithdrawal {
function transferFrom(address _from, address _to, uint256 _requestId) external;
function ownerOf(uint256 _requestId) external returns (address);
function requestWithdrawals(uint256[] calldata _amounts, address _owner)
external
returns (uint256[] memory requestIds);
function getLastCheckpointIndex() external view returns (uint256);
function findCheckpointHints(uint256[] calldata _requestIds, uint256 _firstIndex, uint256 _lastIndex)
external
view
returns (uint256[] memory hintIds);
function claimWithdrawals(uint256[] calldata _requestIds, uint256[] calldata _hints) external;
}
src/LiquidityManagerStEth.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import {OwnableOperable} from "./OwnableOperable.sol";
import {IERC20, IWEth, IStETHWithdrawal} from "./Interfaces.sol";
contract LiquidityManagerStEth is OwnableOperable {
IERC20 public constant steth = IERC20(0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84);
IWEth public constant weth = IWEth(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
IStETHWithdrawal public constant withdrawal = IStETHWithdrawal(0x889edC2eDab5f40e902b864aD4d7AdE8E412F9B1);
/**
* @notice Approve the stETH withdrawal contract. Used for redemption requests.
*/
function approveStETH() external onlyOperatorOrOwner {
steth.approve(address(withdrawal), type(uint256).max);
}
/**
* @notice Mint stETH with ETH
*/
function depositETHForStETH(uint256 amount) external onlyOperatorOrOwner {
_depositETHForStETH(amount);
}
/**
* @notice Mint stETH with WETH
*/
function depositWETHForStETH(uint256 amount) external onlyOperatorOrOwner {
// Unwrap the WETH then deposit the ETH.
weth.withdraw(amount);
_depositETHForStETH(amount);
}
/**
* @notice Mint stETH with ETH.
* Reference: https://docs.lido.fi/contracts/lido#fallback
*/
function _depositETHForStETH(uint256 amount) internal {
require(address(this).balance >= amount, "OSwap: Insufficient ETH balance");
(bool success,) = address(steth).call{value: amount}(new bytes(0));
require(success, "OSwap: ETH transfer failed");
}
/**
* @notice Request a stETH for ETH withdrawal.
* Reference: https://docs.lido.fi/contracts/withdrawal-queue-erc721/
* Note: There is a 1k amount limit. Caller should split large withdrawals in chunks of less or equal to 1k each.)
*/
function requestStETHWithdrawalForETH(uint256[] memory amounts)
external
onlyOperatorOrOwner
returns (uint256[] memory requestIds)
{
requestIds = withdrawal.requestWithdrawals(amounts, address(this));
}
/**
* @notice Claim the ETH owed from the redemption requests.
* Before calling this method, caller should check on the request NFTs to ensure the withdrawal was processed.
*/
function claimStETHWithdrawalForETH(uint256[] memory requestIds) external onlyOperatorOrOwner {
_claimStETHWithdrawalForETH(requestIds);
}
/**
* @notice Claim the ETH owed from the redemption requests and convert it to WETH.
* Before calling this method, caller should check on the request NFTs to ensure the withdrawal was processed.
*/
function claimStETHWithdrawalForWETH(uint256[] memory requestIds) external onlyOperatorOrOwner {
// Claim the NFTs for ETH.
_claimStETHWithdrawalForETH(requestIds);
// Wrap all the received ETH to WETH.
(bool success,) = address(weth).call{value: address(this).balance}(new bytes(0));
require(success, "OSwap: ETH transfer failed");
}
function _claimStETHWithdrawalForETH(uint256[] memory requestIds) internal {
uint256 lastIndex = withdrawal.getLastCheckpointIndex();
uint256[] memory hintIds = withdrawal.findCheckpointHints(requestIds, 1, lastIndex);
withdrawal.claimWithdrawals(requestIds, hintIds);
}
// This method is necessary for receiving the ETH claimed as part of the withdrawal.
receive() external payable {}
}
src/OSwapBase.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import {OwnableOperable} from "./OwnableOperable.sol";
import {IERC20} from "./Interfaces.sol";
contract OSwapBase is OwnableOperable {
address public constant stETH = 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84;
IERC20 public immutable token0;
IERC20 public immutable token1;
/**
* @notice For one `token0` from a Trader, how many `token1` does the pool send.
* For example, if `token0` is WETH and `token1` is stETH then
* `traderate0` is the WETH/stETH price.
* From a Trader's perspective, this is the stETH/WETH buy price.
* Rate is to 36 decimals (1e36).
*/
uint256 public traderate0;
/**
* @notice For one `token1` from a Trader, how many `token0` does the pool send.
* For example, if `token0` is WETH and `token1` is stETH then
* `traderate1` is the stETH/WETH price.
* From a Trader's perspective, this is the stETH/WETH sell price.
* Rate is to 36 decimals (1e36).
*/
uint256 public traderate1;
/// @dev Maximum operator settable traderate. 1e36
uint256 internal constant MAX_OPERATOR_RATE = 1005 * 1e33;
/// @dev Minimum funds to allow operator to price changes
uint256 public minimumFunds;
event TraderateChanged(uint256 traderate0, uint256 traderate1);
constructor(address _token0, address _token1) {
token0 = IERC20(_token0);
token1 = IERC20(_token1);
require(IERC20(token0).decimals() == 18);
require(IERC20(token1).decimals() == 18);
_setOwner(address(0)); // Revoke owner for implementation contract at deployment
}
/**
* @notice Swaps an exact amount of input tokens for as many output tokens as possible.
* msg.sender should have already given the OSwap contract an allowance of
* at least amountIn on the input token.
*
* @param inToken Input token.
* @param outToken Output token.
* @param amountIn The amount of input tokens to send.
* @param amountOutMin The minimum amount of output tokens that must be received for the transaction not to revert.
* @param to Recipient of the output tokens.
*/
function swapExactTokensForTokens(
IERC20 inToken,
IERC20 outToken,
uint256 amountIn,
uint256 amountOutMin,
address to
) external {
_swapExactTokensForTokens(inToken, outToken, amountIn, amountOutMin, to);
}
/**
* @notice Uniswap V2 Router compatible interface. Swaps an exact amount of
* input tokens for as many output tokens as possible.
* msg.sender should have already given the OSwap contract an allowance of
* at least amountIn on the input token.
*
* @param amountIn The amount of input tokens to send.
* @param amountOutMin The minimum amount of output tokens that must be received for the transaction not to revert.
* @param path The input and output token addresses.
* @param to Recipient of the output tokens.
* @param deadline Unix timestamp after which the transaction will revert.
* @return amounts The input and output token amounts.
*/
function swapExactTokensForTokens(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts) {
_inDeadline(deadline);
require(path.length == 2, "OSwap: Invalid path length");
IERC20 inToken = IERC20(path[0]);
IERC20 outToken = IERC20(path[1]);
uint256 amountOut = _swapExactTokensForTokens(inToken, outToken, amountIn, amountOutMin, to);
amounts = new uint256[](2);
amounts[0] = amountIn;
amounts[1] = amountOut;
}
/**
* @notice Receive an exact amount of output tokens for as few input tokens as possible.
* msg.sender should have already given the router an allowance of
* at least amountInMax on the input token.
*
* @param inToken Input token.
* @param outToken Output token.
* @param amountOut The amount of output tokens to receive.
* @param amountInMax The maximum amount of input tokens that can be required before the transaction reverts.
* @param to Recipient of the output tokens.
*/
function swapTokensForExactTokens(
IERC20 inToken,
IERC20 outToken,
uint256 amountOut,
uint256 amountInMax,
address to
) external {
_swapTokensForExactTokens(inToken, outToken, amountOut, amountInMax, to);
}
/**
* @notice Uniswap V2 Router compatible interface. Receive an exact amount of
* output tokens for as few input tokens as possible.
* msg.sender should have already given the router an allowance of
* at least amountInMax on the input token.
*
* @param amountOut The amount of output tokens to receive.
* @param amountInMax The maximum amount of input tokens that can be required before the transaction reverts.
* @param path The input and output token addresses.
* @param to Recipient of the output tokens.
* @param deadline Unix timestamp after which the transaction will revert.
* @return amounts The input and output token amounts.
*/
function swapTokensForExactTokens(
uint256 amountOut,
uint256 amountInMax,
address[] calldata path,
address to,
uint256 deadline
) external returns (uint256[] memory amounts) {
_inDeadline(deadline);
require(path.length == 2, "OSwap: Invalid path length");
IERC20 inToken = IERC20(path[0]);
IERC20 outToken = IERC20(path[1]);
uint256 amountIn = _swapTokensForExactTokens(inToken, outToken, amountOut, amountInMax, to);
amounts = new uint256[](2);
amounts[0] = amountIn;
amounts[1] = amountOut;
}
function _swapExactTokensForTokens(
IERC20 inToken,
IERC20 outToken,
uint256 amountIn,
uint256 amountOutMin,
address to
) internal returns (uint256 amountOut) {
uint256 price;
if (inToken == token0) {
require(outToken == token1, "OSwap: Invalid token");
price = traderate0;
} else if (inToken == token1) {
require(outToken == token0, "OSwap: Invalid token");
price = traderate1;
} else {
revert("OSwap: Invalid token");
}
amountOut = amountIn * price / 1e36;
require(amountOut >= amountOutMin, "OSwap: Insufficient output amount");
// Transfer the input tokens from the caller to this OSwap contract
inToken.transferFrom(msg.sender, address(this), amountIn);
// Transfer the output tokens to the recipient
uint256 transferAmountOut = _calcTransferAmount(address(outToken), amountOut);
outToken.transfer(to, transferAmountOut);
}
function _swapTokensForExactTokens(
IERC20 inToken,
IERC20 outToken,
uint256 amountOut,
uint256 amountInMax,
address to
) internal returns (uint256 amountIn) {
uint256 price;
if (inToken == token0) {
require(outToken == token1, "OSwap: Invalid token");
price = traderate0;
} else if (inToken == token1) {
require(outToken == token0, "OSwap: Invalid token");
price = traderate1;
} else {
revert("OSwap: Invalid token");
}
amountIn = ((amountOut * 1e36) / price) + 1; // +1 to always round in our favor
require(amountIn <= amountInMax, "OSwap: Excess input amount");
// Transfer the input tokens from the caller to this OSwap contract
inToken.transferFrom(msg.sender, address(this), amountIn);
// Transfer the output tokens to the recipient
uint256 transferAmountOut = _calcTransferAmount(address(outToken), amountOut);
outToken.transfer(to, transferAmountOut);
}
/**
* @notice Calculate transfer amount for outToken.
* Due to internal stETH mechanics required for rebasing support,
* in most cases stETH transfers are performed for the value of 1 wei less than passed to transfer method.
* Larger transfer amounts can be 2 wei less.
*/
function _calcTransferAmount(address outToken, uint256 amount) internal pure returns (uint256 transferAmount) {
// Add 2 wei if transferring stETH
transferAmount = outToken == stETH ? amount + 2 : amount;
}
function _inDeadline(uint256 deadline) internal view {
require(deadline >= block.timestamp, "OSwap: Deadline expired");
}
/**
* @notice Set exchange rates from an operator account
* @param buyT1 The buy price of Token 1 (t0 -> t1), denominated in Token 0. 1e36
* @param sellT1 The sell price of Token 1 (t1 -> t0), denominated in Token 0. 1e36
*/
function setPrices(uint256 buyT1, uint256 sellT1) external onlyOperatorOrOwner {
uint256 _traderate0 = 1e72 / sellT1; // base (t0) -> token (t1)
uint256 _traderate1 = buyT1; // token (t1) -> base (t0)
// Limit funds and loss when called by operator
if (msg.sender == _operator()) {
uint256 currentFunds = token0.balanceOf(address(this)) + token1.balanceOf(address(this));
require(currentFunds > minimumFunds, "OSwap: Too much loss");
require(_traderate0 <= MAX_OPERATOR_RATE, "OSwap: Traderate too high");
require(_traderate1 <= MAX_OPERATOR_RATE, "OSwap: Traderate too high");
}
_setTraderates(_traderate0, _traderate1);
}
/**
* @notice Sets the minimum funds to allow operator price changes
*/
function setMinimumFunds(uint256 _minimumFunds) external onlyOwner {
minimumFunds = _minimumFunds;
}
function _setTraderates(uint256 _traderate0, uint256 _traderate1) internal {
require((1e72 / (_traderate0)) > _traderate1, "OSwap: Price cross");
traderate0 = _traderate0;
traderate1 = _traderate1;
emit TraderateChanged(_traderate0, _traderate1);
}
/**
* @notice Rescue token.
*/
function transferToken(address token, address to, uint256 amount) external onlyOwner {
IERC20(token).transfer(to, amount);
}
/**
* @notice Rescue ETH.
*/
function transferEth(address to, uint256 amount) external onlyOwner {
(bool success,) = to.call{value: amount}(new bytes(0));
require(success, "OSwap: ETH transfer failed");
}
}
src/Ownable.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
contract Ownable {
// keccak256(“eip1967.proxy.admin”) - per EIP 1967
bytes32 internal constant OWNER_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
event AdminChanged(address previousAdmin, address newAdmin);
constructor() {
assert(OWNER_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
_setOwner(msg.sender);
}
function owner() external view returns (address) {
return _owner();
}
function setOwner(address newOwner) external onlyOwner {
_setOwner(newOwner);
}
function _owner() internal view returns (address ownerOut) {
bytes32 position = OWNER_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
ownerOut := sload(position)
}
}
function _setOwner(address newOwner) internal {
emit AdminChanged(_owner(), newOwner);
bytes32 position = OWNER_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
sstore(position, newOwner)
}
}
function _onlyOwner() internal view {
require(msg.sender == _owner(), "OSwap: Only owner can call this function.");
}
modifier onlyOwner() {
_onlyOwner();
_;
}
}
src/OwnableOperable.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
import {Ownable} from "./Ownable.sol";
contract OwnableOperable is Ownable {
// keccak256(“eip1967.proxy.operator”) - 1, inspired by EIP 1967
bytes32 internal constant OPERATOR_SLOT = 0x14cc265c8475c78633f4e341e72b9f4f0d55277c8def4ad52d79e69580f31482;
event OperatorChanged(address previousAdmin, address newAdmin);
constructor() {
assert(OPERATOR_SLOT == bytes32(uint256(keccak256("eip1967.proxy.operator")) - 1));
}
function operator() external view returns (address) {
return _operator();
}
function setOperator(address newOperator) external onlyOwner {
_setOperator(newOperator);
}
function _operator() internal view returns (address operatorOut) {
bytes32 position = OPERATOR_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
operatorOut := sload(position)
}
}
function _setOperator(address newOperator) internal {
emit OperatorChanged(_operator(), newOperator);
bytes32 position = OPERATOR_SLOT;
// solhint-disable-next-line no-inline-assembly
assembly {
sstore(position, newOperator)
}
}
modifier onlyOperatorOrOwner() {
require(
msg.sender == _operator() || msg.sender == _owner(), "OSwap: Only operator or owner can call this function."
);
_;
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "paris",
"remappings": [
":ds-test/=lib/forge-std/lib/ds-test/src/",
":forge-std/=lib/forge-std/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.