From 32429d2b05af17f4e1e1e87ffccf0578e7fdccd4 Mon Sep 17 00:00:00 2001 From: Richa-iitr Date: Wed, 8 Jun 2022 19:20:24 +0530 Subject: [PATCH] code refactor --- contracts/avalanche/connectors/swap/events.sol | 3 +-- .../avalanche/connectors/swap/helpers.sol | 18 +++++++++--------- contracts/avalanche/connectors/swap/main.sol | 3 +-- contracts/mainnet/connectors/swap/events.sol | 3 +-- contracts/mainnet/connectors/swap/helpers.sol | 17 +++++++---------- contracts/mainnet/connectors/swap/main.sol | 3 +-- contracts/polygon/connectors/swap/events.sol | 3 +-- contracts/polygon/connectors/swap/helpers.sol | 16 +++++++--------- contracts/polygon/connectors/swap/main.sol | 3 +-- test/avalanche/swap/swap-test.ts | 4 ++-- test/mainnet/swap/swap-test.ts | 4 ++-- test/polygon/swap/swap-test.ts | 4 ++-- 12 files changed, 35 insertions(+), 46 deletions(-) diff --git a/contracts/avalanche/connectors/swap/events.sol b/contracts/avalanche/connectors/swap/events.sol index 463d3730..da92aeb2 100644 --- a/contracts/avalanche/connectors/swap/events.sol +++ b/contracts/avalanche/connectors/swap/events.sol @@ -2,13 +2,12 @@ pragma solidity ^0.7.0; contract Events { - event LogSwap( + event LogSwapAggregator( string _connector, address indexed buyToken, address indexed sellToken, uint256 buyAmt, uint256 sellAmt, - uint256 getId, uint256 setId ); } diff --git a/contracts/avalanche/connectors/swap/helpers.sol b/contracts/avalanche/connectors/swap/helpers.sol index 5285982f..c51683b5 100644 --- a/contracts/avalanche/connectors/swap/helpers.sol +++ b/contracts/avalanche/connectors/swap/helpers.sol @@ -4,7 +4,7 @@ pragma abicoder v2; import { InstaConnectors } from "../../common/interfaces.sol"; -abstract contract Helper { +contract SwapHelpers { /** * @dev Instadapp Connectors Registry */ @@ -19,9 +19,7 @@ abstract contract Helper { bytes[] callDatas; uint256 setId; } -} -contract SwapHelpers is Helper { /** *@dev Swap using the dex aggregators. *@param _connectors name of the connectors in preference order. @@ -35,25 +33,27 @@ contract SwapHelpers is Helper { string memory _connector ) { - require(_connectors.length > 0, "zero-length-not-allowed"); + uint256 _length = _connectors.length; + require(_length > 0, "zero-length-not-allowed"); require( - _inputData.unitAmts.length == _connectors.length, + _inputData.unitAmts.length == _length, "unitAmts-length-invalid" ); require( - _inputData.callDatas.length == _connectors.length, + _inputData.callDatas.length == _length, "callDatas-length-invalid" ); // require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors - for (uint256 i = 0; i < _connectors.length; i++) { + for (uint256 i = 0; i < _length; i++) { bytes4 swapData = bytes4( keccak256("swap(address,address,uint256,uint256,bytes,uint256)") ); - string memory _1INCH = "1INCH-A"; - if (keccak256(bytes(_connectors[i])) == keccak256(bytes(_1INCH))) { + if ( + keccak256(bytes(_connectors[i])) == keccak256(bytes("1INCH-A")) + ) { swapData = bytes4( keccak256( "sell(address,address,uint256,uint256,bytes,uint256)" diff --git a/contracts/avalanche/connectors/swap/main.sol b/contracts/avalanche/connectors/swap/main.sol index 5bb6a6f8..198b663c 100644 --- a/contracts/avalanche/connectors/swap/main.sol +++ b/contracts/avalanche/connectors/swap/main.sol @@ -60,14 +60,13 @@ abstract contract Swap is SwapHelpers, Events { (_buyAmt, _sellAmt) = decodeEvents(_connector, returnData); } - _eventName = "LogSwap(string,address,address,uint256,uint256,uint256,uint256)"; + _eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( _connector, buyAddr, sellAddr, _buyAmt, _sellAmt, - 0, setId ); } diff --git a/contracts/mainnet/connectors/swap/events.sol b/contracts/mainnet/connectors/swap/events.sol index 463d3730..da92aeb2 100644 --- a/contracts/mainnet/connectors/swap/events.sol +++ b/contracts/mainnet/connectors/swap/events.sol @@ -2,13 +2,12 @@ pragma solidity ^0.7.0; contract Events { - event LogSwap( + event LogSwapAggregator( string _connector, address indexed buyToken, address indexed sellToken, uint256 buyAmt, uint256 sellAmt, - uint256 getId, uint256 setId ); } diff --git a/contracts/mainnet/connectors/swap/helpers.sol b/contracts/mainnet/connectors/swap/helpers.sol index f25be85a..1e875145 100644 --- a/contracts/mainnet/connectors/swap/helpers.sol +++ b/contracts/mainnet/connectors/swap/helpers.sol @@ -4,7 +4,7 @@ pragma abicoder v2; import { InstaConnectors } from "../../common/interfaces.sol"; -abstract contract Helper { +contract SwapHelpers { /** * @dev Instadapp Connectors Registry */ @@ -19,9 +19,6 @@ abstract contract Helper { bytes[] callDatas; uint256 setId; } -} - -contract SwapHelpers is Helper { /** *@dev Swap using the dex aggregators. *@param _connectors name of the connectors in preference order. @@ -35,25 +32,25 @@ contract SwapHelpers is Helper { string memory _connector ) { - require(_connectors.length > 0, "zero-length-not-allowed"); + uint256 _length = _connectors.length; + require(_length > 0, "zero-length-not-allowed"); require( - _inputData.unitAmts.length == _connectors.length, + _inputData.unitAmts.length == _length, "unitAmts-length-invalid" ); require( - _inputData.callDatas.length == _connectors.length, + _inputData.callDatas.length == _length, "callDatas-length-invalid" ); // require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors - for (uint256 i = 0; i < _connectors.length; i++) { + for (uint256 i = 0; i < _length; i++) { bytes4 swapData = bytes4( keccak256("swap(address,address,uint256,uint256,bytes,uint256)") ); - string memory _1INCH = "1INCH-A"; - if (keccak256(bytes(_connectors[i])) == keccak256(bytes(_1INCH))) { + if (keccak256(bytes(_connectors[i])) == keccak256(bytes("1INCH-A"))) { swapData = bytes4( keccak256( "sell(address,address,uint256,uint256,bytes,uint256)" diff --git a/contracts/mainnet/connectors/swap/main.sol b/contracts/mainnet/connectors/swap/main.sol index a6c61cbd..2f7bda18 100644 --- a/contracts/mainnet/connectors/swap/main.sol +++ b/contracts/mainnet/connectors/swap/main.sol @@ -60,14 +60,13 @@ abstract contract Swap is SwapHelpers, Events { (_buyAmt, _sellAmt) = decodeEvents(_connector, returnData); } - _eventName = "LogSwap(string,address,address,uint256,uint256,uint256,uint256)"; + _eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( _connector, buyAddr, sellAddr, _buyAmt, _sellAmt, - 0, setId ); } diff --git a/contracts/polygon/connectors/swap/events.sol b/contracts/polygon/connectors/swap/events.sol index 463d3730..da92aeb2 100644 --- a/contracts/polygon/connectors/swap/events.sol +++ b/contracts/polygon/connectors/swap/events.sol @@ -2,13 +2,12 @@ pragma solidity ^0.7.0; contract Events { - event LogSwap( + event LogSwapAggregator( string _connector, address indexed buyToken, address indexed sellToken, uint256 buyAmt, uint256 sellAmt, - uint256 getId, uint256 setId ); } diff --git a/contracts/polygon/connectors/swap/helpers.sol b/contracts/polygon/connectors/swap/helpers.sol index 40c27b6b..f6729788 100644 --- a/contracts/polygon/connectors/swap/helpers.sol +++ b/contracts/polygon/connectors/swap/helpers.sol @@ -4,7 +4,7 @@ pragma abicoder v2; import { InstaConnectors } from "../../common/interfaces.sol"; -abstract contract Helper { +contract SwapHelpers { /** * @dev Instadapp Connectors Registry */ @@ -19,9 +19,7 @@ abstract contract Helper { bytes[] callDatas; uint256 setId; } -} -contract SwapHelpers is Helper { /** *@dev Swap using the dex aggregators. *@param _connectors name of the connectors in preference order. @@ -35,25 +33,25 @@ contract SwapHelpers is Helper { string memory _connector ) { - require(_connectors.length > 0, "zero-length-not-allowed"); + uint256 _length = _connectors.length; + require(_length > 0, "zero-length-not-allowed"); require( - _inputData.unitAmts.length == _connectors.length, + _inputData.unitAmts.length == _length, "unitAmts-length-invalid" ); require( - _inputData.callDatas.length == _connectors.length, + _inputData.callDatas.length == _length, "callDatas-length-invalid" ); // require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors - for (uint256 i = 0; i < _connectors.length; i++) { + for (uint256 i = 0; i < _length; i++) { bytes4 swapData = bytes4( keccak256("swap(address,address,uint256,uint256,bytes,uint256)") ); - string memory _1INCH = "1INCH-A"; - if (keccak256(bytes(_connectors[i])) == keccak256(bytes(_1INCH))) { + if (keccak256(bytes(_connectors[i])) == keccak256(bytes("1INCH-A"))) { swapData = bytes4( keccak256( "sell(address,address,uint256,uint256,bytes,uint256)" diff --git a/contracts/polygon/connectors/swap/main.sol b/contracts/polygon/connectors/swap/main.sol index 93a6d6af..857423dd 100644 --- a/contracts/polygon/connectors/swap/main.sol +++ b/contracts/polygon/connectors/swap/main.sol @@ -60,14 +60,13 @@ abstract contract Swap is SwapHelpers, Events { (_buyAmt, _sellAmt) = decodeEvents(_connector, returnData); } - _eventName = "LogSwap(string,address,address,uint256,uint256,uint256,uint256)"; + _eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)"; _eventParam = abi.encode( _connector, buyAddr, sellAddr, _buyAmt, _sellAmt, - 0, setId ); } diff --git a/test/avalanche/swap/swap-test.ts b/test/avalanche/swap/swap-test.ts index 6490777b..7abcc5c9 100644 --- a/test/avalanche/swap/swap-test.ts +++ b/test/avalanche/swap/swap-test.ts @@ -9,7 +9,7 @@ import { encodeSpells } from "../../../scripts/tests/encodeSpells"; import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; import { addresses } from "../../../scripts/tests/avalanche/addresses"; import { abis } from "../../../scripts/constant/abis"; -import { ConnectV2SwapAvalanche__factory } from "../../../typechain"; +import { ConnectV2SwapAggregatorAvalanche__factory } from "../../../typechain"; import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; import type { Signer, Contract } from "ethers"; @@ -40,7 +40,7 @@ describe("Swap | Avalanche", function () { instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({ connectorName, - contractArtifact: ConnectV2SwapAvalanche__factory, + contractArtifact: ConnectV2SwapAggregatorAvalanche__factory, signer: masterSigner, connectors: instaConnectorsV2 }); diff --git a/test/mainnet/swap/swap-test.ts b/test/mainnet/swap/swap-test.ts index a64c0831..dde35cf3 100644 --- a/test/mainnet/swap/swap-test.ts +++ b/test/mainnet/swap/swap-test.ts @@ -9,7 +9,7 @@ import { encodeSpells } from "../../../scripts/tests/encodeSpells"; import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; import { addresses } from "../../../scripts/tests/mainnet/addresses"; import { abis } from "../../../scripts/constant/abis"; -import { ConnectV2Swap__factory } from "../../../typechain"; +import { ConnectV2SwapAggregator__factory } from "../../../typechain"; import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; import type { Signer, Contract } from "ethers"; @@ -40,7 +40,7 @@ describe("Swap | Mainnet", function () { instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({ connectorName, - contractArtifact: ConnectV2Swap__factory, + contractArtifact: ConnectV2SwapAggregator__factory, signer: masterSigner, connectors: instaConnectorsV2 }); diff --git a/test/polygon/swap/swap-test.ts b/test/polygon/swap/swap-test.ts index 80090ea8..25c8bce6 100644 --- a/test/polygon/swap/swap-test.ts +++ b/test/polygon/swap/swap-test.ts @@ -9,7 +9,7 @@ import { encodeSpells } from "../../../scripts/tests/encodeSpells"; import { getMasterSigner } from "../../../scripts/tests/getMasterSigner"; import { addresses } from "../../../scripts/tests/polygon/addresses"; import { abis } from "../../../scripts/constant/abis"; -import { ConnectV2SwapPolygon__factory } from "../../../typechain"; +import { ConnectV2SwapAggregatorPolygon__factory } from "../../../typechain"; import er20abi from "../../../scripts/constant/abi/basics/erc20.json"; import type { Signer, Contract } from "ethers"; @@ -40,7 +40,7 @@ describe("Swap", function () { instaConnectorsV2 = await ethers.getContractAt(abis.core.connectorsV2, addresses.core.connectorsV2); connector = await deployAndEnableConnector({ connectorName, - contractArtifact: ConnectV2SwapPolygon__factory, + contractArtifact: ConnectV2SwapAggregatorPolygon__factory, signer: masterSigner, connectors: instaConnectorsV2 });