From 7f4dbf0ef821e24d358db2af1ae4cbe421ad1231 Mon Sep 17 00:00:00 2001 From: Anton Cheng Date: Fri, 3 Nov 2023 08:59:36 +0800 Subject: [PATCH] fix: don't pay fee first --- src/gelato/LyraSelfPayingForwarder.sol | 15 ++++++--------- src/gelato/LyraSponsoredForwarder.sol | 24 ++++++++---------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/src/gelato/LyraSelfPayingForwarder.sol b/src/gelato/LyraSelfPayingForwarder.sol index 64c6276..ab032ef 100644 --- a/src/gelato/LyraSelfPayingForwarder.sol +++ b/src/gelato/LyraSelfPayingForwarder.sol @@ -31,19 +31,15 @@ contract LyraSelfPayingForwarder is LyraForwarderBase, GelatoRelayContextERC2771 /** * @notice Deposit USDC to L2 * @dev Users never have to approve USDC to this contract, we use receiveWithAuthorization to save gas - * @param depositAmount Amount of USDC to deposit * @param l2Receiver Address of the receiver on L2 * @param minGasLimit Minimum gas limit for the L2 execution */ function depositUSDCNativeBridge( - uint256 depositAmount, uint256 maxERC20Fee, address l2Receiver, uint32 minGasLimit, ReceiveWithAuthData calldata authData ) external onlyGelatoRelayERC2771 { - _transferRelayFeeCapped(maxERC20Fee); - // step 1: receive USDC from user to this contract IERC3009(usdcLocal).receiveWithAuthorization( _getMsgSender(), @@ -57,7 +53,9 @@ contract LyraSelfPayingForwarder is LyraForwarderBase, GelatoRelayContextERC2771 authData.s ); - uint256 remaining = depositAmount - _getFee(); + _transferRelayFeeCapped(maxERC20Fee); + + uint256 remaining = authData.value - _getFee(); // step 3: call bridge to L2 IL1StandardBridge(standardBridge).bridgeERC20To(usdcLocal, usdcRemote, l2Receiver, remaining, minGasLimit, ""); @@ -67,14 +65,11 @@ contract LyraSelfPayingForwarder is LyraForwarderBase, GelatoRelayContextERC2771 * @notice Deposit USDC to L2 through other socket fast bridge. Gas is paid in USDC */ function depositUSDCSocketBridge( - uint256 depositAmount, uint256 maxERC20Fee, address l2Receiver, uint32 minGasLimit, ReceiveWithAuthData calldata authData ) external onlyGelatoRelayERC2771 { - _transferRelayFeeCapped(maxERC20Fee); - // step 1: receive USDC from user to this contract IERC3009(usdcLocal).receiveWithAuthorization( _getMsgSender(), @@ -88,8 +83,10 @@ contract LyraSelfPayingForwarder is LyraForwarderBase, GelatoRelayContextERC2771 authData.s ); + _transferRelayFeeCapped(maxERC20Fee); + // pay gelato fee - uint256 remaining = depositAmount - _getFee(); + uint256 remaining = authData.value - _getFee(); // pay socket protocol fee uint256 socketFee = ISocketVault(socketVault).getMinFees(socketConnector, minGasLimit); diff --git a/src/gelato/LyraSponsoredForwarder.sol b/src/gelato/LyraSponsoredForwarder.sol index ce17da6..0f070da 100644 --- a/src/gelato/LyraSponsoredForwarder.sol +++ b/src/gelato/LyraSponsoredForwarder.sol @@ -33,17 +33,13 @@ contract LyraSponsoredForwarder is LyraForwarderBase, ERC2771Context { * @notice Deposit USDC to L2 * @dev Users never have to approve USDC to this contract, we use receiveWithAuthorization to save gas * - * @param depositAmount Amount of USDC to deposit * @param isScwWallet True if user wants to deposit to default LightAccount on L2 * @param minGasLimit Minimum gas limit for the L2 execution * @param authData Data and signatures for receiveWithAuthorization */ - function depositUSDCNativeBridge( - uint256 depositAmount, - bool isScwWallet, - uint32 minGasLimit, - ReceiveWithAuthData calldata authData - ) external { + function depositUSDCNativeBridge(bool isScwWallet, uint32 minGasLimit, ReceiveWithAuthData calldata authData) + external + { address msgSender = _msgSender(); IERC3009(usdcLocal).receiveWithAuthorization( @@ -60,7 +56,7 @@ contract LyraSponsoredForwarder is LyraForwarderBase, ERC2771Context { // step 3: call bridge to L2 IL1StandardBridge(standardBridge).bridgeERC20To( - usdcLocal, usdcRemote, _getL2Receiver(msgSender, isScwWallet), depositAmount, minGasLimit, "" + usdcLocal, usdcRemote, _getL2Receiver(msgSender, isScwWallet), authData.value, minGasLimit, "" ); } @@ -68,17 +64,13 @@ contract LyraSponsoredForwarder is LyraForwarderBase, ERC2771Context { * @notice Deposit USDC to L2 through Socket fast bridge * @dev Users never have to approve USDC to this contract, we use receiveWithAuthorization to save gas * - * @param depositAmount Amount of USDC to deposit * @param isScwWallet True if user wants to deposit to default LightAccount on L2 * @param minGasLimit Minimum gas limit for the L2 execution * @param authData Data and signatures for receiveWithAuthorization */ - function depositUSDCSocketBridge( - uint256 depositAmount, - bool isScwWallet, - uint32 minGasLimit, - ReceiveWithAuthData calldata authData - ) external { + function depositUSDCSocketBridge(bool isScwWallet, uint32 minGasLimit, ReceiveWithAuthData calldata authData) + external + { address msgSender = _msgSender(); // step 1: receive USDC from user to this contract @@ -97,7 +89,7 @@ contract LyraSponsoredForwarder is LyraForwarderBase, ERC2771Context { uint256 feeInWei = ISocketVault(socketVault).getMinFees(socketConnector, minGasLimit); ISocketVault(socketVault).depositToAppChain{value: feeInWei}( - _getL2Receiver(msgSender, isScwWallet), depositAmount, minGasLimit, socketConnector + _getL2Receiver(msgSender, isScwWallet), authData.value, minGasLimit, socketConnector ); } }