dsa-connectors/contracts/polygon/connectors/swap/helpers.sol

45 lines
1.1 KiB
Solidity
Raw Permalink Normal View History

2022-06-04 03:43:19 +00:00
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma abicoder v2;
import { InstaConnectors } from "../../common/interfaces.sol";
2022-06-08 13:50:24 +00:00
contract SwapHelpers {
2022-06-04 03:43:19 +00:00
/**
* @dev Instadapp Connectors Registry
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x2A00684bFAb9717C21271E0751BCcb7d2D763c88);
2022-06-04 03:50:36 +00:00
/**
2022-06-04 04:39:03 +00:00
*@dev Swap using the dex aggregators.
2022-06-04 03:50:36 +00:00
*@param _connectors name of the connectors in preference order.
2022-06-15 08:37:35 +00:00
*@param _datas data for the swap cast.
2022-06-04 03:50:36 +00:00
*/
2022-06-15 08:37:35 +00:00
function _swap(string[] memory _connectors, bytes[] memory _datas)
2022-06-04 03:43:19 +00:00
internal
2022-06-14 22:08:28 +00:00
returns (
bool success,
bytes memory returnData,
string memory connector
)
2022-06-04 03:43:19 +00:00
{
2022-06-08 13:50:24 +00:00
uint256 _length = _connectors.length;
require(_length > 0, "zero-length-not-allowed");
2022-06-15 08:37:35 +00:00
require(_datas.length == _length, "calldata-length-invalid");
2022-06-04 12:24:25 +00:00
2022-06-15 09:14:49 +00:00
(bool isOk, address[] memory connectors) = instaConnectors.isConnectors(
_connectors
);
require(isOk, "connector-names-invalid");
2022-06-08 13:50:24 +00:00
for (uint256 i = 0; i < _length; i++) {
2022-06-15 09:14:49 +00:00
(success, returnData) = connectors[i].delegatecall(_datas[i]);
2022-06-14 22:08:28 +00:00
if (success) {
connector = _connectors[i];
break;
}
2022-06-04 03:43:19 +00:00
}
}
}