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

Connext Adapter #27

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
branch = v4.9.0
[submodule "lib/interfaces"]
path = lib/interfaces
url = https://github.com/connext/interfaces
1 change: 1 addition & 0 deletions lib/interfaces
Submodule interfaces added at 2e0287
3 changes: 2 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ axelar-gmp-sdk-solidity/=lib/axelar-gmp-sdk-solidity/
ds-test/=lib/forge-std/lib/ds-test/src/
forge-gas-snapshot/=lib/forge-gas-snapshot/src/
forge-std/=lib/forge-std/src/
openzeppelin-contracts/=lib/openzeppelin-contracts/
openzeppelin-contracts/=lib/openzeppelin-contracts/
connext-interfaces/=lib/interfaces/
217 changes: 217 additions & 0 deletions src/adapters/ConnextAdapter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.10;

import "../interfaces/ISushiXSwapV2Adapter.sol";
import "../interfaces/IRouteProcessor.sol";
import "../interfaces/IWETH.sol";

import {IXReceiver} from "connext-interfaces/core/IXReceiver.sol";
import {IConnext} from "connext-interfaces/core/IConnext.sol";
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";



contract ConnextAdapter is ISushiXSwapV2Adapter, IXReceiver {
using SafeERC20 for IERC20;

IConnext public immutable connext;
IRouteProcessor public immutable rp;
IWETH public immutable weth;

address constant NATIVE_ADDRESS =
0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;

struct ConnextBridgeParams {
uint32 destinationDomain; // connext dst chain id
address target; // destination address for _execute call
address to; // address for fallback transfers on _execute call
address token; // token getting bridged
uint256 amount; // amount to bridge
uint256 slippage; // max amount of slippage willing to take in BPS (e.g. 30 = 0.3%)
}

error RpSentNativeIn();
error NotConnext();
error NoGasReceived();

constructor(
address _connext,
address _rp,
address _weth
) {
connext = IConnext(_connext);
rp = IRouteProcessor(_rp);
weth = IWETH(_weth);
}

/// @inheritdoc ISushiXSwapV2Adapter
function swap(
uint256 _amountBridged,
bytes calldata _swapData,
address _token,
bytes calldata _payloadData
) external payable override {
IRouteProcessor.RouteProcessorData memory rpd = abi.decode(
_swapData,
(IRouteProcessor.RouteProcessorData)
);

// send tokens to RP
IERC20(rpd.tokenIn).safeTransfer(address(rp), _amountBridged);

rp.processRoute(
rpd.tokenIn,
_amountBridged,
rpd.tokenOut,
rpd.amountOutMin,
rpd.to,
rpd.route
);

// tokens should be sent via rp
if (_payloadData.length > 0) {
PayloadData memory pd = abi.decode(_payloadData, (PayloadData));
try
IPayloadExecutor(pd.target).onPayloadReceive{gas: pd.gasLimit}(
pd.targetData
)
{} catch (bytes memory) {
revert();
}
}
}

/// @inheritdoc ISushiXSwapV2Adapter
function executePayload(
uint256 _amountBridged,
bytes calldata _payloadData,
address _token
) external payable override {
PayloadData memory pd = abi.decode(_payloadData, (PayloadData));
IERC20(_token).safeTransfer(pd.target, _amountBridged);
IPayloadExecutor(pd.target).onPayloadReceive{gas: pd.gasLimit}(
pd.targetData
);
}

// todo: getFee - think there is a way to fetch this on-chain


/// @inheritdoc ISushiXSwapV2Adapter
function adapterBridge(
bytes calldata _adapterData,
address _refundAddress,
bytes calldata _swapData,
bytes calldata _payloadData
) external payable override {
ConnextBridgeParams memory params = abi.decode(
_adapterData,
(ConnextBridgeParams)
);

if (params.token == NATIVE_ADDRESS) {
// RP should not send native in, since we won't know the exact amount to bridge
if (params.amount == 0) revert RpSentNativeIn();

weth.deposit{value: params.amount}();
params.token = address(weth);
}

if (params.amount == 0)
params.amount = IERC20(params.token).balanceOf(address(this));

IERC20(params.token).forceApprove(
address(connext),
params.amount
);

// build payload from params.to, _swapData, and _payloadData
bytes memory payload = abi.encode(params.to, _swapData, _payloadData);

// check if gas was received, since it doesn't throw on xcall
if (address(this).balance == 0)
revert NoGasReceived();

connext.xcall{value: address(this).balance} (
params.destinationDomain,
params.target,
params.token,
_refundAddress,
params.amount,
params.slippage,
payload
);
}

/// @notice receiver function on dst chain
/// @param _transferId id of the xchain transaction
/// @param _amount amount of tokeks that were bridged
/// @param _asset asset that was bridged
/// @param _originSender address of the sender on the origin chain
/// @param _origin chain id of the origin chain
/// @param _callData data received from source chain
function xReceive(
bytes32 _transferId,
uint256 _amount,
address _asset,
address _originSender,
uint32 _origin,
bytes memory _callData
) external override returns (bytes memory) {
uint256 gasLeft = gasleft();
if (msg.sender != address(connext))
revert NotConnext();

(address to, bytes memory _swapData, bytes memory _payloadData) = abi
.decode(_callData, (address, bytes, bytes));

uint256 reserveGas = 100000;

if (gasLeft < reserveGas) {
IERC20(_asset).safeTransfer(to, _amount);

/// @dev transfer any native token
if (address(this).balance > 0)
to.call{value: (address(this).balance)}("");

return bytes("");
}

// 100000 -> exit gas
uint256 limit = gasLeft - reserveGas;

if (_swapData.length > 0) {
try
ISushiXSwapV2Adapter(address(this)).swap{gas: limit}(
_amount,
_swapData,
_asset,
_payloadData
)
{} catch (bytes memory) {}
} else if (_payloadData.length > 0) {
try
ISushiXSwapV2Adapter(address(this)).executePayload{gas: limit}(
_amount,
_payloadData,
_asset
)
{} catch (bytes memory) {}
}

if (IERC20(_asset).balanceOf(address(this)) > 0)
IERC20(_asset).safeTransfer(to, IERC20(_asset).balanceOf(address(this)));

/// @dev transfer any native token received as dust to the to address
if (address(this).balance > 0)
to.call{value: (address(this).balance)}("");
}

/// @inheritdoc ISushiXSwapV2Adapter
function sendMessage(bytes calldata _adapterData) external override {
(_adapterData);
revert();
}

receive() external payable {}
}
4 changes: 2 additions & 2 deletions test/AxelarAdapterTests/AxelarAdapterBridgeTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ contract AxelarAdapterBridgeTest is BaseTest {
assertEq(
address(axelarAdapter).balance,
0,
"axelarAdapter should have 0 usdc"
"axelarAdapter should have 0 native"
);
assertEq(user.balance, 0, "user should have 0 usdc");
assertEq(user.balance, 0, "user should have 0 native");
}

function test_RevertWhen_BridgeUnsupportedERC20() public {
Expand Down
Loading