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

70 lines
1.6 KiB
Solidity
Raw Normal View History

2022-06-04 19:04:58 +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 19:04:58 +00:00
/**
* @dev Instadapp Connectors Registry
*/
InstaConnectors internal constant instaConnectors =
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
struct InputData {
address buyAddr;
address sellAddr;
uint256 sellAmt;
uint256[] unitAmts;
2022-06-14 03:12:15 +00:00
bytes4[] swapDatas;
2022-06-04 19:04:58 +00:00
bytes[] callDatas;
uint256 setId;
}
/**
*@dev Swap using the dex aggregators.
*@param _connectors name of the connectors in preference order.
*@param _inputData data for the swap cast.
*/
function _swap(string[] memory _connectors, InputData memory _inputData)
internal
2022-06-14 03:12:15 +00:00
returns (bool success, bytes memory returnData)
2022-06-04 19:04:58 +00:00
{
2022-06-08 13:50:24 +00:00
uint256 _length = _connectors.length;
require(_length > 0, "zero-length-not-allowed");
2022-06-04 19:04:58 +00:00
require(
2022-06-08 13:50:24 +00:00
_inputData.unitAmts.length == _length,
2022-06-04 19:04:58 +00:00
"unitAmts-length-invalid"
);
require(
2022-06-08 13:50:24 +00:00
_inputData.callDatas.length == _length,
2022-06-04 19:04:58 +00:00
"callDatas-length-invalid"
);
2022-06-14 03:12:15 +00:00
require(
_inputData.swapDatas.length == _length,
"swapDatas-length-invalid"
);
2022-06-04 19:04:58 +00:00
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
2022-06-08 13:50:24 +00:00
for (uint256 i = 0; i < _length; i++) {
2022-06-04 19:04:58 +00:00
bytes memory _data = abi.encodeWithSelector(
2022-06-14 03:12:15 +00:00
_inputData.swapDatas[i],
2022-06-04 19:04:58 +00:00
_inputData.buyAddr,
_inputData.sellAddr,
_inputData.sellAmt,
_inputData.unitAmts[i],
_inputData.callDatas[i],
_inputData.setId
);
(success, returnData) = instaConnectors
.connectors(_connectors[i])
.delegatecall(_data);
if (success) {
break;
}
}
}
}