diff --git a/contracts/avalanche/connectors/swap/helpers.sol b/contracts/avalanche/connectors/swap/helpers.sol index 3d7b291b..7775be61 100644 --- a/contracts/avalanche/connectors/swap/helpers.sol +++ b/contracts/avalanche/connectors/swap/helpers.sol @@ -11,56 +11,23 @@ contract SwapHelpers { InstaConnectors internal constant instaConnectors = InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14); - struct InputData { - address buyAddr; - address sellAddr; - uint256 sellAmt; - uint256[] unitAmts; - bytes4[] swapDatas; - 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. + *@param _data data for the swap cast. */ - function _swap(string[] memory _connectors, InputData memory _inputData) + function _swap(string[] memory _connectors, bytes[] memory _data) internal returns (bool success, bytes memory returnData) { uint256 _length = _connectors.length; require(_length > 0, "zero-length-not-allowed"); - require( - _inputData.unitAmts.length == _length, - "unitAmts-length-invalid" - ); - require( - _inputData.callDatas.length == _length, - "callDatas-length-invalid" - ); - require( - _inputData.swapDatas.length == _length, - "swapDatas-length-invalid" - ); - - // require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors + require(_data.length == _length, "calldata-length-invalid"); for (uint256 i = 0; i < _length; i++) { - bytes memory _data = abi.encodeWithSelector( - _inputData.swapDatas[i], - _inputData.buyAddr, - _inputData.sellAddr, - _inputData.sellAmt, - _inputData.unitAmts[i], - _inputData.callDatas[i], - _inputData.setId - ); - (success, returnData) = instaConnectors .connectors(_connectors[i]) - .delegatecall(_data); + .delegatecall(_data[i]); if (success) { break; } diff --git a/contracts/avalanche/connectors/swap/main.sol b/contracts/avalanche/connectors/swap/main.sol index 0cfa98ef..83ce0c26 100644 --- a/contracts/avalanche/connectors/swap/main.sol +++ b/contracts/avalanche/connectors/swap/main.sol @@ -15,39 +15,17 @@ abstract contract Swap is SwapHelpers { * @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 buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAmt The amount of the token to sell. - * @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs. - * @param swapDatas The function selectors of swap methods of the DEXs. - * @param callDatas Data from APIs for respective DEXs. - * @param setId ID stores the amount of token brought. + * @param _data Encoded function call data including function selector encoded with parameters. */ function swap( - address buyAddr, - address sellAddr, - uint256 sellAmt, - uint256[] memory unitAmts, - bytes4[] memory swapDatas, - bytes[] calldata callDatas, string[] memory _connectors, - uint256 setId + bytes[] memory _data ) external payable returns (string memory _eventName, bytes memory _eventParam) { - InputData memory inputData = InputData({ - buyAddr: buyAddr, - sellAddr: sellAddr, - sellAmt: sellAmt, - unitAmts: unitAmts, - swapDatas: swapDatas, - callDatas: callDatas, - setId: setId - }); - - (bool success, bytes memory returnData) = _swap(_connectors, inputData); + (bool success, bytes memory returnData) = _swap(_connectors, _data); require(success, "swap-Aggregator-failed"); (_eventName, _eventParam) = abi.decode(returnData, (string, bytes)); diff --git a/contracts/mainnet/connectors/swap/helpers.sol b/contracts/mainnet/connectors/swap/helpers.sol index 890ae9c0..68b37832 100644 --- a/contracts/mainnet/connectors/swap/helpers.sol +++ b/contracts/mainnet/connectors/swap/helpers.sol @@ -11,56 +11,23 @@ contract SwapHelpers { InstaConnectors internal constant instaConnectors = InstaConnectors(0x97b0B3A8bDeFE8cB9563a3c610019Ad10DB8aD11); - struct InputData { - address buyAddr; - address sellAddr; - uint256 sellAmt; - uint256[] unitAmts; - bytes4[] swapDatas; - 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. + *@param _data data for the swap cast. */ - function _swap(string[] memory _connectors, InputData memory _inputData) + function _swap(string[] memory _connectors, bytes[] memory _data) internal returns (bool success, bytes memory returnData) { uint256 _length = _connectors.length; require(_length > 0, "zero-length-not-allowed"); - require( - _inputData.unitAmts.length == _length, - "unitAmts-length-invalid" - ); - require( - _inputData.callDatas.length == _length, - "callDatas-length-invalid" - ); - require( - _inputData.swapDatas.length == _length, - "swapDatas-length-invalid" - ); - - // require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors + require(_data.length == _length, "calldata-length-invalid"); for (uint256 i = 0; i < _length; i++) { - bytes memory _data = abi.encodeWithSelector( - _inputData.swapDatas[i], - _inputData.buyAddr, - _inputData.sellAddr, - _inputData.sellAmt, - _inputData.unitAmts[i], - _inputData.callDatas[i], - _inputData.setId - ); - (success, returnData) = instaConnectors .connectors(_connectors[i]) - .delegatecall(_data); + .delegatecall(_data[i]); if (success) { break; } diff --git a/contracts/mainnet/connectors/swap/main.sol b/contracts/mainnet/connectors/swap/main.sol index a76c2432..a98ee841 100644 --- a/contracts/mainnet/connectors/swap/main.sol +++ b/contracts/mainnet/connectors/swap/main.sol @@ -15,39 +15,14 @@ abstract contract Swap is SwapHelpers { * @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 buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAmt The amount of the token to sell. - * @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs. - * @param swapDatas The function selectors of swap methods of the DEXs. - * @param callDatas Data from APIs for respective DEXs. - * @param setId ID stores the amount of token brought. + * @param _data Encoded function call data including function selector encoded with parameters. */ - function swap( - address buyAddr, - address sellAddr, - uint256 sellAmt, - uint256[] memory unitAmts, - bytes4[] memory swapDatas, - bytes[] calldata callDatas, - string[] memory _connectors, - uint256 setId - ) + function swap(string[] memory _connectors, bytes[] memory _data) external payable returns (string memory _eventName, bytes memory _eventParam) { - InputData memory inputData = InputData({ - buyAddr: buyAddr, - sellAddr: sellAddr, - sellAmt: sellAmt, - unitAmts: unitAmts, - swapDatas: swapDatas, - callDatas: callDatas, - setId: setId - }); - - (bool success, bytes memory returnData) = _swap(_connectors, inputData); + (bool success, bytes memory returnData) = _swap(_connectors, _data); require(success, "swap-Aggregator-failed"); (_eventName, _eventParam) = abi.decode(returnData, (string, bytes)); diff --git a/contracts/polygon/connectors/swap/helpers.sol b/contracts/polygon/connectors/swap/helpers.sol index 26e99514..788a746b 100644 --- a/contracts/polygon/connectors/swap/helpers.sol +++ b/contracts/polygon/connectors/swap/helpers.sol @@ -11,56 +11,23 @@ contract SwapHelpers { InstaConnectors internal constant instaConnectors = InstaConnectors(0x2A00684bFAb9717C21271E0751BCcb7d2D763c88); - struct InputData { - address buyAddr; - address sellAddr; - uint256 sellAmt; - uint256[] unitAmts; - bytes4[] swapDatas; - 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. + *@param _data data for the swap cast. */ - function _swap(string[] memory _connectors, InputData memory _inputData) + function _swap(string[] memory _connectors, bytes[] memory _data) internal returns (bool success, bytes memory returnData) { uint256 _length = _connectors.length; require(_length > 0, "zero-length-not-allowed"); - require( - _inputData.unitAmts.length == _length, - "unitAmts-length-invalid" - ); - require( - _inputData.callDatas.length == _length, - "callDatas-length-invalid" - ); - require( - _inputData.swapDatas.length == _length, - "swapDatas-length-invalid" - ); - - // require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors + require(_data.length == _length, "calldata-length-invalid"); for (uint256 i = 0; i < _length; i++) { - bytes memory _data = abi.encodeWithSelector( - _inputData.swapDatas[i], - _inputData.buyAddr, - _inputData.sellAddr, - _inputData.sellAmt, - _inputData.unitAmts[i], - _inputData.callDatas[i], - _inputData.setId - ); - (success, returnData) = instaConnectors .connectors(_connectors[i]) - .delegatecall(_data); + .delegatecall(_data[i]); if (success) { break; } diff --git a/contracts/polygon/connectors/swap/main.sol b/contracts/polygon/connectors/swap/main.sol index ada14dc6..e8c79a42 100644 --- a/contracts/polygon/connectors/swap/main.sol +++ b/contracts/polygon/connectors/swap/main.sol @@ -15,39 +15,14 @@ abstract contract Swap is SwapHelpers { * @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 buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE) - * @param sellAmt The amount of the token to sell. - * @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs. - * @param swapDatas The function selectors of swap methods of the DEXs. - * @param callDatas Data from APIs for respective DEXs. - * @param setId ID stores the amount of token brought. + * @param _data Encoded function call data including function selector encoded with parameters. */ - function swap( - address buyAddr, - address sellAddr, - uint256 sellAmt, - uint256[] memory unitAmts, - bytes4[] memory swapDatas, - bytes[] calldata callDatas, - string[] memory _connectors, - uint256 setId - ) + function swap(string[] memory _connectors, bytes[] memory _data) external payable returns (string memory _eventName, bytes memory _eventParam) { - InputData memory inputData = InputData({ - buyAddr: buyAddr, - sellAddr: sellAddr, - sellAmt: sellAmt, - unitAmts: unitAmts, - swapDatas: swapDatas, - callDatas: callDatas, - setId: setId - }); - - (bool success, bytes memory returnData) = _swap(_connectors, inputData); + (bool success, bytes memory returnData) = _swap(_connectors, _data); require(success, "swap-Aggregator-failed"); (_eventName, _eventParam) = abi.decode(returnData, (string, bytes)); diff --git a/test/avalanche/swap/swap-test.ts b/test/avalanche/swap/swap-test.ts index a1f74a5d..8dc4efb0 100644 --- a/test/avalanche/swap/swap-test.ts +++ b/test/avalanche/swap/swap-test.ts @@ -73,24 +73,11 @@ describe("Swap | Avalanche", function () { describe("Main", function () { it("should swap the tokens", async function () { let buyTokenAmountZeroX: any; + let unitAmount1Inch: any; + let calldata1Inch: any; // let buyTokenAmount1Inch: any; let buyTokenAmountParaswap: any; - async function getSelector(connector: string) { - var abi = [ - "function swap(address,address,uint256,uint256,bytes,uint256)", - "function sell(address,address,uint256,uint256,bytes,uint256)" - ]; - var iface = new ethers.utils.Interface(abi); - var id; - if (connector == "1INCH-A") { - id = iface.getSighash("sell"); - } else { - id = iface.getSighash("swap"); - } - - return id; - } async function getArg() { // const slippage = 0.5; /* avax -> usdt */ @@ -155,17 +142,45 @@ describe("Swap | Avalanche", function () { unitAmt = unitAmt.multipliedBy(1e18).toFixed(0); return unitAmt; }; + let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX); let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap); - let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A")); - let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A")); - let unitAmts = [unitAmtParaswap, unitAmt0x]; - let calldatas = [calldataPara, calldataZeroX]; - let swapDatas = [swapDataPara, swapDataZeroX]; + + function getSelector(connector: string, unitAmt: any, callData: any) { + var abi = [ + "function swap(address,address,uint256,uint256,bytes,uint256)", + "function sell(address,address,uint256,uint256,bytes,uint256)" + ]; + var iface = new ethers.utils.Interface(abi); + var data; + if (connector == "1INCH-A") { + data = iface.encodeFunctionData("sell", [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + callData, + 0 + ]); + } else { + data = iface.encodeFunctionData("swap", [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + callData, + 0 + ]); + } + return data; + } + let dataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A", unitAmtParaswap, calldataPara)); + let dataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A", unitAmt0x, calldataZeroX)); + let datas = [dataPara, dataZeroX]; let connectors = ["PARASWAP-A", "ZEROX-A"]; - return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0]; + return [connectors, datas]; } let arg = await getArg(); diff --git a/test/mainnet/swap/swap-test.ts b/test/mainnet/swap/swap-test.ts index eef0ea7c..69d4dad4 100644 --- a/test/mainnet/swap/swap-test.ts +++ b/test/mainnet/swap/swap-test.ts @@ -76,22 +76,6 @@ describe("Swap | Mainnet", function () { let buyTokenAmount1Inch: any; let buyTokenAmountParaswap: any; - async function getSelector(connector: string) { - var abi = [ - "function swap(address,address,uint256,uint256,bytes,uint256)", - "function sell(address,address,uint256,uint256,bytes,uint256)" - ]; - var iface = new ethers.utils.Interface(abi); - var id; - if (connector == "1INCH-A") { - id = iface.getSighash("sell"); - } else { - id = iface.getSighash("swap"); - } - - return id; - } - async function getArg() { const slippage = 0.5; /* eth -> dai */ @@ -178,17 +162,42 @@ describe("Swap | Mainnet", function () { let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX); let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap); - let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A")); - let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A")); - let swapData1Inch = ethers.utils.hexlify(await getSelector("1INCH-A")); + function getSelector(connector: string, unitAmt: any, callData: any) { + var abi = [ + "function swap(address,address,uint256,uint256,bytes,uint256)", + "function sell(address,address,uint256,uint256,bytes,uint256)" + ]; + var iface = new ethers.utils.Interface(abi); + var data; + if (connector == "1INCH-A") { + data = iface.encodeFunctionData("sell", [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + callData, + 0 + ]); + } else { + data = iface.encodeFunctionData("swap", [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + callData, + 0 + ]); + } + return data; + } + let data1Inch = ethers.utils.hexlify(await getSelector("1INCH-A", unitAmt1Inch, calldata1Inch)); + let dataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A", unitAmtParaswap, calldataPara)); + let dataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A", unitAmt0x, calldataZeroX)); + let datas = [data1Inch, dataPara, dataZeroX]; - let unitAmts = [unitAmt1Inch, unitAmt0x, unitAmtParaswap]; - let calldatas = [calldata1Inch, calldataZeroX, calldataPara]; - let swapDatas = [swapData1Inch, swapDataZeroX, swapDataPara]; + let connectors = ["1INCH-A", "PARASWAP-A", "ZEROX-A"]; - let connectors = ["1INCH-A", "ZEROX-A", "PARASWAP-A"]; - - return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0]; + return [connectors, datas]; } let arg = await getArg(); diff --git a/test/polygon/swap/swap-test.ts b/test/polygon/swap/swap-test.ts index 0abbd569..2b54e70a 100644 --- a/test/polygon/swap/swap-test.ts +++ b/test/polygon/swap/swap-test.ts @@ -75,6 +75,8 @@ describe("Swap", function () { let buyTokenAmountZeroX: any; let buyTokenAmount1Inch: any; let buyTokenAmountParaswap: any; + let unitAmount1Inch: any; + let calldata1Inch: any; async function getSelector(connector: string) { var abi = [ @@ -159,16 +161,41 @@ describe("Swap", function () { let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX); let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap); - let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A")); - let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A")); - - let unitAmts = [unitAmt0x, unitAmtParaswap]; - let calldatas = [calldataZeroX, calldataPara]; - let swapDatas = [swapDataZeroX, swapDataPara]; + function getCallData(connector: string, unitAmt: any, callData: any) { + var abi = [ + "function swap(address,address,uint256,uint256,bytes,uint256)", + "function sell(address,address,uint256,uint256,bytes,uint256)" + ]; + var iface = new ethers.utils.Interface(abi); + var data; + if (connector == "1INCH-A") { + data = iface.encodeFunctionData("sell", [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + callData, + 0 + ]); + } else { + data = iface.encodeFunctionData("swap", [ + buyTokenAddress, + sellTokenAddress, + srcAmount, + unitAmt, + callData, + 0 + ]); + } + return data; + } + let dataPara = ethers.utils.hexlify(await getCallData("PARASWAP-A", unitAmtParaswap, calldataPara)); + let dataZeroX = ethers.utils.hexlify(await getCallData("ZEROX-A", unitAmt0x, calldataZeroX)); + let datas = [dataZeroX, dataPara]; let connectors = ["ZEROX-A", "PARASWAP-A"]; - return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0]; + return [connectors, datas]; } let arg = await getArg();