mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
new fixes in import
This commit is contained in:
parent
b5675925c8
commit
e0882cb830
|
@ -1,16 +0,0 @@
|
||||||
//SPDX-License-Identifier: MIT
|
|
||||||
pragma solidity ^0.7.0;
|
|
||||||
pragma experimental ABIEncoderV2;
|
|
||||||
|
|
||||||
contract Events {
|
|
||||||
event LogEulerImport(
|
|
||||||
address indexed user,
|
|
||||||
uint256 indexed sourceId,
|
|
||||||
uint256 indexed targetId,
|
|
||||||
address[] supplyTokens,
|
|
||||||
address[] borrowTokens,
|
|
||||||
uint256[] supplyAmts,
|
|
||||||
uint256[] borrowAmts,
|
|
||||||
bool[] enterMarket
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -1,104 +0,0 @@
|
||||||
//SPDX-License-Identifier: MIT
|
|
||||||
pragma solidity ^0.7.0;
|
|
||||||
pragma experimental ABIEncoderV2;
|
|
||||||
import "./helpers.sol";
|
|
||||||
|
|
||||||
contract EulerImport is EulerHelpers {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @dev Import Euler position.
|
|
||||||
* @notice Import EOA's Euler subaccount position to DSA's Euler subaccount
|
|
||||||
* @param userAccount The address of the EOA from which position will be imported
|
|
||||||
* @param sourceId EOA sub-account id from which position be be imported
|
|
||||||
* @param targetId DSA sub-account id
|
|
||||||
* @param inputData The struct containing all the neccessary input data
|
|
||||||
* @param enterMarket The boolean array to enable market in the imported position
|
|
||||||
*/
|
|
||||||
function importEuler(
|
|
||||||
address userAccount,
|
|
||||||
uint256 sourceId,
|
|
||||||
uint256 targetId,
|
|
||||||
ImportInputData memory inputData,
|
|
||||||
bool[] memory enterMarket
|
|
||||||
)
|
|
||||||
external
|
|
||||||
payable
|
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
|
||||||
{
|
|
||||||
(_eventName, _eventParam) = _importEuler(userAccount, sourceId, targetId, inputData, enterMarket);
|
|
||||||
}
|
|
||||||
|
|
||||||
function _importEuler(
|
|
||||||
address userAccount,
|
|
||||||
uint256 sourceId,
|
|
||||||
uint256 targetId,
|
|
||||||
ImportInputData memory inputData,
|
|
||||||
bool[] memory enterMarket
|
|
||||||
)
|
|
||||||
internal
|
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
|
||||||
{
|
|
||||||
require(
|
|
||||||
AccountInterface(address(this)).isAuth(userAccount),
|
|
||||||
"user-account-not-auth"
|
|
||||||
);
|
|
||||||
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
|
|
||||||
require(enterMarket.length == inputData.supplyTokens.length, "lengths-not-same");
|
|
||||||
|
|
||||||
address _sourceAccount = getSubAccountAddress(userAccount, sourceId);
|
|
||||||
address _targetAccount = getSubAccountAddress(address(this), targetId);
|
|
||||||
|
|
||||||
ImportData memory data;
|
|
||||||
|
|
||||||
data = getBorrowAmounts(_sourceAccount, inputData, data);
|
|
||||||
data = getSupplyAmounts(_targetAccount, inputData, data);
|
|
||||||
|
|
||||||
eulerExec.deferLiquidityCheck(_sourceAccount, abi.encode(data, enterMarket, _sourceAccount, _targetAccount, targetId));
|
|
||||||
|
|
||||||
_eventName = "LogEulerImport(address,uint256,uint256,address[],address[],uint256[],uint256[],bool[])";
|
|
||||||
_eventParam = abi.encode(
|
|
||||||
userAccount,
|
|
||||||
sourceId,
|
|
||||||
targetId,
|
|
||||||
inputData.supplyTokens,
|
|
||||||
inputData.borrowTokens,
|
|
||||||
data.supplyAmts,
|
|
||||||
data.borrowAmts,
|
|
||||||
enterMarket
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function onDeferredLiquidityCheck(bytes memory encodedData) external {
|
|
||||||
(
|
|
||||||
ImportData memory data,
|
|
||||||
bool[] memory enterMarket,
|
|
||||||
address _sourceAccount,
|
|
||||||
address _targetAccount,
|
|
||||||
uint targetId
|
|
||||||
) = abi.decode(encodedData, (ImportData, bool[], address, address, uint));
|
|
||||||
|
|
||||||
_TransferEtokens(
|
|
||||||
data._supplyTokens.length,
|
|
||||||
data.eTokens,
|
|
||||||
data.supplyAmts,
|
|
||||||
data._supplyTokens,
|
|
||||||
enterMarket,
|
|
||||||
_sourceAccount,
|
|
||||||
_targetAccount,
|
|
||||||
targetId
|
|
||||||
);
|
|
||||||
|
|
||||||
_TransferDtokens(
|
|
||||||
data._borrowTokens.length,
|
|
||||||
data.dTokens,
|
|
||||||
data.borrowAmts,
|
|
||||||
data._borrowTokens,
|
|
||||||
_sourceAccount,
|
|
||||||
_targetAccount
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
contract ConnectV2EulerImport is EulerImport {
|
|
||||||
string public constant name = "Euler-import-v1.0";
|
|
||||||
}
|
|
15
contracts/mainnet/connectors/euler/euler-import/events.sol
Normal file
15
contracts/mainnet/connectors/euler/euler-import/events.sol
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
contract Events {
|
||||||
|
|
||||||
|
event LogEulerImport (
|
||||||
|
address user,
|
||||||
|
uint sourceId,
|
||||||
|
uint targetId,
|
||||||
|
address[] supplyTokens,
|
||||||
|
address[] borrowTokens,
|
||||||
|
bool[] enterMarket
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
//SPDX-License-Identifier: MIT
|
//SPDX-License-Identifier: MIT
|
||||||
pragma solidity ^0.7.0;
|
pragma solidity ^0.7.0;
|
||||||
pragma experimental ABIEncoderV2;
|
pragma experimental ABIEncoderV2;
|
||||||
import { TokenInterface, AccountInterface } from "../../common/interfaces.sol";
|
import { TokenInterface, AccountInterface } from "../../../common/interfaces.sol";
|
||||||
import { Basic } from "../../common/basic.sol";
|
import { Basic } from "../../../common/basic.sol";
|
||||||
import "./interface.sol";
|
import "./interface.sol";
|
||||||
|
|
||||||
contract EulerHelpers is Basic {
|
contract EulerHelpers is Basic {
|
||||||
|
@ -36,6 +36,7 @@ contract EulerHelpers is Basic {
|
||||||
struct ImportInputData {
|
struct ImportInputData {
|
||||||
address[] supplyTokens;
|
address[] supplyTokens;
|
||||||
address[] borrowTokens;
|
address[] borrowTokens;
|
||||||
|
bool[] enterMarket;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ImportData {
|
struct ImportData {
|
||||||
|
@ -48,7 +49,7 @@ contract EulerHelpers is Basic {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getSupplyAmounts(
|
function getSupplyAmounts(
|
||||||
address userAccount,
|
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) {
|
||||||
|
@ -72,7 +73,7 @@ contract EulerHelpers is Basic {
|
||||||
: 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);
|
data.supplyAmts[i] = data.eTokens[i].balanceOf(userAccount);//All 18 dec
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
@ -113,59 +114,4 @@ contract EulerHelpers is Basic {
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
//transfer and enter market
|
|
||||||
function _TransferEtokens(
|
|
||||||
uint256 _length,
|
|
||||||
EulerTokenInterface[] memory etokenContracts,
|
|
||||||
uint256[] memory amts,
|
|
||||||
address[] memory tokens,
|
|
||||||
bool[] memory enterMarket,
|
|
||||||
address userAccountFrom,
|
|
||||||
address userAccountTo,
|
|
||||||
uint256 targetId
|
|
||||||
) internal {
|
|
||||||
for (uint256 i = 0; i < _length; i++) {
|
|
||||||
if (amts[i] > 0) {
|
|
||||||
uint256 _amt = amts[i];
|
|
||||||
require(
|
|
||||||
etokenContracts[i].transferFrom(
|
|
||||||
userAccountFrom,
|
|
||||||
userAccountTo,
|
|
||||||
_amt
|
|
||||||
),
|
|
||||||
"allowance?"//change
|
|
||||||
);
|
|
||||||
|
|
||||||
if (enterMarket[i]) {
|
|
||||||
markets.enterMarket(targetId, tokens[i]);
|
|
||||||
} else {
|
|
||||||
markets.exitMarket(targetId, tokens[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _TransferDtokens(
|
|
||||||
uint256 _length,
|
|
||||||
EulerTokenInterface[] memory dtokenContracts,
|
|
||||||
uint256[] memory amts,
|
|
||||||
address[] memory tokens,
|
|
||||||
address eoaIdFrom,
|
|
||||||
address dsaIdTo
|
|
||||||
) internal {
|
|
||||||
for (uint256 i = 0; i < _length; i++) {
|
|
||||||
if (amts[i] > 0) {
|
|
||||||
uint256 _amt = amts[i];
|
|
||||||
require(
|
|
||||||
dtokenContracts[i].transferFrom(
|
|
||||||
eoaIdFrom,
|
|
||||||
dsaIdTo,
|
|
||||||
_amt
|
|
||||||
),
|
|
||||||
"debt-transfer-failed?"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -49,7 +49,7 @@ interface IEulerExecute {
|
||||||
bytes result;
|
bytes result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function batchDispatch(EulerBatchItem[] calldata items, address[] calldata deferLiquidityChecks) external returns (EulerBatchItemResponse[] memory);
|
function batchDispatch(EulerBatchItem[] calldata items, address[] calldata deferLiquidityChecks) external;
|
||||||
|
|
||||||
function deferLiquidityCheck(address account, bytes memory data) external;
|
function deferLiquidityCheck(address account, bytes memory data) external;
|
||||||
}
|
}
|
150
contracts/mainnet/connectors/euler/euler-import/main.sol
Normal file
150
contracts/mainnet/connectors/euler/euler-import/main.sol
Normal file
|
@ -0,0 +1,150 @@
|
||||||
|
//SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.7.0;
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
import "./helpers.sol";
|
||||||
|
import "./interface.sol";
|
||||||
|
import "./events.sol";
|
||||||
|
|
||||||
|
contract EulerImport is EulerHelpers {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Import 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 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 inputData The struct containing all the neccessary input data
|
||||||
|
*/
|
||||||
|
function importEuler(
|
||||||
|
address userAccount,
|
||||||
|
uint256 sourceId,
|
||||||
|
uint256 targetId,
|
||||||
|
ImportInputData memory inputData
|
||||||
|
)
|
||||||
|
external
|
||||||
|
payable
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
(_eventName, _eventParam) = _importEuler(userAccount, sourceId, targetId, inputData);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct importHelper {
|
||||||
|
uint enterMarketLength;
|
||||||
|
uint supplylength;
|
||||||
|
uint borrowlength;
|
||||||
|
uint totalLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Import 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 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 inputData The struct containing all the neccessary input data
|
||||||
|
*/
|
||||||
|
function _importEuler(
|
||||||
|
address userAccount,
|
||||||
|
uint256 sourceId,
|
||||||
|
uint256 targetId,
|
||||||
|
ImportInputData memory inputData
|
||||||
|
)
|
||||||
|
internal
|
||||||
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
|
{
|
||||||
|
|
||||||
|
importHelper memory helper;
|
||||||
|
|
||||||
|
require(
|
||||||
|
AccountInterface(address(this)).isAuth(userAccount),
|
||||||
|
"user-account-not-auth"
|
||||||
|
);
|
||||||
|
require(inputData.supplyTokens.length > 0, "0-length-not-allowed");
|
||||||
|
|
||||||
|
ImportData memory data;
|
||||||
|
|
||||||
|
helper.enterMarketLength = inputData.enterMarket.length;
|
||||||
|
|
||||||
|
require(helper.enterMarketLength == inputData.supplyTokens.length, "lengths-not-same");
|
||||||
|
|
||||||
|
address _sourceAccount = getSubAccountAddress(userAccount, sourceId);
|
||||||
|
address _targetAccount = getSubAccountAddress(address(this), targetId);
|
||||||
|
|
||||||
|
data = getBorrowAmounts(_sourceAccount, inputData, data);
|
||||||
|
|
||||||
|
// In 18 dec
|
||||||
|
data = getSupplyAmounts(_sourceAccount, inputData, data);
|
||||||
|
|
||||||
|
helper.supplylength = data._supplyTokens.length;
|
||||||
|
helper.borrowlength = data._borrowTokens.length;
|
||||||
|
uint count = 0;
|
||||||
|
|
||||||
|
for(uint i = 0; i < helper.enterMarketLength; i++) {
|
||||||
|
count = inputData.enterMarket[i] ? count++ : count;
|
||||||
|
}
|
||||||
|
|
||||||
|
helper.totalLength = count + helper.supplylength + helper.borrowlength;
|
||||||
|
|
||||||
|
IEulerExecute.EulerBatchItem[] memory items = new IEulerExecute.EulerBatchItem[](helper.totalLength);
|
||||||
|
|
||||||
|
uint k = 0;
|
||||||
|
|
||||||
|
for(uint i = 0; i < helper.supplylength; i++) {
|
||||||
|
|
||||||
|
items[k] = IEulerExecute.EulerBatchItem({
|
||||||
|
allowError: false,
|
||||||
|
proxyAddr: address(data.eTokens[i]),
|
||||||
|
data: abi.encodeWithSignature(
|
||||||
|
"transferFrom(address,address,uint256)",
|
||||||
|
_sourceAccount, _targetAccount, data.supplyAmts[i]
|
||||||
|
)
|
||||||
|
});
|
||||||
|
k++;
|
||||||
|
|
||||||
|
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 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(
|
||||||
|
userAccount,
|
||||||
|
sourceId,
|
||||||
|
targetId,
|
||||||
|
inputData.supplyTokens,
|
||||||
|
inputData.borrowTokens,
|
||||||
|
inputData.enterMarket
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract ConnectV2EulerImport is EulerImport {
|
||||||
|
string public constant name = "Euler-import-v1.0";
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user