minor updates + lint

This commit is contained in:
Shriya Tyagi 2022-08-25 17:40:58 +05:30
parent da5716c0f3
commit cc526d5899
3 changed files with 146 additions and 129 deletions

View File

@ -9,7 +9,9 @@ contract Events {
uint sourceId, uint sourceId,
uint targetId, uint targetId,
address[] supplyTokens, address[] supplyTokens,
uint256[] supplyAmounts,
address[] borrowTokens, address[] borrowTokens,
uint256[] borrowAmounts,
bool[] enterMarket bool[] enterMarket
); );
} }

View File

@ -34,44 +34,52 @@ contract EulerHelpers is Basic {
} }
struct ImportInputData { struct ImportInputData {
address[] supplyTokens; address[] _supplyTokens;
address[] borrowTokens; address[] _borrowTokens;
bool[] enterMarket; bool[] _enterMarket;
} }
struct ImportData { struct ImportData {
address[] _supplyTokens; address[] supplyTokens;
address[] _borrowTokens; address[] borrowTokens;
EulerTokenInterface[] eTokens; EulerTokenInterface[] eTokens;
EulerTokenInterface[] dTokens; EulerTokenInterface[] dTokens;
uint256[] supplyAmts; uint256[] supplyAmts;
uint256[] borrowAmts; uint256[] borrowAmts;
} }
struct ImportHelper {
uint256 supplylength;
uint256 borrowlength;
uint256 totalExecutions;
address sourceAccount;
address targetAccount;
}
function getSupplyAmounts( function getSupplyAmounts(
address userAccount,//user's EOA sub-account address address userAccount,// user's EOA sub-account address
ImportInputData memory inputData, ImportInputData memory inputData,
ImportData memory data ImportData memory data
) internal view returns (ImportData memory) { ) internal view returns (ImportData memory) {
data.supplyAmts = new uint256[](inputData.supplyTokens.length); data.supplyAmts = new uint256[](inputData._supplyTokens.length);
data._supplyTokens = new address[](inputData.supplyTokens.length); data.supplyTokens = new address[](inputData._supplyTokens.length);
data.eTokens = new EulerTokenInterface[](inputData.supplyTokens.length); data.eTokens = new EulerTokenInterface[](inputData._supplyTokens.length);
for (uint256 i = 0; i < inputData.supplyTokens.length; i++) { for (uint256 i = 0; i < inputData._supplyTokens.length; i++) {
for (uint256 j = i; j < inputData.supplyTokens.length; j++) { for (uint256 j = i; j < inputData._supplyTokens.length; j++) {
if (j != i) { if (j != i) {
require( require(
inputData.supplyTokens[i] != inputData.supplyTokens[j], inputData._supplyTokens[i] != inputData._supplyTokens[j],
"token-repeated" "token-repeated"
); );
} }
} }
} }
for (uint256 i = 0; i < inputData.supplyTokens.length; i++) { for (uint256 i = 0; i < inputData._supplyTokens.length; i++) {
address _token = inputData.supplyTokens[i] == ethAddr address _token = inputData._supplyTokens[i] == ethAddr
? wethAddr ? wethAddr
: inputData.supplyTokens[i]; : inputData._supplyTokens[i];
data._supplyTokens[i] = _token; data.supplyTokens[i] = _token;
data.eTokens[i] = EulerTokenInterface(markets.underlyingToEToken(_token)); data.eTokens[i] = EulerTokenInterface(markets.underlyingToEToken(_token));
data.supplyAmts[i] = data.eTokens[i].balanceOf(userAccount);//All 18 dec data.supplyAmts[i] = data.eTokens[i].balanceOf(userAccount);//All 18 dec
} }
@ -80,22 +88,22 @@ contract EulerHelpers is Basic {
} }
function getBorrowAmounts( function getBorrowAmounts(
address userAccount,//user's EOA sub-account address address userAccount,// user's EOA sub-account address
ImportInputData memory inputData, ImportInputData memory inputData,
ImportData memory data ImportData memory data
) internal view returns (ImportData memory) { ) internal view returns (ImportData memory) {
uint _borrowTokensLength = inputData.borrowTokens.length; uint _borrowTokensLength = inputData._borrowTokens.length;
if (_borrowTokensLength > 0) { if (_borrowTokensLength > 0) {
data._borrowTokens = new address[](_borrowTokensLength); data.borrowTokens = new address[](_borrowTokensLength);
data.dTokens = new EulerTokenInterface[](_borrowTokensLength); data.dTokens = new EulerTokenInterface[](_borrowTokensLength);
data.borrowAmts = new uint256[](_borrowTokensLength); data.borrowAmts = new uint256[](_borrowTokensLength);
for (uint256 i = 0; i < _borrowTokensLength; i++) { for (uint256 i = 0; i < _borrowTokensLength; i++) {
for (uint256 j = i; j < _borrowTokensLength; j++) { for (uint256 j = i; j < _borrowTokensLength; j++) {
if (j != i) { if (j != i) {
require( require(
inputData.borrowTokens[i] != inputData._borrowTokens[i] !=
inputData.borrowTokens[j], inputData._borrowTokens[j],
"token-repeated" "token-repeated"
); );
} }
@ -103,11 +111,11 @@ contract EulerHelpers is Basic {
} }
for (uint256 i = 0; i < _borrowTokensLength; i++) { for (uint256 i = 0; i < _borrowTokensLength; i++) {
address _token = inputData.borrowTokens[i] == ethAddr address _token = inputData._borrowTokens[i] == ethAddr
? wethAddr ? wethAddr
: inputData.borrowTokens[i]; : inputData._borrowTokens[i];
data._borrowTokens[i] = _token; data.borrowTokens[i] = _token;
data.dTokens[i] = EulerTokenInterface(markets.underlyingToDToken(_token)); data.dTokens[i] = EulerTokenInterface(markets.underlyingToDToken(_token));
data.borrowAmts[i] = data.dTokens[i].balanceOf(userAccount); data.borrowAmts[i] = data.dTokens[i].balanceOf(userAccount);
} }

View File

@ -6,143 +6,150 @@ import "./interface.sol";
import "./events.sol"; import "./events.sol";
contract EulerImport is EulerHelpers { contract EulerImport is EulerHelpers {
/**
/**
* @dev Import Euler position . * @dev Import Euler position .
* @notice Import EOA's Euler position to DSA's Euler position * @notice Import EOA's Euler position to DSA's Euler position
* @param userAccount The address of the EOA from which position will be imported * @param userAccount EOA address
* @param sourceId sub-account id of EOA from which the funds will be transferred * @param sourceId Sub-account id of "EOA" from which the funds will be transferred
* @param targetId sub-account id of DSA to which the funds will be transferred * @param targetId Sub-account id of "DSA" to which the funds will be transferred
* @param inputData The struct containing all the neccessary input data * @param inputData The struct containing all the neccessary input data
*/ */
function importEuler( function importEuler(
address userAccount, address userAccount,
uint256 sourceId, uint256 sourceId,
uint256 targetId, uint256 targetId,
ImportInputData memory inputData ImportInputData memory inputData
) )
external external
payable payable
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
(_eventName, _eventParam) = _importEuler(userAccount, sourceId, targetId, inputData); (_eventName, _eventParam) = _importEuler(
userAccount,
sourceId,
targetId,
inputData
);
} }
struct importHelper { /**
uint enterMarketLength;
uint supplylength;
uint borrowlength;
uint totalLength;
}
/**
* @dev Import Euler position . * @dev Import Euler position .
* @notice Import EOA's Euler position to DSA's Euler position * @notice Import EOA's Euler position to DSA's Euler position
* @param userAccount The address of the EOA from which position will be imported * @param userAccount EOA address
* @param sourceId sub-account id of EOA from which the funds will be transferred * @param sourceId Sub-account id of "EOA" from which the funds will be transferred
* @param targetId sub-account id of DSA to which the funds will be transferred * @param targetId Sub-account id of "DSA" to which the funds will be transferred
* @param inputData The struct containing all the neccessary input data * @param inputData The struct containing all the neccessary input data
*/ */
function _importEuler( function _importEuler(
address userAccount, address userAccount,
uint256 sourceId, uint256 sourceId,
uint256 targetId, uint256 targetId,
ImportInputData memory inputData ImportInputData memory inputData
) )
internal internal
returns (string memory _eventName, bytes memory _eventParam) returns (string memory _eventName, bytes memory _eventParam)
{ {
importHelper memory helper;
require(inputData._supplyTokens.length > 0, "0-length-not-allowed");
require(
AccountInterface(address(this)).isAuth(userAccount),
"user-account-not-auth"
);
require( require(
AccountInterface(address(this)).isAuth(userAccount), inputData._enterMarket.length == inputData._supplyTokens.length,
"user-account-not-auth" "lengths-not-same"
); );
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
ImportData memory data; ImportData memory data;
ImportHelper memory helper;
helper.enterMarketLength = inputData.enterMarket.length; helper.sourceAccount = getSubAccountAddress(userAccount, sourceId);
helper.targetAccount = getSubAccountAddress(address(this), targetId);
require(helper.enterMarketLength == inputData.supplyTokens.length, "lengths-not-same"); // BorrowAmts will be in underlying token decimals
data = getBorrowAmounts(helper.sourceAccount, inputData, data);
address _sourceAccount = getSubAccountAddress(userAccount, sourceId); // SupplyAmts will be in 18 decimals
address _targetAccount = getSubAccountAddress(address(this), targetId); data = getSupplyAmounts(helper.sourceAccount, inputData, data);
data = getBorrowAmounts(_sourceAccount, inputData, data); helper.supplylength = data.supplyTokens.length;
helper.borrowlength = data.borrowTokens.length;
uint16 enterMarkets = 0;
// In 18 dec for (uint16 i = 0; i < inputData._enterMarket.length; i++) {
data = getSupplyAmounts(_sourceAccount, inputData, data); if (inputData._enterMarket[i]) {
++enterMarkets;
}
}
helper.supplylength = data._supplyTokens.length; helper.totalExecutions = helper.supplylength + enterMarkets + helper.borrowlength;
helper.borrowlength = data._borrowTokens.length;
uint count = 0;
for(uint i = 0; i < helper.enterMarketLength; i++) { IEulerExecute.EulerBatchItem[]
count = inputData.enterMarket[i] ? count++ : count; memory items = new IEulerExecute.EulerBatchItem[](
} helper.totalExecutions
);
helper.totalLength = count + helper.supplylength + helper.borrowlength; uint16 k = 0;
IEulerExecute.EulerBatchItem[] memory items = new IEulerExecute.EulerBatchItem[](helper.totalLength); for (uint16 i = 0; i < helper.supplylength; i++) {
items[k] = IEulerExecute.EulerBatchItem({
allowError: false,
proxyAddr: address(data.eTokens[i]),
data: abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
helper.sourceAccount,
helper.targetAccount,
data.supplyAmts[i]
)
});
k++;
uint k = 0; if (inputData._enterMarket[i]) {
items[k] = IEulerExecute.EulerBatchItem({
allowError: false,
proxyAddr: address(markets),
data: abi.encodeWithSignature(
"enterMarket(uint256,address)",
targetId,
data.supplyTokens[i]
)
});
k++;
}
}
for(uint i = 0; i < helper.supplylength; i++) { for (uint16 j = 0; j < helper.borrowlength; j++) {
items[k] = IEulerExecute.EulerBatchItem({
allowError: false,
proxyAddr: address(data.dTokens[j]),
data: abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
helper.sourceAccount,
helper.targetAccount,
data.borrowAmts[j]
)
});
k++;
}
items[k] = IEulerExecute.EulerBatchItem({ address[] memory deferLiquidityChecks = new address[](2);
allowError: false, deferLiquidityChecks[0] = helper.sourceAccount;
proxyAddr: address(data.eTokens[i]), deferLiquidityChecks[1] = helper.targetAccount;
data: abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
_sourceAccount, _targetAccount, data.supplyAmts[i]
)
});
k++;
if (inputData.enterMarket[i]) { eulerExec.batchDispatch(items, deferLiquidityChecks);
items[k] = IEulerExecute.EulerBatchItem({ _eventName = "LogEulerImport(address,uint,uint,address[],address[],bool[])";
allowError: false,
proxyAddr: address(markets),
data: abi.encodeWithSignature(
"enterMarket(uint256,address)",
targetId, data._supplyTokens[i]
)
});
k++;
}
}
for(uint j = 0; j < helper.borrowlength; j++) {
items[k] = IEulerExecute.EulerBatchItem({
allowError: false,
proxyAddr: address(data.dTokens[j]),
data: abi.encodeWithSignature(
"transferFrom(address,address,uint256)",
_sourceAccount, _targetAccount, data.borrowAmts[j]
)
});
k++;
}
address[] memory deferLiquidityChecks = new address[](2);
deferLiquidityChecks[0] = _sourceAccount;
deferLiquidityChecks[1] = _targetAccount;
eulerExec.batchDispatch(items, deferLiquidityChecks);
_eventName = "LogEulerImport(address,uint,uint,address[],address[],bool[])";
_eventParam = abi.encode( _eventParam = abi.encode(
userAccount, userAccount,
sourceId, sourceId,
targetId, targetId,
inputData.supplyTokens, inputData._supplyTokens,
inputData.borrowTokens, data.supplyAmts,
inputData.enterMarket inputData._borrowTokens,
data.borrowAmts,
inputData._enterMarket
); );
} }
} }
contract ConnectV2EulerImport is EulerImport { contract ConnectV2EulerImport is EulerImport {