mirror of
https://github.com/Instadapp/dsa-polygon-migration.git
synced 2024-07-29 22:27:58 +00:00
Logic structuring done
This commit is contained in:
parent
2a58cb9ff5
commit
5812e992fc
|
@ -11,8 +11,18 @@ import { Helpers } from "./helpers.sol";
|
||||||
contract MigrateResolver is Helpers, Events {
|
contract MigrateResolver is Helpers, Events {
|
||||||
using SafeERC20 for IERC20;
|
using SafeERC20 for IERC20;
|
||||||
|
|
||||||
mapping (address => AaveData) public positions;
|
// This will be used to have debt/collateral ratio always 20% less than liquidation
|
||||||
|
// TODO: Is this number correct for it?
|
||||||
|
uint public safeRatioGap = 20000000000000000; // 20%?
|
||||||
|
|
||||||
|
// dsa => position
|
||||||
|
mapping(uint => AaveData) public positions;
|
||||||
mapping(address => mapping(address => uint)) deposits;
|
mapping(address => mapping(address => uint)) deposits;
|
||||||
|
// TODO: Add function for flash deposits and withdraw
|
||||||
|
mapping(address => mapping(address => uint)) flashDeposits; // Flash deposits of particular token
|
||||||
|
mapping(address => uint) flashAmts; // token amount for flashloan usage (these token will always stay raw in this contract)
|
||||||
|
// TODO: need to add function to add this mapping
|
||||||
|
mapping(address => address) tokensL1L2; // L1 tokens mapping to L2 tokens. Eg:- L1 DAI to L2 DAI
|
||||||
|
|
||||||
// InstaIndex Address.
|
// InstaIndex Address.
|
||||||
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
|
||||||
|
@ -33,6 +43,7 @@ contract MigrateResolver is Helpers, Events {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Deposit in Aave
|
||||||
function deposit(address[] calldata tokens, uint[] calldata amts) external {
|
function deposit(address[] calldata tokens, uint[] calldata amts) external {
|
||||||
uint _length = tokens.length;
|
uint _length = tokens.length;
|
||||||
require(_length == amts.length, "invalid-length");
|
require(_length == amts.length, "invalid-length");
|
||||||
|
@ -53,6 +64,7 @@ contract MigrateResolver is Helpers, Events {
|
||||||
emit LogDeposit(msg.sender, tokens, _amts);
|
emit LogDeposit(msg.sender, tokens, _amts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Withdraw from Aave
|
||||||
function withdraw(address[] calldata tokens, uint[] calldata amts) external {
|
function withdraw(address[] calldata tokens, uint[] calldata amts) external {
|
||||||
uint _length = tokens.length;
|
uint _length = tokens.length;
|
||||||
require(_length == amts.length, "invalid-length");
|
require(_length == amts.length, "invalid-length");
|
||||||
|
@ -84,26 +96,70 @@ contract AaveV2Migrator is MigrateResolver {
|
||||||
|
|
||||||
uint private lastStateId;
|
uint private lastStateId;
|
||||||
|
|
||||||
function _migratePosition(AaveData memory data) internal {
|
function _migratePosition(AaveData memory _data) internal {
|
||||||
AaveData storage data = positions[owner];
|
AaveData storage data = _data;
|
||||||
|
|
||||||
// require(!data.isFinal, "already-migrated");
|
address dsa = _data.targetDsa;
|
||||||
|
address[] memory supplyAmts = _data.supplyAmts;
|
||||||
|
address[] memory borrowAmts = _data.borrowAmts;
|
||||||
|
address[] memory supplyTokens = _data.supplyTokens;
|
||||||
|
address[] memory borrowTokens = _data.borrowTokens;
|
||||||
|
|
||||||
|
for (uint i = 0; i < supplyTokens.length; i++) {
|
||||||
|
address _token = tokensL1L2[supplyTokens[i]];
|
||||||
|
IERC20 _atokenContract = IERC20(_token); // TODO: Fetch atoken from Aave mapping (change _token to atoken address)
|
||||||
|
uint _atokenBal = _atokenContract.balanceOf(address(this));
|
||||||
|
uint _supplyAmt = supplyAmts[i];
|
||||||
|
if (_atokenBal < _supplyAmt) {
|
||||||
|
// TODO: Loop in the token. by borrow & supply to desirable amount
|
||||||
|
// Things to take into consideration:
|
||||||
|
// number of loops (have to check from liquidity available in Aave & accounts borrowing limit with every loop it'll decrease)
|
||||||
|
}
|
||||||
|
_atokenContract.safeTransfer(dsa, _supplyAmt);
|
||||||
|
}
|
||||||
|
|
||||||
// for (uint i = 0; i < data.supplyTokens.length; i++) {
|
// Have to borrow from user's account
|
||||||
// IERC20(data.supplyTokens[i]).safeTransfer(data.targetDsa, data.supplyAmts[i]);
|
for (uint i = 0; i < borrowTokens.length; i++) {
|
||||||
// }
|
address _token = tokensL1L2[borrowTokens[i]];
|
||||||
|
// get Aave liquidity of token
|
||||||
|
uint tokenLiq = uint(0);
|
||||||
|
uint _borrowAmt = borrowAmts[i];
|
||||||
|
if (tokenLiq < _borrowAmt) {
|
||||||
|
// deposit flash amt in Aave
|
||||||
|
uint _flashAmt = flashAmts[_token];
|
||||||
|
// TODO: deposit in Aave
|
||||||
|
tokenLiq += _flashAmt;
|
||||||
|
}
|
||||||
|
// TODO: Check number of loops needed. Borrow and supply on user's account.
|
||||||
|
uint num = _borrowAmt/tokenLiq + 1; // TODO: Is this right
|
||||||
|
uint splitAmt = _borrowAmt/num; // TODO: Check decimal
|
||||||
|
uint finalSplit = _borrowAmt - (splitAmt * (num - 1)); // TODO: to resolve upper decimal error
|
||||||
|
|
||||||
// AccountInterface(data.targetDsa).migrateAave(owner);
|
uint spellsAmt = num + 1;
|
||||||
// data.isFinal = true;
|
string[] memory targets = string[](spellsAmt);
|
||||||
|
bytes[] memory castData = bytes[](spellsAmt);
|
||||||
|
for (uint j = 0; j < num; j++) {
|
||||||
|
targets[j] = "AAVE-A";
|
||||||
|
if (i < num - 1) {
|
||||||
|
castData[j] = bytes(0); // encode the cast data & use splitAmt
|
||||||
|
} else {
|
||||||
|
castData[j] = bytes(0); // encode the cast data & use finalSplit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
targets[spellsAmt] = "BASIC-A"; // TODO: right spell?
|
||||||
|
castData[spellsAmt] = bytes(0); // encode the data of atoken withdrawal
|
||||||
|
// TODO: Call DSAs cast and borrow (maybe create a new implementation which only this contract can run?)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Final position should be 20% less than liquidation (use 'safeRatioGap', Also should we check this at start?)
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPosition(address owner) public view returns (AaveData memory data) {
|
function getPosition(address owner) public view returns (AaveData memory data) {
|
||||||
data = positions[owner];
|
data = positions[owner];
|
||||||
}
|
}
|
||||||
|
|
||||||
function canMigrate(address owner) public view returns (bool can) {
|
// have to add more conditions
|
||||||
|
function canMigrate(AaveData calldata data) public view returns (bool can) {
|
||||||
can = true;
|
can = true;
|
||||||
|
|
||||||
AaveData memory data = getPosition(owner);
|
AaveData memory data = getPosition(owner);
|
||||||
|
@ -117,20 +173,23 @@ contract AaveV2Migrator is MigrateResolver {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onStateReceive(uint256 stateId, bytes calldata receivedData) external {
|
function onStateReceive(uint256 stateId, bytes calldata receivedData) external {
|
||||||
// require(stateId > lastStateId, "wrong-data");
|
require(stateId > lastStateId, "wrong-data");
|
||||||
lastStateId = stateId;
|
lastStateId = stateId;
|
||||||
|
|
||||||
(address dsa, AaveData memory data) = abi.decode(receivedData, (address, AaveData));
|
(AaveData memory data) = abi.decode(receivedData, (AaveData));
|
||||||
// positions[owner] = remapTokens(data);
|
positions[stateId] = data; // TODO: what's the best way to store user's data to create position later
|
||||||
|
|
||||||
// if (canMigrate(owner)) {
|
|
||||||
_migratePosition(dsa, data);
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// function migrate(address owner) external {
|
function migrate(uint _id) external {
|
||||||
// require(canMigrate(owner), "not-enough-liquidity");
|
AaveData memory data = positions[_id];
|
||||||
|
|
||||||
// _migratePosition(owner);
|
require(data != AaveData(0), "already-migrated");
|
||||||
// }
|
|
||||||
|
require(canMigrate(data), "not-enough-liquidity"); // TODO: do we need this? as we can see if transaction will go through or not from our bot
|
||||||
|
|
||||||
|
_migratePosition(data);
|
||||||
|
|
||||||
|
delete positions[_id];
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user