Skip to content

Commit

Permalink
πŸ‘·πŸ» πŸš€ βœ… Add univ3 router to paymaster
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyrharper committed Jul 9, 2024
1 parent d0c6b67 commit fe85584
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 11 deletions.
9 changes: 6 additions & 3 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ contract Setup is Script {
address usdc,
address sUSDProxy,
address spotMarketProxy,
uint128 sUSDCId
uint128 sUSDCId,
address uniV3Router
) public returns (address) {
MarginPaymaster marginPaymaster = new MarginPaymaster(
entryPoint,
Expand All @@ -28,7 +29,8 @@ contract Setup is Script {
usdc,
sUSDProxy,
spotMarketProxy,
sUSDCId
sUSDCId,
uniV3Router
);
return address(marginPaymaster);
}
Expand All @@ -49,7 +51,8 @@ contract DeployBase is Setup, BaseParameters {
USDC,
USD_PROXY_ANDROMEDA,
SPOT_MARKET_PROXY_ANDROMEDA,
SUSDC_SPOT_MARKET_ID
SUSDC_SPOT_MARKET_ID,
UNISWAP_ROUTER_02
);

vm.stopBroadcast();
Expand Down
6 changes: 5 additions & 1 deletion src/MarginPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.20;

import {IPaymaster, UserOperation} from "lib/account-abstraction/contracts/interfaces/IPaymaster.sol";
import {IPerpsMarketProxy} from "src/interfaces/external/IPerpsMarketProxy.sol";
import {IV3SwapRouter} from "src/interfaces/external/IV3SwapRouter.sol";
import {IEngine} from "src/interfaces/IEngine.sol";
import {Account} from "src/Account.sol";
import {Zap} from "lib/zap/src/Zap.sol";
Expand All @@ -16,6 +17,7 @@ contract MarginPaymaster is IPaymaster, Zap {
address public immutable entryPoint;
IEngine public immutable smartMarginV3;
IPerpsMarketProxy public immutable perpsMarketSNXV3;
IV3SwapRouter public immutable uniV3Router;
uint128 public constant sUSDId = 0;

error InvalidEntryPoint();
Expand All @@ -27,11 +29,13 @@ contract MarginPaymaster is IPaymaster, Zap {
address _usdc,
address _sUSDProxy,
address _spotMarketProxy,
uint128 _sUSDCId
uint128 _sUSDCId,
address _uniV3Router
) Zap(_usdc, _sUSDProxy, _spotMarketProxy, _sUSDCId) {
entryPoint = _entryPoint;
smartMarginV3 = IEngine(_smartMarginV3);
perpsMarketSNXV3 = IPerpsMarketProxy(_perpsMarketSNXV3);
uniV3Router = IV3SwapRouter(_uniV3Router);
}

function validatePaymasterUserOp(
Expand Down
67 changes: 67 additions & 0 deletions src/interfaces/external/IV3SwapRouter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity 0.8.20;


/// @title Router token swapping functionality
/// @notice Functions for swapping tokens via Uniswap V3
interface IV3SwapRouter {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}

/// @notice Swaps `amountIn` of one token for as much as possible of another token
/// @dev Setting `amountIn` to 0 will cause the contract to look up its own balance,
/// and swap the entire amount, enabling contracts to send tokens before calling this function.
/// @param params The parameters necessary for the swap, encoded as `ExactInputSingleParams` in calldata
/// @return amountOut The amount of the received token
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);

struct ExactInputParams {
bytes path;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
}

/// @notice Swaps `amountIn` of one token for as much as possible of another along the specified path
/// @dev Setting `amountIn` to 0 will cause the contract to look up its own balance,
/// and swap the entire amount, enabling contracts to send tokens before calling this function.
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactInputParams` in calldata
/// @return amountOut The amount of the received token
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);

struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}

/// @notice Swaps as little as possible of one token for `amountOut` of another token
/// that may remain in the router after the swap.
/// @param params The parameters necessary for the swap, encoded as `ExactOutputSingleParams` in calldata
/// @return amountIn The amount of the input token
function exactOutputSingle(ExactOutputSingleParams calldata params) external payable returns (uint256 amountIn);

struct ExactOutputParams {
bytes path;
address recipient;
uint256 amountOut;
uint256 amountInMaximum;
}

/// @notice Swaps as little as possible of one token for `amountOut` of another along the specified path (reversed)
/// that may remain in the router after the swap.
/// @param params The parameters necessary for the multi-hop swap, encoded as `ExactOutputParams` in calldata
/// @return amountIn The amount of the input token
function exactOutput(ExactOutputParams calldata params) external payable returns (uint256 amountIn);
}
24 changes: 17 additions & 7 deletions test/utils/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {IEntryPoint} from "lib/account-abstraction/contracts/interfaces/IEntryPo

import {MarginPaymaster, OptimismGoerliParameters, OptimismParameters, BaseParameters, Setup} from "script/Deploy.s.sol";
import {IPerpsMarketProxy} from "src/interfaces/external/IPerpsMarketProxy.sol";
import {IV3SwapRouter} from "src/interfaces/external/IV3SwapRouter.sol";
import {AccountFactory, Account} from "src/Account.sol";
import {IUSDC} from "test/utils/interfaces/IUSDC.sol";
import {Test} from "lib/forge-std/src/Test.sol";
Expand Down Expand Up @@ -42,6 +43,7 @@ contract Bootstrap is Test {
address internal usdcAddress;
IUSDC internal usdc;
uint128 internal sUSDCId;
IV3SwapRouter internal uniV3Router;

function initializeLocal() internal {
entryPoint = new EntryPoint();
Expand All @@ -53,7 +55,8 @@ contract Bootstrap is Test {
address(0),
address(0),
address(0),
0
0,
address(0)
);
marginPaymaster = MarginPaymaster(marginPaymasterAddress);
}
Expand All @@ -69,7 +72,8 @@ contract Bootstrap is Test {
address _smartMarginV3Address,
address _canonicalEntryPointAddress,
address _usdc,
uint128 _sUSDCId
uint128 _sUSDCId,
address _uniswapRouter
) = bootstrap.init();
perpsMarketProxyAddress = _perpsMarketProxyAddress;
perpsMarketProxy = IPerpsMarketProxy(perpsMarketProxyAddress);
Expand All @@ -82,6 +86,7 @@ contract Bootstrap is Test {
usdcAddress = _usdc;
usdc = IUSDC(usdcAddress);
sUSDCId = _sUSDCId;
uniV3Router = IV3SwapRouter(_uniswapRouter);

marginPaymasterAddress = _marginPaymasterAddress;
marginPaymaster = MarginPaymaster(marginPaymasterAddress);
Expand All @@ -108,7 +113,8 @@ contract BootstrapLocal is Setup {
address usdc,
address sUSDProxy,
address spotMarketProxy,
uint128 sUSDCId
uint128 sUSDCId,
address uniswapRouter
) public returns (address) {
address marginPaymasterAddress = Setup.deploySystem(
entryPoint,
Expand All @@ -117,7 +123,8 @@ contract BootstrapLocal is Setup {
usdc,
sUSDProxy,
spotMarketProxy,
sUSDCId
sUSDCId,
uniswapRouter
);

return marginPaymasterAddress;
Expand All @@ -136,7 +143,8 @@ contract BootstrapBase is Setup, BaseParameters {
address,
address,
address,
uint128
uint128,
address
)
{
address marginPaymasterAddress = Setup.deploySystem(
Expand All @@ -146,7 +154,8 @@ contract BootstrapBase is Setup, BaseParameters {
USDC,
USD_PROXY_ANDROMEDA,
SPOT_MARKET_PROXY_ANDROMEDA,
SUSDC_SPOT_MARKET_ID
SUSDC_SPOT_MARKET_ID,
UNISWAP_ROUTER_02
);

return (
Expand All @@ -158,7 +167,8 @@ contract BootstrapBase is Setup, BaseParameters {
SMART_MARGIN_V3,
CANONICAL_ENTRY_POINT,
USDC,
SUSDC_SPOT_MARKET_ID
SUSDC_SPOT_MARKET_ID,
UNISWAP_ROUTER_02
);
}
}
Expand Down

0 comments on commit fe85584

Please sign in to comment.