From ebc63def2e4cbf5b62c667691f01324766afd34c Mon Sep 17 00:00:00 2001 From: aniket965 Date: Fri, 19 Jul 2024 23:09:51 +0530 Subject: [PATCH 1/2] feat: add permit functions --- contracts/bridge/Controller.sol | 33 ++++++++++++++++++++++++++++++++- contracts/bridge/Vault.sol | 32 +++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/contracts/bridge/Controller.sol b/contracts/bridge/Controller.sol index 20187688..9f04918c 100644 --- a/contracts/bridge/Controller.sol +++ b/contracts/bridge/Controller.sol @@ -27,7 +27,7 @@ contract Controller is Base { address connector_, bytes calldata extraData_, bytes calldata options_ - ) external payable nonReentrant { + ) public payable nonReentrant { ( TransferInfo memory transferInfo, bytes memory postHookData @@ -49,6 +49,37 @@ contract Controller is Base { ); } + /** + * @notice Bridges tokens between chains with permit. + * @dev This function allows bridging tokens between different chains. + * @param receiver_ The address to receive the bridged tokens. + * @param amount_ The amount of tokens to bridge. + * @param msgGasLimit_ The gas limit for the execution of the bridging process. + * @param connector_ The address of the connector contract responsible for the bridge. + * @param extraData_ The extra data passed to hook functions. + * @param options_ Additional options for the bridging process. + * @param deadline_ The deadline for the permit signature. + * @param v_ The recovery id of the permit signature. + * @param r_ The r value of the permit signature. + * @param s_ The s value of the permit signature. + */ + function bridgeWithPermit( + address receiver_, + uint256 amount_, + uint256 msgGasLimit_, + address connector_, + bytes calldata execPayload_, + bytes calldata options_, + uint256 deadline_, + uint8 v_, + bytes32 r_, + bytes32 s_ + ) external payable { + ERC20(token).permit(msg.sender, address(this), amount_, deadline_, v_, r_, s_); + bridge(receiver_, amount_, msgGasLimit_, connector_, execPayload_, options_); + } + + /** * @notice Receives inbound tokens from another chain. * @dev This function is used to receive tokens from another chain. diff --git a/contracts/bridge/Vault.sol b/contracts/bridge/Vault.sol index 696c18cd..b401f513 100644 --- a/contracts/bridge/Vault.sol +++ b/contracts/bridge/Vault.sol @@ -39,7 +39,7 @@ contract Vault is Base { address connector_, bytes calldata extraData_, bytes calldata options_ - ) external payable nonReentrant { + ) public payable nonReentrant { ( TransferInfo memory transferInfo, bytes memory postHookData @@ -59,6 +59,36 @@ contract Vault is Base { ); } + /** + * @notice Bridges tokens between chains with permit. + * @dev This function allows bridging tokens between different chains. + * @param receiver_ The address to receive the bridged tokens. + * @param amount_ The amount of tokens to bridge. + * @param msgGasLimit_ The gas limit for the execution of the bridging process. + * @param connector_ The address of the connector contract responsible for the bridge. + * @param extraData_ The extra data passed to hook functions. + * @param options_ Additional options for the bridging process. + * @param deadline_ The deadline for the permit signature. + * @param v_ The recovery id of the permit signature. + * @param r_ The r value of the permit signature. + * @param s_ The s value of the permit signature. + */ + function bridgeWithPermit( + address receiver_, + uint256 amount_, + uint256 msgGasLimit_, + address connector_, + bytes calldata execPayload_, + bytes calldata options_, + uint256 deadline_, + uint8 v_, + bytes32 r_, + bytes32 s_ + ) external payable { + ERC20(token).permit(msg.sender, address(this), amount_, deadline_, v_, r_, s_); + bridge(receiver_, amount_, msgGasLimit_, connector_, execPayload_, options_); + } + /** * @notice Receives inbound tokens from another chain. * @dev This function is used to receive tokens from another chain. From f2fd99b8b9d68995b539fcfb9277faab73fd68a3 Mon Sep 17 00:00:00 2001 From: aniket965 Date: Wed, 31 Jul 2024 13:23:13 +0530 Subject: [PATCH 2/2] fix: add try catch on permit --- contracts/bridge/Controller.sol | 60 +++++++++------------------------ contracts/bridge/Vault.sol | 8 +++-- 2 files changed, 22 insertions(+), 46 deletions(-) diff --git a/contracts/bridge/Controller.sol b/contracts/bridge/Controller.sol index 9f04918c..c9307c9b 100644 --- a/contracts/bridge/Controller.sol +++ b/contracts/bridge/Controller.sol @@ -28,25 +28,14 @@ contract Controller is Base { bytes calldata extraData_, bytes calldata options_ ) public payable nonReentrant { - ( - TransferInfo memory transferInfo, - bytes memory postHookData - ) = _beforeBridge( - connector_, - TransferInfo(receiver_, amount_, extraData_) - ); + (TransferInfo memory transferInfo, bytes memory postHookData) = + _beforeBridge(connector_, TransferInfo(receiver_, amount_, extraData_)); // to maintain socket dl specific accounting for super token // re check this logic for mint and mint use cases and if other minter involved totalMinted -= transferInfo.amount; _burn(msg.sender, transferInfo.amount); - _afterBridge( - msgGasLimit_, - connector_, - options_, - postHookData, - transferInfo - ); + _afterBridge(msgGasLimit_, connector_, options_, postHookData, transferInfo); } /** @@ -75,10 +64,13 @@ contract Controller is Base { bytes32 r_, bytes32 s_ ) external payable { - ERC20(token).permit(msg.sender, address(this), amount_, deadline_, v_, r_, s_); - bridge(receiver_, amount_, msgGasLimit_, connector_, execPayload_, options_); - } + try ERC20(token).permit(msg.sender, address(this), amount_, deadline_, v_, r_, s_) { + bridge(receiver_, amount_, msgGasLimit_, connector_, execPayload_, options_); + } catch { + bridge(receiver_, amount_, msgGasLimit_, connector_, extraData_, options_); + } + } /** * @notice Receives inbound tokens from another chain. @@ -86,28 +78,14 @@ contract Controller is Base { * @param siblingChainSlug_ The identifier of the sibling chain. * @param payload_ The payload containing the inbound tokens. */ - function receiveInbound( - uint32 siblingChainSlug_, - bytes memory payload_ - ) external payable override nonReentrant { - ( - address receiver, - uint256 lockAmount, - bytes32 messageId, - bytes memory extraData - ) = abi.decode(payload_, (address, uint256, bytes32, bytes)); + function receiveInbound(uint32 siblingChainSlug_, bytes memory payload_) external payable override nonReentrant { + (address receiver, uint256 lockAmount, bytes32 messageId, bytes memory extraData) = + abi.decode(payload_, (address, uint256, bytes32, bytes)); // convert to shares - TransferInfo memory transferInfo = TransferInfo( - receiver, - lockAmount, - extraData - ); + TransferInfo memory transferInfo = TransferInfo(receiver, lockAmount, extraData); bytes memory postHookData; - (postHookData, transferInfo) = _beforeMint( - siblingChainSlug_, - transferInfo - ); + (postHookData, transferInfo) = _beforeMint(siblingChainSlug_, transferInfo); _mint(transferInfo.receiver, transferInfo.amount); totalMinted += transferInfo.amount; @@ -121,14 +99,8 @@ contract Controller is Base { * @param connector_ The address of the connector contract responsible for the failed transaction. * @param messageId_ The unique identifier of the failed transaction. */ - function retry( - address connector_, - bytes32 messageId_ - ) external nonReentrant { - ( - bytes memory postHookData, - TransferInfo memory transferInfo - ) = _beforeRetry(connector_, messageId_); + function retry(address connector_, bytes32 messageId_) external nonReentrant { + (bytes memory postHookData, TransferInfo memory transferInfo) = _beforeRetry(connector_, messageId_); _mint(transferInfo.receiver, transferInfo.amount); totalMinted += transferInfo.amount; diff --git a/contracts/bridge/Vault.sol b/contracts/bridge/Vault.sol index b401f513..40b1b833 100644 --- a/contracts/bridge/Vault.sol +++ b/contracts/bridge/Vault.sol @@ -85,8 +85,12 @@ contract Vault is Base { bytes32 r_, bytes32 s_ ) external payable { - ERC20(token).permit(msg.sender, address(this), amount_, deadline_, v_, r_, s_); - bridge(receiver_, amount_, msgGasLimit_, connector_, execPayload_, options_); + + try ERC20(token).permit(msg.sender, address(this), amount_, deadline_, v_, r_, s_) { + bridge(receiver_, amount_, msgGasLimit_, connector_, execPayload_, options_); + } catch { + bridge(receiver_, amount_, msgGasLimit_, connector_, extraData_, options_); + } } /**