From 3bff63707d99797cee3bc4c20e91c5be5054b52d Mon Sep 17 00:00:00 2001 From: gitpusha Date: Tue, 3 Nov 2020 14:56:56 +0100 Subject: [PATCH] chore: cleanup --- .../ConnectGelatoDataForFullRefinance.sol | 114 ++++++----- .../ConnectGelatoDataForPartialRefinance.sol | 181 ++++++++---------- .../ConnectGelatoProviderPayment.sol | 15 +- .../5_ConnectGelatoProviderPayment.test.js | 4 +- 4 files changed, 147 insertions(+), 167 deletions(-) diff --git a/contracts/contracts/connectors/ConnectGelatoDataForFullRefinance.sol b/contracts/contracts/connectors/ConnectGelatoDataForFullRefinance.sol index e175cbc..6e4b106 100644 --- a/contracts/contracts/connectors/ConnectGelatoDataForFullRefinance.sol +++ b/contracts/contracts/connectors/ConnectGelatoDataForFullRefinance.sol @@ -57,7 +57,7 @@ contract ConnectGelatoDataForFullRefinance is ConnectorInterface { /// @dev Connector Details function connectorID() - public + external view override returns (uint256 _type, uint256 id) @@ -65,11 +65,16 @@ contract ConnectGelatoDataForFullRefinance is ConnectorInterface { (_type, id) = (1, _id); // Should put specific value. } + /// @notice Entry Point for DSA.cast DebtBridge from e.g ETH-A to ETH-B + /// @dev payable to be compatible in conjunction with DSA.cast payable target + /// @param _vaultId Id of the unsafe vault of the client. + /// @param _token vault's col token address . + /// @param _colType colType of the new vault. example : ETH-B, ETH-A. function getDataAndCastForFromMakerToMaker( uint256 _vaultId, address _token, string calldata _colType - ) public payable { + ) external payable { ( address[] memory targets, bytes[] memory datas @@ -82,10 +87,14 @@ contract ConnectGelatoDataForFullRefinance is ConnectorInterface { _cast(targets, datas); } + /// @notice Entry Point for DSA.cast DebtBridge from Maker to Compound + /// @dev payable to be compatible in conjunction with DSA.cast payable target + /// @param _vaultId Id of the unsafe vault of the client. + /// @param _token vault's col token address . function getDataAndCastForFromMakerToCompound( uint256 _vaultId, address _token - ) public payable { + ) external payable { ( address[] memory targets, bytes[] memory datas @@ -114,61 +123,6 @@ contract ConnectGelatoDataForFullRefinance is ConnectorInterface { /* solhint-disable function-max-lines */ - /// @notice Generate Task for a full refinancing between Maker to Compound. - /// @param _vaultId Id of the unsafe vault of the client. - /// @param _token vault's col token address . - /// @return targets : flashloan contract address - /// @return datas : calldata for flashloan - function _execPayloadForFullRefinanceFromMakerToCompound( - uint256 _vaultId, - address _token - ) internal view returns (address[] memory targets, bytes[] memory datas) { - targets = new address[](1); - targets[0] = INSTA_POOL_V2; - - uint256 wDaiDebtToMove = _getMakerVaultDebt(_vaultId); - uint256 wColToWithdrawFromMaker = _getMakerVaultCollateralBalance( - _vaultId - ); - uint256 gasFeesPaidFromCol = _getGelatoProviderFees(GAS_COST); - - address[] memory _targets = new address[](6); - _targets[0] = CONNECT_MAKER; // payback - _targets[1] = CONNECT_MAKER; // withdraw - _targets[2] = CONNECT_COMPOUND; // deposit - _targets[3] = CONNECT_COMPOUND; // borrow - _targets[4] = _connectGelatoProviderPayment; - _targets[5] = INSTA_POOL_V2; - - bytes[] memory _datas = new bytes[](6); - _datas[0] = _encodePaybackMakerVault(_vaultId, uint256(-1), 0, 0); - _datas[1] = _encodedWithdrawMakerVault(_vaultId, uint256(-1), 0, 0); - _datas[2] = _encodeDepositCompound( - _token, - sub(wColToWithdrawFromMaker, gasFeesPaidFromCol), - 0, - 0 - ); - _datas[3] = _encodeBorrowCompound(DAI, wDaiDebtToMove, 0, 0); - _datas[4] = _encodePayGelatoProvider(_token, gasFeesPaidFromCol, 0, 0); - _datas[5] = _encodeFlashPayback(DAI, wDaiDebtToMove, 0, 0); - - datas = new bytes[](1); - datas[0] = abi.encodeWithSelector( - IConnectInstaPoolV2.flashBorrowAndCast.selector, - DAI, - wDaiDebtToMove, - 0, - abi.encode(_targets, _datas) - ); - } - - /// @notice Generate Task for a full refinancing between Maker e.g. ETH-A to ETH-B. - /// @param _vaultId Id of the unsafe vault of the client. - /// @param _token vault's col token address . - /// @param _colType colType of the new vault, exemple : ETH-B, ETH-A. - /// @return targets : flashloan contract address - /// @return datas : calldata for flashloan function _execPayloadForFullRefinanceFromMakerToMaker( uint256 _vaultId, address _token, @@ -216,5 +170,49 @@ contract ConnectGelatoDataForFullRefinance is ConnectorInterface { ); } + function _execPayloadForFullRefinanceFromMakerToCompound( + uint256 _vaultId, + address _token + ) internal view returns (address[] memory targets, bytes[] memory datas) { + targets = new address[](1); + targets[0] = INSTA_POOL_V2; + + uint256 wDaiDebtToMove = _getMakerVaultDebt(_vaultId); + uint256 wColToWithdrawFromMaker = _getMakerVaultCollateralBalance( + _vaultId + ); + uint256 gasFeesPaidFromCol = _getGelatoProviderFees(GAS_COST); + + address[] memory _targets = new address[](6); + _targets[0] = CONNECT_MAKER; // payback + _targets[1] = CONNECT_MAKER; // withdraw + _targets[2] = CONNECT_COMPOUND; // deposit + _targets[3] = CONNECT_COMPOUND; // borrow + _targets[4] = _connectGelatoProviderPayment; + _targets[5] = INSTA_POOL_V2; + + bytes[] memory _datas = new bytes[](6); + _datas[0] = _encodePaybackMakerVault(_vaultId, uint256(-1), 0, 0); + _datas[1] = _encodedWithdrawMakerVault(_vaultId, uint256(-1), 0, 0); + _datas[2] = _encodeDepositCompound( + _token, + sub(wColToWithdrawFromMaker, gasFeesPaidFromCol), + 0, + 0 + ); + _datas[3] = _encodeBorrowCompound(DAI, wDaiDebtToMove, 0, 0); + _datas[4] = _encodePayGelatoProvider(_token, gasFeesPaidFromCol, 0, 0); + _datas[5] = _encodeFlashPayback(DAI, wDaiDebtToMove, 0, 0); + + datas = new bytes[](1); + datas[0] = abi.encodeWithSelector( + IConnectInstaPoolV2.flashBorrowAndCast.selector, + DAI, + wDaiDebtToMove, + 0, + abi.encode(_targets, _datas) + ); + } + /* solhint-enable function-max-lines */ } diff --git a/contracts/contracts/connectors/ConnectGelatoDataForPartialRefinance.sol b/contracts/contracts/connectors/ConnectGelatoDataForPartialRefinance.sol index d0cd0be..0eb2153 100644 --- a/contracts/contracts/connectors/ConnectGelatoDataForPartialRefinance.sol +++ b/contracts/contracts/connectors/ConnectGelatoDataForPartialRefinance.sol @@ -45,6 +45,16 @@ import { } from "../../functions/gelato/FGelatoDebtBridge.sol"; contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { + using GelatoBytes for bytes; + + // vaultId: Id of the unsafe vault of the client. + // token: vault's col token address . + // wMinColRatioMaker: Min col ratio (wad) on Maker debt position + // wMinColRatioB: Min col ratio (wad) on debt position B (e.g. Compound, Maker, ...) + // priceOracle: The price oracle contract to supply the collateral price + // e.g. Maker's ETH/USD oracle for ETH collateral pricing. + // oraclePayload: The data for making the staticcall to the oracle's read + // method e.g. the function selector of MakerOracle's read function. struct PartialDebtBridgePayload { uint256 vaultId; address token; @@ -54,8 +64,6 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { bytes oraclePayload; } - using GelatoBytes for bytes; - // solhint-disable-next-line const-name-snakecase string public constant override name = "ConnectGelatoData-v1.0"; uint256 internal immutable _id; @@ -70,7 +78,7 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { /// @dev Connector Details function connectorID() - public + external view override returns (uint256 _type, uint256 id) @@ -78,10 +86,14 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { (_type, id) = (1, _id); // Should put specific value. } + /// @notice Entry Point for DSA.cast DebtBridge from e.g ETH-A to ETH-B + /// @dev payable to be compatible in conjunction with DSA.cast payable target + /// @param _payload See PartialDebtBridgePayload struct + /// @param _colType colType of the new vault. example : ETH-B, ETH-A. function getDataAndCastForFromMakerToMaker( PartialDebtBridgePayload calldata _payload, string calldata _colType - ) public payable { + ) external payable { ( address[] memory targets, bytes[] memory datas @@ -90,9 +102,12 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { _cast(targets, datas); } + /// @notice Entry Point for DSA.cast DebtBridge from Maker to Compound + /// @dev payable to be compatible in conjunction with DSA.cast payable target + /// @param _payload See PartialDebtBridgePayload struct function getDataAndCastForFromMakerToCompound( PartialDebtBridgePayload calldata _payload - ) public payable { + ) external payable { ( address[] memory targets, bytes[] memory datas @@ -121,97 +136,6 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { /* solhint-disable function-max-lines */ - /// @notice Generate Task for a full refinancing between Maker to Compound. - /// @param _payload contain : - // @param _vaultId Id of the unsafe vault of the client. - // @param _token vault's col token address . - // @param _wMinColRatioMaker Min col ratio (wad) on Maker debt position - // @param _wMinColRatioB Min col ratio (wad) on debt position B (e.g. Compound, Maker, ...) - // @param _priceOracle The price oracle contract to supply the collateral price - // e.g. Maker's ETH/USD oracle for ETH collateral pricing. - // @param _oraclePayload The data for making the staticcall to the oracle's read - // method e.g. the function selector of MakerOracle's read function. - // @param _provider address of the paying provider. - /// @return targets : flashloan contract address - /// @return datas : calldata for flashloan - function _execPayloadForPartialRefinanceFromMakerToCompound( - PartialDebtBridgePayload calldata _payload - ) internal view returns (address[] memory targets, bytes[] memory datas) { - targets = new address[](1); - targets[0] = INSTA_POOL_V2; - - ( - uint256 wDaiDebtToMove, - uint256 wColToWithdrawFromMaker, - uint256 gasFeesPaidFromCol - ) = computeDebtBridge( - _payload.vaultId, - _payload.wMinColRatioMaker, - _payload.wMinColRatioB, - _payload.priceOracle, - _payload.oraclePayload - ); - - address[] memory _targets = new address[](6); - _targets[0] = CONNECT_MAKER; // payback - _targets[1] = CONNECT_MAKER; // withdraw - _targets[2] = CONNECT_COMPOUND; // deposit - _targets[3] = CONNECT_COMPOUND; // borrow - _targets[4] = _connectGelatoProviderPayment; - _targets[5] = INSTA_POOL_V2; - - bytes[] memory _datas = new bytes[](6); - _datas[0] = _encodePaybackMakerVault( - _payload.vaultId, - uint256(-1), - 0, - 0 - ); - _datas[1] = _encodedWithdrawMakerVault( - _payload.vaultId, - uint256(-1), - 0, - 0 - ); - _datas[2] = _encodeDepositCompound( - _payload.token, - sub(wColToWithdrawFromMaker, gasFeesPaidFromCol), - 0, - 0 - ); - _datas[3] = _encodeBorrowCompound(DAI, wDaiDebtToMove, 0, 0); - _datas[4] = _encodePayGelatoProvider( - _payload.token, - gasFeesPaidFromCol, - 0, - 0 - ); - _datas[5] = _encodeFlashPayback(DAI, wDaiDebtToMove, 0, 0); - - datas = new bytes[](1); - datas[0] = abi.encodeWithSelector( - IConnectInstaPoolV2.flashBorrowAndCast.selector, - DAI, - wDaiDebtToMove, - 0, - abi.encode(_targets, _datas) - ); - } - - /// @notice Generate Task for a full refinancing between Maker e.g. ETH-A and ETH-B. - /// @param _payload contain : - // @param _vaultId Id of the unsafe vault of the client. - // @param _token vault's col token address . - // @param _wMinColRatioMaker Min col ratio (wad) on Maker debt position - // @param _wMinColRatioB Min col ratio (wad) on debt position B (e.g. Compound, Maker, ...) - // @param _priceOracle The price oracle contract to supply the collateral price - // e.g. Maker's ETH/USD oracle for ETH collateral pricing. - // @param _oraclePayload The data for making the staticcall to the oracle's read - // method e.g. the function selector of MakerOracle's read function. - // @param _provider address of the paying provider. - /// @param _colType colType of the new vault, exemple : ETH-B, ETH-A. - /// @return targets : flashloan contract address - /// @return datas : calldata for flashloan function _execPayloadForPartialRefinanceFromMakerToMaker( PartialDebtBridgePayload calldata _payload, string calldata _colType @@ -279,6 +203,70 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { ); } + function _execPayloadForPartialRefinanceFromMakerToCompound( + PartialDebtBridgePayload calldata _payload + ) internal view returns (address[] memory targets, bytes[] memory datas) { + targets = new address[](1); + targets[0] = INSTA_POOL_V2; + + ( + uint256 wDaiDebtToMove, + uint256 wColToWithdrawFromMaker, + uint256 gasFeesPaidFromCol + ) = computeDebtBridge( + _payload.vaultId, + _payload.wMinColRatioMaker, + _payload.wMinColRatioB, + _payload.priceOracle, + _payload.oraclePayload + ); + + address[] memory _targets = new address[](6); + _targets[0] = CONNECT_MAKER; // payback + _targets[1] = CONNECT_MAKER; // withdraw + _targets[2] = CONNECT_COMPOUND; // deposit + _targets[3] = CONNECT_COMPOUND; // borrow + _targets[4] = _connectGelatoProviderPayment; + _targets[5] = INSTA_POOL_V2; + + bytes[] memory _datas = new bytes[](6); + _datas[0] = _encodePaybackMakerVault( + _payload.vaultId, + uint256(-1), + 0, + 0 + ); + _datas[1] = _encodedWithdrawMakerVault( + _payload.vaultId, + uint256(-1), + 0, + 0 + ); + _datas[2] = _encodeDepositCompound( + _payload.token, + sub(wColToWithdrawFromMaker, gasFeesPaidFromCol), + 0, + 0 + ); + _datas[3] = _encodeBorrowCompound(DAI, wDaiDebtToMove, 0, 0); + _datas[4] = _encodePayGelatoProvider( + _payload.token, + gasFeesPaidFromCol, + 0, + 0 + ); + _datas[5] = _encodeFlashPayback(DAI, wDaiDebtToMove, 0, 0); + + datas = new bytes[](1); + datas[0] = abi.encodeWithSelector( + IConnectInstaPoolV2.flashBorrowAndCast.selector, + DAI, + wDaiDebtToMove, + 0, + abi.encode(_targets, _datas) + ); + } + /// @notice Computes values needed for DebtBridge Maker->ProtocolB /// @dev Use wad for colRatios. /// @param _vaultId The id of the makerDAO vault. @@ -292,7 +280,6 @@ contract ConnectGelatoDataForPartialRefinance is ConnectorInterface { /// flashBorrow->repay Maker->withdraw from B->flashPayback. /// @return wColToWithdrawFromMaker (wad) to: withdraw from Maker and deposit on B. /// @return gasFeesPaidFromCol Gelato automation-gas-fees paid from user's collateral - // solhint-disable function-max-lines function computeDebtBridge( uint256 _vaultId, uint256 _wMinColRatioMaker, diff --git a/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol b/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol index cfeb40a..f3ef8d9 100644 --- a/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol +++ b/contracts/contracts/connectors/ConnectGelatoProviderPayment.sol @@ -31,8 +31,8 @@ contract ConnectGelatoProviderPayment is constructor(uint256 id, address _gelatoProvider) { _id = id; - gelatoProvider = _gelatoProvider; _this = address(this); + gelatoProvider = _gelatoProvider; } /// @dev Connector Details @@ -55,9 +55,6 @@ contract ConnectGelatoProviderPayment is /// - _getId does not match actual InstaMemory gelatoProvider payment slot /// - _token balance not in DSA /// - worthless _token risk - /// @param _gelatoProvider The Provider who pays the Gelato network for automation. - // This param should be verified / replaced by the ProviderModule in Gelato on-chain. - // In the latter case, it does not matter what address is passed off-chain. /// @param _token The token used to pay the Provider. /// @param _amt The amount of _token to pay the Gelato Provider. /// @param _getId The InstaMemory slot at which the payment amount was stored. @@ -68,16 +65,16 @@ contract ConnectGelatoProviderPayment is uint256 _getId, uint256 _setId ) external payable override { - address _gelatoProvider = IConnectGelatoProviderPayment(_this) + address provider = IConnectGelatoProviderPayment(_this) .gelatoProvider(); require( - _gelatoProvider != address(0x0), - "ConnectGelatoProviderPayment.payProvider:!_gelatoProvider" + provider != address(0x0), + "ConnectGelatoProviderPayment.payProvider:!provider" ); uint256 amt = _getUint(_getId, _amt); _setUint(_setId, amt); _token == ETH - ? payable(_gelatoProvider).sendValue(amt) - : IERC20(_token).safeTransfer(_gelatoProvider, amt); + ? payable(provider).sendValue(amt) + : IERC20(_token).safeTransfer(provider, amt); } } diff --git a/test/unit_tests/5_ConnectGelatoProviderPayment.test.js b/test/unit_tests/5_ConnectGelatoProviderPayment.test.js index 42771fa..b79d9b9 100644 --- a/test/unit_tests/5_ConnectGelatoProviderPayment.test.js +++ b/test/unit_tests/5_ConnectGelatoProviderPayment.test.js @@ -168,9 +168,7 @@ describe("ConnectGelatoProviderPayment Unit Test", function () { value: ethers.utils.parseEther("1"), } ) - ).to.be.revertedWith( - "ConnectGelatoProviderPayment.payProvider:!_gelatoProvider" - ); + ).to.be.revertedWith("ConnectGelatoProviderPayment.payProvider:!provider"); }); it("#2: setProvider should change the provider address", async function () {