dsa-connectors/contracts/mainnet/connectors/euler/import/helpers.sol

112 lines
3.1 KiB
Solidity
Raw Normal View History

2022-08-03 16:21:00 +00:00
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
2022-08-24 23:56:44 +00:00
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
import { Basic } from "../../../common/basic.sol";
2022-08-03 16:21:00 +00:00
import "./interface.sol";
contract EulerHelpers is Basic {
2022-08-03 17:32:08 +00:00
/**
* @dev Euler's Market Module
*/
2022-08-26 10:06:30 +00:00
IEulerMarkets internal constant markets =
2022-08-03 16:21:00 +00:00
IEulerMarkets(0x3520d5a913427E6F0D6A83E07ccD4A4da316e4d3);
2022-08-03 17:32:08 +00:00
/**
* @dev Euler's Execution Module
*/
2022-08-26 10:06:30 +00:00
IEulerExecute internal constant eulerExec =
IEulerExecute(0x59828FdF7ee634AaaD3f58B19fDBa3b03E2D9d80);
2022-08-03 16:21:00 +00:00
2022-08-03 17:32:08 +00:00
/**
* @dev Compute sub account address.
* @notice Compute sub account address from sub-account id
* @param primary primary address
2022-08-26 10:06:30 +00:00
* @param subAccountId sub-account id whose address needs to be computed
2022-08-03 17:32:08 +00:00
*/
2022-08-03 16:21:00 +00:00
function getSubAccountAddress(address primary, uint256 subAccountId)
public
pure
returns (address)
{
require(subAccountId < 256, "sub-account-id-too-big");
return address(uint160(primary) ^ uint160(subAccountId));
}
2022-08-26 10:06:30 +00:00
struct ImportInputData {
2022-08-25 12:10:58 +00:00
address[] _supplyTokens;
address[] _borrowTokens;
bool[] _enterMarket;
2022-08-03 16:21:00 +00:00
}
2022-08-26 10:06:30 +00:00
struct ImportData {
2022-08-25 12:10:58 +00:00
address[] supplyTokens;
address[] borrowTokens;
2022-08-03 16:21:00 +00:00
EulerTokenInterface[] eTokens;
2022-08-26 10:06:30 +00:00
EulerTokenInterface[] dTokens;
2022-08-03 16:21:00 +00:00
uint256[] supplyAmts;
uint256[] borrowAmts;
}
2022-08-25 12:10:58 +00:00
struct ImportHelper {
uint256 supplylength;
uint256 borrowlength;
uint256 totalExecutions;
address sourceAccount;
address targetAccount;
}
2022-08-26 10:06:30 +00:00
function getSupplyAmounts(
address userAccount, // user's EOA sub-account address
2022-08-03 16:21:00 +00:00
ImportInputData memory inputData,
ImportData memory data
) internal view returns (ImportData memory) {
2022-08-25 12:10:58 +00:00
data.supplyAmts = new uint256[](inputData._supplyTokens.length);
data.supplyTokens = new address[](inputData._supplyTokens.length);
2022-08-26 10:06:30 +00:00
data.eTokens = new EulerTokenInterface[](
inputData._supplyTokens.length
);
uint256 length_ = inputData._supplyTokens.length;
2022-08-26 17:15:25 +00:00
for (uint256 i = 0; i < length_; i++) {
2022-08-26 17:15:25 +00:00
address token_ = inputData._supplyTokens[i] == ethAddr
2022-08-03 16:21:00 +00:00
? wethAddr
2022-08-25 12:10:58 +00:00
: inputData._supplyTokens[i];
2022-08-26 17:15:25 +00:00
data.supplyTokens[i] = token_;
2022-08-26 10:06:30 +00:00
data.eTokens[i] = EulerTokenInterface(
2022-08-26 17:15:25 +00:00
markets.underlyingToEToken(token_)
2022-08-26 10:06:30 +00:00
);
data.supplyAmts[i] = data.eTokens[i].balanceOf(userAccount); //All 18 dec
2022-08-03 16:21:00 +00:00
}
return data;
}
2022-08-26 10:06:30 +00:00
function getBorrowAmounts(
address userAccount, // user's EOA sub-account address
2022-08-03 16:21:00 +00:00
ImportInputData memory inputData,
ImportData memory data
) internal view returns (ImportData memory) {
uint256 borrowTokensLength_ = inputData._borrowTokens.length;
2022-08-04 16:20:17 +00:00
if (borrowTokensLength_ > 0) {
2022-08-26 17:15:25 +00:00
data.borrowTokens = new address[](borrowTokensLength_);
data.dTokens = new EulerTokenInterface[](borrowTokensLength_);
data.borrowAmts = new uint256[](borrowTokensLength_);
2022-08-04 16:20:17 +00:00
2022-08-26 17:15:25 +00:00
for (uint256 i = 0; i < borrowTokensLength_; i++) {
2022-08-25 12:10:58 +00:00
address _token = inputData._borrowTokens[i] == ethAddr
2022-08-03 16:21:00 +00:00
? wethAddr
2022-08-25 12:10:58 +00:00
: inputData._borrowTokens[i];
2022-08-03 16:21:00 +00:00
2022-08-25 12:10:58 +00:00
data.borrowTokens[i] = _token;
2022-08-26 10:06:30 +00:00
data.dTokens[i] = EulerTokenInterface(
markets.underlyingToDToken(_token)
);
data.borrowAmts[i] = data.dTokens[i].balanceOf(userAccount);
2022-08-03 16:21:00 +00:00
}
}
return data;
}
2022-08-04 16:20:17 +00:00
}