contracts-opt,arb,ftm

This commit is contained in:
Richa-iitr 2022-06-15 20:54:05 +05:30
parent f3aaea677b
commit c61d69682d
12 changed files with 317 additions and 0 deletions

View File

@ -1,5 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
interface TokenInterface {
function approve(address, uint256) external;
@ -26,3 +27,7 @@ interface AccountInterface {
function disable(address) external;
function isAuth(address) external view returns (bool);
}
interface InstaConnectors {
function isConnectors(string[] calldata) external returns (bool, address[] memory);
}

View File

@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
contract Events {
event LogSwapAggregator(
string[] connectors,
string connectorName,
string eventName,
bytes eventParam
);
}

View File

@ -0,0 +1,44 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
import { InstaConnectors } from "../../common/interfaces.sol";
contract SwapHelpers {
/**
* @dev Instadapp Connectors Registry
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x67fCE99Dd6d8d659eea2a1ac1b8881c57eb6592B);
/**
*@dev Swap using the dex aggregators.
*@param _connectors name of the connectors in preference order.
*@param _datas data for the swap cast.
*/
function _swap(string[] memory _connectors, bytes[] memory _datas)
internal
returns (
bool success,
bytes memory returnData,
string memory connector
)
{
uint256 _length = _connectors.length;
require(_length > 0, "zero-length-not-allowed");
require(_datas.length == _length, "calldata-length-invalid");
(bool isOk, address[] memory connectors) = instaConnectors.isConnectors(
_connectors
);
require(isOk, "connector-names-invalid");
for (uint256 i = 0; i < _length; i++) {
(success, returnData) = connectors[i].delegatecall(_datas[i]);
if (success) {
connector = _connectors[i];
break;
}
}
}
}

View File

@ -0,0 +1,44 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/**
* @title Swap.
* @dev Swap integration for DEX Aggregators.
*/
// import files
import { SwapHelpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract Swap is SwapHelpers, Events {
/**
* @dev Swap ETH/ERC20_Token using dex aggregators.
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
* @param _datas Encoded function call data including function selector encoded with parameters.
*/
function swap(string[] memory _connectors, bytes[] memory _datas)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
(bool success, bytes memory returnData, string memory connector) = _swap(
_connectors,
_datas
);
require(success, "swap-Aggregator-failed");
(string memory eventName, bytes memory eventParam) = abi.decode(
returnData,
(string, bytes)
);
_eventName = "LogSwapAggregator(string[],string,string,bytes)";
_eventParam = abi.encode(_connectors, connector, eventName, eventParam);
}
}
contract ConnectV2SwapAggregatorArbitrum is Swap {
string public name = "Swap-Aggregator-v1";
}

View File

@ -1,5 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
interface TokenInterface {
function approve(address, uint256) external;
@ -34,3 +35,9 @@ interface AccountInterface {
function isAuth(address) external view returns (bool);
}
interface InstaConnectors {
function isConnectors(string[] calldata)
external
returns (bool, address[] memory);
}

View File

@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
contract Events {
event LogSwapAggregator(
string[] connectors,
string connectorName,
string eventName,
bytes eventParam
);
}

View File

@ -0,0 +1,44 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
import { InstaConnectors } from "../../common/interfaces.sol";
contract SwapHelpers {
/**
* @dev Instadapp Connectors Registry
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x819910794a030403F69247E1e5C0bBfF1593B968);
/**
*@dev Swap using the dex aggregators.
*@param _connectors name of the connectors in preference order.
*@param _datas data for the swap cast.
*/
function _swap(string[] memory _connectors, bytes[] memory _datas)
internal
returns (
bool success,
bytes memory returnData,
string memory connector
)
{
uint256 _length = _connectors.length;
require(_length > 0, "zero-length-not-allowed");
require(_datas.length == _length, "calldata-length-invalid");
(bool isOk, address[] memory connectors) = instaConnectors.isConnectors(
_connectors
);
require(isOk, "connector-names-invalid");
for (uint256 i = 0; i < _length; i++) {
(success, returnData) = connectors[i].delegatecall(_datas[i]);
if (success) {
connector = _connectors[i];
break;
}
}
}
}

View File

@ -0,0 +1,44 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/**
* @title Swap.
* @dev Swap integration for DEX Aggregators.
*/
// import files
import { SwapHelpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract Swap is SwapHelpers, Events {
/**
* @dev Swap ETH/ERC20_Token using dex aggregators.
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
* @param _datas Encoded function call data including function selector encoded with parameters.
*/
function swap(string[] memory _connectors, bytes[] memory _datas)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
(bool success, bytes memory returnData, string memory connector) = _swap(
_connectors,
_datas
);
require(success, "swap-Aggregator-failed");
(string memory eventName, bytes memory eventParam) = abi.decode(
returnData,
(string, bytes)
);
_eventName = "LogSwapAggregator(string[],string,string,bytes)";
_eventParam = abi.encode(_connectors, connector, eventName, eventParam);
}
}
contract ConnectV2SwapAggregatorFantom is Swap {
string public name = "Swap-Aggregator-v1";
}

View File

@ -1,5 +1,6 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
interface TokenInterface {
function approve(address, uint256) external;
@ -23,3 +24,7 @@ interface AccountInterface {
function disable(address) external;
function isAuth(address) external view returns (bool);
}
interface InstaConnectors {
function isConnectors(string[] calldata) external returns (bool, address[] memory);
}

View File

@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
contract Events {
event LogSwapAggregator(
string[] connectors,
string connectorName,
string eventName,
bytes eventParam
);
}

View File

@ -0,0 +1,44 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
import { InstaConnectors } from "../../common/interfaces.sol";
contract SwapHelpers {
/**
* @dev Instadapp Connectors Registry
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
/**
*@dev Swap using the dex aggregators.
*@param _connectors name of the connectors in preference order.
*@param _datas data for the swap cast.
*/
function _swap(string[] memory _connectors, bytes[] memory _datas)
internal
returns (
bool success,
bytes memory returnData,
string memory connector
)
{
uint256 _length = _connectors.length;
require(_length > 0, "zero-length-not-allowed");
require(_datas.length == _length, "calldata-length-invalid");
(bool isOk, address[] memory connectors) = instaConnectors.isConnectors(
_connectors
);
require(isOk, "connector-names-invalid");
for (uint256 i = 0; i < _length; i++) {
(success, returnData) = connectors[i].delegatecall(_datas[i]);
if (success) {
connector = _connectors[i];
break;
}
}
}
}

View File

@ -0,0 +1,44 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
/**
* @title Swap.
* @dev Swap integration for DEX Aggregators.
*/
// import files
import { SwapHelpers } from "./helpers.sol";
import { Events } from "./events.sol";
abstract contract Swap is SwapHelpers, Events {
/**
* @dev Swap ETH/ERC20_Token using dex aggregators.
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
* @param _datas Encoded function call data including function selector encoded with parameters.
*/
function swap(string[] memory _connectors, bytes[] memory _datas)
external
payable
returns (string memory _eventName, bytes memory _eventParam)
{
(bool success, bytes memory returnData, string memory connector) = _swap(
_connectors,
_datas
);
require(success, "swap-Aggregator-failed");
(string memory eventName, bytes memory eventParam) = abi.decode(
returnData,
(string, bytes)
);
_eventName = "LogSwapAggregator(string[],string,string,bytes)";
_eventParam = abi.encode(_connectors, connector, eventName, eventParam);
}
}
contract ConnectV2SwapAggregatorOptimism is Swap {
string public name = "Swap-Aggregator-v1";
}