Skip to content

Commit

Permalink
Tasks: Implement socket bridger (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo committed Sep 12, 2024
1 parent 55a50af commit ac81ddf
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 11 deletions.
91 changes: 91 additions & 0 deletions packages/tasks/contracts/bridge/SocketBridger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity ^0.8.0;

import '@mimic-fi/v3-connectors/contracts/interfaces/socket/ISocketConnector.sol';

import './BaseBridgeTask.sol';
import '../interfaces/bridge/ISocketBridger.sol';

/**
* @title Socket bridger
* @dev Task that extends the base bridge task to use Socket
*/
contract SocketBridger is ISocketBridger, BaseBridgeTask {
using FixedPoint for uint256;

// Execution type for relayers
bytes32 public constant override EXECUTION_TYPE = keccak256('SOCKET_BRIDGER');

/**
* @dev Socket bridge config. Only used in the initializer.
*/
struct SocketBridgeConfig {
BaseBridgeConfig baseBridgeConfig;
}

/**
* @dev Initializes the Socket bridger
* @param config Socket bridge config
*/
function initialize(SocketBridgeConfig memory config) external virtual initializer {
__SocketBridger_init(config);
}

/**
* @dev Initializes the Socket bridger. It does call upper contracts initializers.
* @param config Socket bridge config
*/
function __SocketBridger_init(SocketBridgeConfig memory config) internal onlyInitializing {
__BaseBridgeTask_init(config.baseBridgeConfig);
__SocketBridger_init_unchained(config);
}

/**
* @dev Initializes the Socket bridger. It does not call upper contracts initializers.
* @param config Socket bridge config
*/
function __SocketBridger_init_unchained(SocketBridgeConfig memory config) internal onlyInitializing {
// solhint-disable-previous-line no-empty-blocks
}

/**
* @dev Execute Socket bridger
*/
function call(address token, uint256 amount, bytes memory data) external override authP(authParams(token, amount)) {
if (amount == 0) amount = getTaskAmount(token);
_beforeSocketBridger(token, amount);

bytes memory connectorData = abi.encodeWithSelector(ISocketConnector.execute.selector, token, amount, data);
ISmartVault(smartVault).execute(connector, connectorData);
_afterSocketBridger(token, amount);
}

/**
* @dev Before Socket bridger hook
*/
function _beforeSocketBridger(address token, uint256 amount) internal virtual {
// Socket does not support specifying slippage nor fee
_beforeBaseBridgeTask(token, amount, 0, 0);
}

/**
* @dev After Socket bridger task hook
*/
function _afterSocketBridger(address token, uint256 amount) internal virtual {
// Socket does not support specifying slippage nor fee
_afterBaseBridgeTask(token, amount, 0, 0);
}
}
27 changes: 27 additions & 0 deletions packages/tasks/contracts/interfaces/bridge/ISocketBridger.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity >=0.8.0;

import './IBaseBridgeTask.sol';

/**
* @dev Socket bridger task interface
*/
interface ISocketBridger is IBaseBridgeTask {
/**
* @dev Execute Socket bridger task
*/
function call(address token, uint256 amount, bytes memory data) external;
}
2 changes: 1 addition & 1 deletion packages/tasks/contracts/swap/OneInchV5Swapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ contract OneInchV5Swapper is IOneInchV5Swapper, BaseSwapTask {
*/
function call(address tokenIn, uint256 amountIn, uint256 slippage, bytes memory data)
external
override
virtual
override
authP(authParams(tokenIn, amountIn, slippage))
{
if (amountIn == 0) amountIn = getTaskAmount(tokenIn);
Expand Down
23 changes: 23 additions & 0 deletions packages/tasks/contracts/test/bridge/SocketConnectorMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

pragma solidity ^0.8.0;

contract SocketConnectorMock {
event LogExecute(address token, uint256 amount, bytes data);

function execute(address token, uint256 amount, bytes memory data) external {
emit LogExecute(token, amount, data);
}
}
2 changes: 1 addition & 1 deletion packages/tasks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"dependencies": {
"@mimic-fi/v3-authorizer": "0.1.1",
"@mimic-fi/v3-connectors": "0.2.3",
"@mimic-fi/v3-connectors": "0.2.4",
"@mimic-fi/v3-helpers": "0.1.9",
"@mimic-fi/v3-price-oracle": "0.1.0",
"@mimic-fi/v3-smart-vault": "0.1.0",
Expand Down
Loading

0 comments on commit ac81ddf

Please sign in to comment.