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

58 lines
1.5 KiB
Solidity
Raw Normal View History

2022-06-30 23:43:20 +00:00
//SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
2022-07-07 18:51:06 +00:00
import "./interface.sol";
2022-07-06 01:07:56 +00:00
import "./events.sol";
2022-06-30 23:43:20 +00:00
import { Basic } from "../../common/basic.sol";
2022-07-07 18:51:06 +00:00
contract Helpers is Basic, Events {
address internal constant EULER_MAINNET =
0x27182842E098f60e3D576794A5bFFb0777E025d3;
IEulerMarkets internal constant markets =
IEulerMarkets(0x3520d5a913427E6F0D6A83E07ccD4A4da316e4d3);
2022-07-06 13:52:01 +00:00
/**
* @dev Get sub account address
* @param primary address of user
2022-08-14 10:34:45 +00:00
* @param subAccountId sub-account id
2022-07-06 13:52:01 +00:00
*/
2022-07-06 13:40:38 +00:00
function getSubAccount(address primary, uint256 subAccountId)
public
pure
returns (address)
{
require(subAccountId < 256, "sub-account-id-too-big");
return address(uint160(primary) ^ uint160(subAccountId));
}
2022-07-06 01:07:56 +00:00
2022-08-14 10:34:45 +00:00
/**
* @dev Get Enetered markets for a user
* @param subAccountId sub-account id
*/
function getEnteredMarkets(uint256 subAccountId)
internal
view
returns (address[] memory enteredMarkets)
{
address _subAccountAddress = getSubAccount(address(this), subAccountId);
enteredMarkets = markets.getEnteredMarkets(_subAccountAddress);
}
2022-07-06 13:52:01 +00:00
/**
* @dev Check if the market is entered
2022-08-14 10:34:45 +00:00
* @param subAccountId sub-account id
2022-07-06 13:52:01 +00:00
* @param token token address
*/
2022-08-14 10:34:45 +00:00
function checkIfEnteredMarket(uint256 subAccountId, address token) public view returns (bool) {
address[] memory enteredMarkets = getEnteredMarkets(subAccountId);
2022-07-06 01:07:56 +00:00
uint256 length = enteredMarkets.length;
2022-07-06 13:40:38 +00:00
for (uint256 i = 0; i < length; i++) {
if (enteredMarkets[i] == token) {
2022-07-06 01:07:56 +00:00
return true;
}
}
return false;
}
2022-06-30 23:43:20 +00:00
}