diff --git a/contracts/base/connectors/susds/events.sol b/contracts/base/connectors/susds/events.sol new file mode 100644 index 0000000..5e51bf0 --- /dev/null +++ b/contracts/base/connectors/susds/events.sol @@ -0,0 +1,17 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.2; + +contract Events { + event LogSwapExactIn( + address assetIn, + uint256 amountIn, + uint256 getId, + uint256 setId + ); + event LogSwapExactOut( + address assetOut, + uint256 amountOut, + uint256 getId, + uint256 setId + ); +} diff --git a/contracts/base/connectors/susds/helpers.sol b/contracts/base/connectors/susds/helpers.sol new file mode 100644 index 0000000..2fc235f --- /dev/null +++ b/contracts/base/connectors/susds/helpers.sol @@ -0,0 +1,14 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.2; + +import "./interface.sol"; + +contract Helpers { + uint256 internal constant referralCode = 1006; + + address internal constant SUSDSAddr = + 0x5875eEE11Cf8398102FdAd704C9E96607675467a; + + ISparkPSM3 internal constant SparkPSM3 = + ISparkPSM3(0x1601843c5E9bC251A3272907010AFa41Fa18347E); +} diff --git a/contracts/base/connectors/susds/interface.sol b/contracts/base/connectors/susds/interface.sol new file mode 100644 index 0000000..21e48e0 --- /dev/null +++ b/contracts/base/connectors/susds/interface.sol @@ -0,0 +1,46 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.2; + +interface ISparkPSM3 { + function deposit( + address asset, + address receiver, + uint256 assetsToDeposit + ) external returns (uint256 newShares); + + function withdraw( + address asset, + address receiver, + uint256 maxAssetsToWithdraw + ) external returns (uint256 assetsWithdrawn); + + function swapExactIn( + address assetIn, + address assetOut, + uint256 amountIn, + uint256 minAmountOut, + address receiver, + uint256 referralCode + ) external returns (uint256 amountOut); + + function swapExactOut( + address assetIn, + address assetOut, + uint256 amountOut, + uint256 maxAmountIn, + address receiver, + uint256 referralCode + ) external returns (uint256 amountIn); + + function previewSwapExactIn( + address assetIn, + address assetOut, + uint256 amountIn + ) external view returns (uint256 amountOut); + + function previewSwapExactOut( + address assetIn, + address assetOut, + uint256 amountOut + ) external view returns (uint256 amountIn); +} diff --git a/contracts/base/connectors/susds/main.sol b/contracts/base/connectors/susds/main.sol new file mode 100644 index 0000000..6ae37e0 --- /dev/null +++ b/contracts/base/connectors/susds/main.sol @@ -0,0 +1,101 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.2; + +/** + * @title SUSDS Connectors. + * @dev Interface for Spark PSM3 contract for DSA. + */ + +import "./helpers.sol"; +import {Basic} from "../../common/basic.sol"; +import {TokenInterface} from "../../common/basic.sol"; + +contract SUSDSConnector is Helpers, Basic { + /** + * + * @param assetAddr Address of the asset to swap (e.g. USDS, USDC) + * @param amountIn Amount of asset to swap in (e.g. USDS, USDC amount) + * @param getId ID to retrieve amount. + * @param setId ID stores the amount of tokens swapped in. + */ + function swapExactIn( + address assetAddr, + uint256 amountIn, + uint256 getId, + uint256 setId + ) external returns (string memory _eventName, bytes memory _eventParam) { + uint256 _amountIn = getUint(getId, amountIn); + + _amountIn = _amountIn == type(uint256).max + ? TokenInterface(assetAddr).balanceOf(address(this)) + : _amountIn; + + approve(TokenInterface(assetAddr), address(SparkPSM3), _amountIn); + + uint256 minAmountOut = SparkPSM3.previewSwapExactIn( + assetAddr, + SUSDSAddr, + _amountIn + ); + + SparkPSM3.swapExactIn( + assetAddr, + SUSDSAddr, + _amountIn, + minAmountOut, + address(this), + referralCode + ); + + setUint(setId, _amountIn); + + _eventName = "LogSwapExactIn(address,uint256,uint256,uint256)"; + _eventParam = abi.encode(assetAddr, SUSDSAddr, _amountIn, getId); + } + + /** + * + * @param assetAddr Address of the asset to swap (e.g. USDS, USDC) + * @param amountOut Amount of asset to swap out (e.g. USDS, USDC amount) + * @param getId ID to retrieve amount. + * @param setId ID stores the amount of tokens swapped out. + */ + function swapExactOut( + address assetAddr, + uint256 amountOut, + uint256 getId, + uint256 setId + ) external returns (string memory _eventName, bytes memory _eventParam) { + uint256 _amountOut = getUint(getId, amountOut); + + _amountOut = _amountOut == type(uint256).max + ? TokenInterface(assetAddr).balanceOf(address(this)) + : _amountOut; + + uint256 maxAmountIn = SparkPSM3.previewSwapExactOut( + assetAddr, + SUSDSAddr, + _amountOut + ); + + approve(TokenInterface(SUSDSAddr), address(SparkPSM3), maxAmountIn); + + SparkPSM3.swapExactOut( + SUSDSAddr, + assetAddr, + _amountOut, + maxAmountIn, + address(this), + referralCode + ); + + setUint(setId, _amountOut); + + _eventName = "LogSwapExactOut(address,uint256,uint256,uint256)"; + _eventParam = abi.encode(assetAddr, SUSDSAddr, _amountOut, getId); + } +} + +contract ConnectV2SUSDSBase is SUSDSConnector { + string public constant name = "SUSD-Base-v1.0"; +}