Init Connext connector

This commit is contained in:
just-a-node 2023-02-21 17:05:04 -07:00
parent 392a905f4d
commit 896a7254b9
No known key found for this signature in database
GPG Key ID: 0DA737526BA15F3B
4 changed files with 184 additions and 0 deletions

View File

@ -0,0 +1,15 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
contract Events {
event LogXCall(
uint32 destination,
address to,
address asset,
address delegate,
uint256 amount,
uint256 slippage,
bytes callData,
uint256 getId
);
}

View File

@ -0,0 +1,79 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
import { TokenInterface } from "../../common/interfaces.sol";
import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol";
import { IConnext } from "./interface.sol";
import { IInstaReceiver } from "./interface.sol";
contract Helpers is DSMath, Basic {
/**
* @dev Connext Diamond Address
*/
address internal constant connextAddr = 0x8f7492DE823025b4CfaAB1D34c58963F2af5DEDA;
IConnext internal constant connext = IConnext(connextAddr);
/**
* @dev InstaReceiver Address
*/
address internal constant instaReceiverAddr = 0x0000000000000000000000000000000000000000; // TODO: Add InstaReceiver address
IInstaReceiver internal constant instaReceiver = IInstaReceiver(instaReceiverAddr);
/**
* @param destination The destination domain ID.
* @param asset he address of token to be bridged.(For USDC: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174)
* @param delegate The address to recieve the token on destination chain.
* @param amount The total amount sent by user (Includes bonder fee, destination chain Tx cost).
* @param slippage The fee to be recieved by bonder at destination chain.
* @param relayerFee Relayer fee paid in origin native asset.
* @param callData minimum amount of token out for swap on source chain.
*/
struct XCallParams {
uint32 destination;
address to;
address asset;
address delegate;
uint256 amount;
uint256 slippage;
uint256 relayerFee;
bytes callData;
}
function _xcall(XCallParams memory params) internal {
TokenInterface tokenContract = TokenInterface(params.asset);
bool isNative = params.asset == ethAddr;
uint256 nativeTokenAmt;
if (isNative) {
// xcall does not take native asset, must wrap
convertEthToWeth(true, tokenContract, params.amount);
params.amount = params.amount == uint256(-1)
? address(this).balance
: params.amount;
nativeTokenAmt = params.amount;
} else {
params.amount = params.amount == uint256(-1)
? tokenContract.balanceOf(address(this))
: params.amount;
nativeTokenAmt = 0;
}
if (!isNative && params.amount > 0) {
approve(tokenContract, connextAddr, params.amount);
}
connext.xcall{ value: params.relayerFee + nativeTokenAmt }(
params.destination,
params.to,
params.asset,
params.delegate,
params.amount,
params.slippage,
params.callData
);
}
}

View File

@ -0,0 +1,30 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
interface IConnext {
function xcall(
uint32 _destination,
address _to,
address _asset,
address _delegate,
uint256 _amount,
uint256 _slippage,
bytes calldata _callData
) external payable returns (bytes32);
}
interface IInstaReceiver {
function withdraw(
address _asset,
uint256 _amount
) external returns (bytes memory);
function xReceive(
bytes32 _transferId,
uint256 _amount,
address _asset,
address _originSender,
uint32 _origin,
bytes memory _callData
) external returns (bytes memory);
}

View File

@ -0,0 +1,60 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/**
* @title Connext.
* @dev Cross chain bridge.
*/
import { TokenInterface, MemoryInterface } from "../../common/interfaces.sol";
import { Stores } from "../../common/stores.sol";
import "./interface.sol";
import "./helpers.sol";
import "./events.sol";
abstract contract ConnextResolver is Helpers {
/**
* @dev Call xcall on Connext.
* @notice Call xcall on Connext.
* @param params XCallParams struct.
* @param getId ID to retrieve amount from last spell.
*/
function xcall(XCallParams memory params, uint256 getId)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
params.amount = getUint(getId, params.amount);
TokenInterface tokenContract = TokenInterface(params.asset);
_xcall(params);
_eventName = "LogXCall(uint32,address,address,address,uint256,uint256,bytes,uint256)";
_eventParam = abi.encode(
params.destination,
params.to,
params.asset,
params.delegate,
params.amount,
params.slippage,
params.callData,
getId
);
}
/**
* @dev Delegatecall'ed by DSA.
* @notice Withdraw from the receiver contract to the calling DSA.
* @param asset Address of the asset to withdraw.
* @param getId ID to retrieve amount from last spell.
*/
function withdraw(address asset, uint256 amount, uint256 getId) external {
uint256 _amt = getUint(getId, amount);
instaReceiver.withdraw(asset, _amt);
}
}
contract ConnectV2ConnextOptimism is ConnextResolver {
string public constant name = "Connext-v1.0";
}