Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add (un)/wrapping for steth #317

Merged
merged 11 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion contracts/base/Dispatcher.sol
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,24 @@ abstract contract Dispatcher is Payments, V2SwapRouter, V3SwapRouter, Callbacks,
spender := calldataload(add(inputs.offset, 0x20))
}
Payments.approveERC20(token, spender);
} else if (command == Commands.WRAP_STETH) {
address recipient;
uint256 amount;
assembly {
recipient := calldataload(inputs.offset)
amount := calldataload(add(inputs.offset, 0x20))
}
Payments.wrapSTETH(map(recipient), amount);
} else if (command == Commands.UNWRAP_STETH) {
address recipient;
uint256 amountMin;
assembly {
recipient := calldataload(inputs.offset)
amountMin := calldataload(add(inputs.offset, 0x20))
}
Payments.unwrapSTETH(map(recipient), amountMin);
} else {
// placeholder area for commands 0x22-0x3f
// placeholder area for commands 0x25-0x3f
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added 2 commands but this number increased by 3? Seems it switched from inclusive to exclusive

Copy link
Member Author

@ewilz ewilz Sep 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this was just forgotten in a past PR

revert InvalidCommandType(command);
}
}
Expand Down
9 changes: 9 additions & 0 deletions contracts/base/RouterImmutables.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ pragma solidity ^0.8.17;
import {IAllowanceTransfer} from 'permit2/src/interfaces/IAllowanceTransfer.sol';
import {ERC20} from 'solmate/src/tokens/ERC20.sol';
import {IWETH9} from '../interfaces/external/IWETH9.sol';
import {IWSTETH} from '../interfaces/external/IWSTETH.sol';

struct RouterParameters {
address permit2;
address weth9;
address steth;
address wsteth;
address seaportV1_5;
address seaportV1_4;
address openseaConduit;
Expand All @@ -34,6 +37,10 @@ contract RouterImmutables {
/// @dev WETH9 address
IWETH9 internal immutable WETH9;

ERC20 internal immutable STETH;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add @ dev comments


IWSTETH internal immutable WSTETH;

/// @dev Permit2 address
IAllowanceTransfer internal immutable PERMIT2;

Expand Down Expand Up @@ -99,6 +106,8 @@ contract RouterImmutables {
constructor(RouterParameters memory params) {
PERMIT2 = IAllowanceTransfer(params.permit2);
WETH9 = IWETH9(params.weth9);
WSTETH = IWSTETH(params.wsteth);
STETH = ERC20(params.steth);
SEAPORT_V1_5 = params.seaportV1_5;
SEAPORT_V1_4 = params.seaportV1_4;
OPENSEA_CONDUIT = params.openseaConduit;
Expand Down
24 changes: 24 additions & 0 deletions contracts/interfaces/external/IWSTETH.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.4;

import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';

/// @title Interface for wstETH
interface IWSTETH is IERC20 {
/// @notice wrap steth to get wsteth
function wrap(uint256 stETHAmount) external returns (uint256);

/// @notice unwrap wsteth to get steth
function unwrap(uint256 wstETHAmount) external returns (uint256);

function tokensPerStEth() external view returns (uint256);

function stEthPerToken() external view returns (uint256);

function stETH() external view returns (address);

function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
external;

function DOMAIN_SEPARATOR() external view returns (bytes32);
}
3 changes: 3 additions & 0 deletions contracts/libraries/Commands.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,8 @@ library Commands {
uint256 constant SEAPORT_V1_4 = 0x20;
uint256 constant EXECUTE_SUB_PLAN = 0x21;
uint256 constant APPROVE_ERC20 = 0x22;
uint256 constant WRAP_STETH = 0x23;
uint256 constant UNWRAP_STETH = 0x24;

// COMMAND_PLACEHOLDER for 0x23 to 0x3f (all unused)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: change comment for new command values

}
34 changes: 34 additions & 0 deletions contracts/modules/Payments.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ abstract contract Payments is RouterImmutables {

error InsufficientToken();
error InsufficientETH();
error InsufficientSTETH();
error InvalidBips();
error InvalidSpender();

Expand Down Expand Up @@ -137,4 +138,37 @@ abstract contract Payments is RouterImmutables {
}
}
}

/// @notice Wrap an amount of stETH into wstETH
/// @param recipient The recipient of the stETH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wstETH

/// @param amount The amount of wstETH desired
function wrapSTETH(address recipient, uint256 amount) internal {
if (amount == Constants.CONTRACT_BALANCE) {
amount = STETH.balanceOf(address(this));
} else if (amount > STETH.balanceOf(address(this))) {
revert InsufficientSTETH();
}
if (amount > 0) {
WSTETH.wrap(amount);
if (recipient != address(this)) {
WSTETH.transfer(recipient, amount);
ewilz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// @notice Unwraps all of the contract's wstETH into stETH
/// @param recipient The recipient of the stETH
/// @param amountMinimum The minimum amount of ETH desired
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: amount of stETH desired

function unwrapSTETH(address recipient, uint256 amountMinimum) internal {
uint256 amount = WSTETH.balanceOf(address(this));
if (amount < amountMinimum) {
revert InsufficientSTETH();
}
if (amount > 0) {
WSTETH.unwrap(amount);
if (recipient != address(this)) {
recipient.safeTransferETH(amount);
ewilz marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
2 changes: 1 addition & 1 deletion deploy-addresses/base-goerli.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"UniversalRouterV1_2": "0xd0872d928672ae2ff74bdb2f5130ac12229cafaf",
"UnsupportedProtocol": "0x7B46ee9BaB49bd5b37117494689A035b0F187B59"
}
}
4 changes: 4 additions & 0 deletions script/DeployUniversalRouter.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ abstract contract DeployUniversalRouter is Script {
params = RouterParameters({
permit2: mapUnsupported(params.permit2),
weth9: mapUnsupported(params.weth9),
steth: mapUnsupported(params.steth),
wsteth: mapUnsupported(params.wsteth),
seaportV1_5: mapUnsupported(params.seaportV1_5),
seaportV1_4: mapUnsupported(params.seaportV1_4),
openseaConduit: mapUnsupported(params.openseaConduit),
Expand Down Expand Up @@ -69,6 +71,8 @@ abstract contract DeployUniversalRouter is Script {
function logParams() internal view {
console2.log('permit2:', params.permit2);
console2.log('weth9:', params.weth9);
console2.log('steth:', params.steth);
console2.log('wsteth:', params.wsteth);
console2.log('seaportV1_5:', params.seaportV1_5);
console2.log('seaportV1_4:', params.seaportV1_4);
console2.log('openseaConduit:', params.openseaConduit);
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployArbitrum.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployArbitrum is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x82aF49447D8a07e3bd95BD0d56f35241523fBab1,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployArbitrumGoerli.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployArbitrumGoerli is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0xe39Ab88f8A4777030A534146A9Ca3B52bd5D43A3,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: UNSUPPORTED_PROTOCOL,
seaportV1_4: UNSUPPORTED_PROTOCOL,
openseaConduit: UNSUPPORTED_PROTOCOL,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployAvalanche.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployAvalanche is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployBSC.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployBSC is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployBase.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployBase is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x4200000000000000000000000000000000000006,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployBaseGoerli.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployBaseGoerli is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x44D627f900da8AdaC7561bD73aA745F132450798,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployCelo.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployCelo is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: UNSUPPORTED_PROTOCOL,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: UNSUPPORTED_PROTOCOL,
seaportV1_4: UNSUPPORTED_PROTOCOL,
openseaConduit: UNSUPPORTED_PROTOCOL,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployCeloAlfajores.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployCeloAlfajores is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: UNSUPPORTED_PROTOCOL,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: UNSUPPORTED_PROTOCOL,
seaportV1_4: UNSUPPORTED_PROTOCOL,
openseaConduit: UNSUPPORTED_PROTOCOL,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployGoerli.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployGoerli is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployMainnet.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployMainnet is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2,
steth: 0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84,
wsteth: 0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployOptimism.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployOptimism is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x4200000000000000000000000000000000000006,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployOptimismGoerli.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployOptimismGoerli is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x4200000000000000000000000000000000000006,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: UNSUPPORTED_PROTOCOL,
seaportV1_4: UNSUPPORTED_PROTOCOL,
openseaConduit: UNSUPPORTED_PROTOCOL,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployPolygon.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ contract DeployPolygon is DeployUniversalRouter {
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
nftxZap: UNSUPPORTED_PROTOCOL,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeployPolygonMumbai.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeployPolygonMumbai is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions script/deployParameters/DeploySepolia.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ contract DeploySepolia is DeployUniversalRouter {
params = RouterParameters({
permit2: 0x000000000022D473030F116dDEE9F6B43aC78BA3,
weth9: 0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14,
steth: UNSUPPORTED_PROTOCOL,
wsteth: UNSUPPORTED_PROTOCOL,
seaportV1_5: 0x00000000000000ADc04C56Bf30aC9d3c0aAF14dC,
seaportV1_4: 0x00000000000001ad428e4906aE43D8F9852d0dD6,
openseaConduit: 0x1E0049783F008A0085193E00003D00cd54003c71,
Expand Down
2 changes: 2 additions & 0 deletions test/foundry-tests/UniswapV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract contract UniswapV2Test is Test {
RouterParameters memory params = RouterParameters({
permit2: address(PERMIT2),
weth9: address(WETH9),
steth: address(0),
wsteth: address(0),
seaportV1_5: address(0),
seaportV1_4: address(0),
openseaConduit: address(0),
Expand Down
2 changes: 2 additions & 0 deletions test/foundry-tests/UniversalRouter.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ contract UniversalRouterTest is Test {
RouterParameters memory params = RouterParameters({
permit2: address(0),
weth9: address(0),
steth: address(0),
wsteth: address(0),
seaportV1_5: address(0),
seaportV1_4: address(0),
openseaConduit: address(0),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
exports[`NFTX Gas Tests gas: buyAndRedeem w/ specific selection 1`] = `
Object {
"calldataByteLength": 964,
"gasUsed": 574870,
"gasUsed": 574873,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ Object {
exports[`Payments Gas Tests Individual Command Tests gas: SWEEP with ERC20 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 39235,
"gasUsed": 39247,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: SWEEP_WITH_FEE 1`] = `
Object {
"calldataByteLength": 516,
"gasUsed": 67925,
"gasUsed": 67949,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: TRANSFER with ERC20 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 38248,
"gasUsed": 38260,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: TRANSFER with ETH 1`] = `
Object {
"calldataByteLength": 356,
"gasUsed": 33821,
"gasUsed": 33833,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: UNWRAP_WETH 1`] = `
Object {
"calldataByteLength": 324,
"gasUsed": 46765,
"gasUsed": 46768,
}
`;

exports[`Payments Gas Tests Individual Command Tests gas: UNWRAP_WETH_WITH_FEE 1`] = `
Object {
"calldataByteLength": 644,
"gasUsed": 53127,
"gasUsed": 53154,
}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Seaport v1.4 Gas Tests ERC20 -> NFT gas: fulfillAdvancedOrder 1`] = `
Object {
"calldataByteLength": 2532,
"gasUsed": 237135,
"gasUsed": 237147,
}
`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`Seaport v1.5 Gas Tests ETH -> NFT gas: fulfillAdvancedOrder 1`] = `
Object {
"calldataByteLength": 2020,
"gasUsed": 152097,
"gasUsed": 152100,
}
`;

Expand Down
Loading
Loading