From b14f886365abef6fd2372f7174e10dfc4282debb Mon Sep 17 00:00:00 2001 From: Chinmay Chougaonkar Date: Mon, 21 Mar 2022 11:39:31 +0530 Subject: [PATCH] changed flashLoanFee to flashLoanFees[], removed migrateCompound function, removed checks for repeated tokens from getSupplyAmounts and getBorrowAmounts --- .../connectors/compound-import/helpers.sol | 39 ++++++++----------- .../connectors/compound-import/main.sol | 32 +++------------ 2 files changed, 22 insertions(+), 49 deletions(-) diff --git a/contracts/mainnet/connectors/compound-import/helpers.sol b/contracts/mainnet/connectors/compound-import/helpers.sol index 78e600b1..6e906ad7 100644 --- a/contracts/mainnet/connectors/compound-import/helpers.sol +++ b/contracts/mainnet/connectors/compound-import/helpers.sol @@ -79,15 +79,6 @@ contract CompoundHelper is Helpers { data.borrowCtokensAddr = new address[](_importInputData.borrowIds.length); data.borrowAmts = new uint[](_importInputData.borrowIds.length); - // check for repeated tokens - for (uint i = 0; i < _importInputData.borrowIds.length; i++) { - bytes32 i_hash = keccak256(abi.encode(_importInputData.borrowIds[i])); - for (uint j = i + 1; j < _importInputData.borrowIds.length; j++) { - bytes32 j_hash = keccak256(abi.encode(_importInputData.borrowIds[j])); - require(i_hash != j_hash, "token-repeated"); - } - } - // populate the arrays with borrow tokens, cToken addresses and instances, and borrow amounts for (uint i = 0; i < _importInputData.borrowIds.length; i++) { (address _token, address _cToken) = compMapping.getMapping(_importInputData.borrowIds[i]); @@ -128,15 +119,6 @@ contract CompoundHelper is Helpers { data.supplyCtokensAddr = new address[](_importInputData.supplyIds.length); data.supplyAmts = new uint[](_importInputData.supplyIds.length); - // check for repeated tokens - for (uint i = 0; i < _importInputData.supplyIds.length; i++) { - bytes32 i_hash = keccak256(abi.encode(_importInputData.supplyIds[i])); - for (uint j = i + 1; j < _importInputData.supplyIds.length; j++) { - bytes32 j_hash = keccak256(abi.encode(_importInputData.supplyIds[j])); - require(i_hash != j_hash, "token-repeated"); - } - } - // populate arrays with supply data (supply tokens address, cToken addresses, cToken instances and supply amounts) for (uint i = 0; i < _importInputData.supplyIds.length; i++) { (address _token, address _cToken) = compMapping.getMapping(_importInputData.supplyIds[i]); @@ -172,7 +154,10 @@ contract CompoundHelper is Helpers { cEth.repayBorrowBehalf{value: _borrowAmts[i]}(_userAccount); } else{ - require(_cTokenContracts[i].repayBorrowBehalf(_userAccount, _borrowAmts[i]) == 0, "repayOnBehalf-failed"); + require(_cTokenContracts[i].repayBorrowBehalf( + _userAccount, + _borrowAmts[i] + ) == 0, "repayOnBehalf-failed"); } } } @@ -192,7 +177,11 @@ contract CompoundHelper is Helpers { ) internal { for(uint i = 0; i < _cTokenContracts.length; i++) { if(_amts[i] > 0) { - require(_cTokenContracts[i].transferFrom(_userAccount, address(this), _amts[i]), "ctoken-transfer-failed-allowance?"); + require(_cTokenContracts[i].transferFrom( + _userAccount, + address(this), + _amts[i] + ), "ctoken-transfer-failed-allowance?"); } } } @@ -202,16 +191,20 @@ contract CompoundHelper is Helpers { * @dev actually borrow some extra amount than the original position to cover the flash loan fee * @param _cTokenContracts array containing all interfaces to the cToken contracts in which the user has debt positions * @param _amts array containing the amounts the user had borrowed originally from Compound plus the flash loan fee - * @param _flashLoanFee flash loan fee (in percentage and scaled up to 10**2) + * @param _flashLoanFees flash loan fee (in percentage and scaled up to 10**2) */ function _borrowDebtPosition( CTokenInterface[] memory _cTokenContracts, uint256[] memory _amts, - uint256 _flashLoanFee + uint256[] memory _flashLoanFees ) internal { for (uint i = 0; i < _cTokenContracts.length; i++) { if (_amts[i] > 0) { - require(_cTokenContracts[i].borrow(add(_amts[i], mul(_amts[i], mul(_flashLoanFee, 10**14)))) == 0, "borrow-failed-collateral?"); + require(_cTokenContracts[i].borrow( + add( + _amts[i], + _flashLoanFees[i] + )) == 0, "borrow-failed-collateral?"); } } } diff --git a/contracts/mainnet/connectors/compound-import/main.sol b/contracts/mainnet/connectors/compound-import/main.sol index fbea89a3..eda52e75 100644 --- a/contracts/mainnet/connectors/compound-import/main.sol +++ b/contracts/mainnet/connectors/compound-import/main.sol @@ -16,11 +16,11 @@ contract CompoundImportResolver is CompoundHelper { * @notice this function performs the import of user's Compound positions into its DSA * @dev called internally by the importCompound and migrateCompound functions * @param _importInputData the struct containing borrowIds of the users borrowed tokens - * @param _flashLoanFee flash loan fee + * @param _flashLoanFees list of flash loan fees */ function _importCompound( ImportInputData memory _importInputData, - uint256 _flashLoanFee + uint256[] memory _flashLoanFees ) internal returns (string memory _eventName, bytes memory _eventParam) { require(AccountInterface(address(this)).isAuth(_importInputData.userAccount), "user-account-not-auth"); @@ -46,7 +46,7 @@ contract CompoundImportResolver is CompoundHelper { _transferTokensToDsa(_importInputData.userAccount, data.supplyCtokens, data.supplyAmts); // borrow the earlier position from Compound with flash loan fee added - _borrowDebtPosition(data.borrowCtokens, data.borrowAmts, _flashLoanFee); + _borrowDebtPosition(data.borrowCtokens, data.borrowAmts, _flashLoanFees); _eventName = "LogCompoundImport(address,address[],string[],string[],uint256[],uint256[])"; _eventParam = abi.encode( @@ -65,13 +65,13 @@ contract CompoundImportResolver is CompoundHelper { * @param _userAccount address of user whose position is to be imported to DSA * @param _supplyIds Ids of all tokens the user has supplied to Compound * @param _borrowIds Ids of all token borrowed by the user - * @param _flashLoanFee flash loan fee (in percentage and scaled up to 10**2) + * @param _flashLoanFees list of flash loan fees */ function importCompound( address _userAccount, string[] memory _supplyIds, string[] memory _borrowIds, - uint256 _flashLoanFee + uint256[] memory _flashLoanFees ) external payable returns (string memory _eventName, bytes memory _eventParam) { ImportInputData memory inputData = ImportInputData({ userAccount: _userAccount, @@ -79,29 +79,9 @@ contract CompoundImportResolver is CompoundHelper { borrowIds: _borrowIds }); - (_eventName, _eventParam) = _importCompound(inputData, _flashLoanFee); + (_eventName, _eventParam) = _importCompound(inputData, _flashLoanFees); } - /** - * @notice import msg.sender's Compound position (which is the user since this is a delegateCall) - * @dev internally calls _importContract to perform the actual import - * @param _supplyIds Ids of all tokens the user has supplied to Compound - * @param _borrowIds Ids of all token borrowed by the user - * @param _flashLoanFee flash loan fee (in percentage and scaled up to 10**2) - */ - function migrateCompound( - string[] memory _supplyIds, - string[] memory _borrowIds, - uint256 _flashLoanFee - ) external payable returns (string memory _eventName, bytes memory _eventParam) { - ImportInputData memory inputData = ImportInputData({ - userAccount: msg.sender, - supplyIds: _supplyIds, - borrowIds: _borrowIds - }); - - (_eventName, _eventParam) = _importCompound(inputData, _flashLoanFee); - } } contract ConnectV2CompoundImport is CompoundImportResolver {