Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add susds spark psm connector #47

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions contracts/base/connectors/susds/events.sol
Original file line number Diff line number Diff line change
@@ -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
);
}
14 changes: 14 additions & 0 deletions contracts/base/connectors/susds/helpers.sol
Original file line number Diff line number Diff line change
@@ -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);
}
46 changes: 46 additions & 0 deletions contracts/base/connectors/susds/interface.sol
Original file line number Diff line number Diff line change
@@ -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);
}
101 changes: 101 additions & 0 deletions contracts/base/connectors/susds/main.sol
Original file line number Diff line number Diff line change
@@ -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";
}