dsa-polygon-migration/contracts/receivers2/aave-v2-receiver/main.sol

158 lines
5.2 KiB
Solidity
Raw Normal View History

2021-04-07 00:22:28 +00:00
pragma solidity ^0.7.0;
2021-04-05 00:33:23 +00:00
pragma experimental ABIEncoderV2;
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
2021-04-07 00:22:28 +00:00
import { AccountInterface, AaveData, IndexInterface } from "./interfaces.sol";
2021-04-05 00:33:23 +00:00
import { Events } from "./events.sol";
import { Helpers } from "./helpers.sol";
contract MigrateResolver is Helpers, Events {
using SafeERC20 for IERC20;
2021-04-05 23:33:34 +00:00
// dsa => position
2021-04-07 00:22:28 +00:00
mapping(uint => bytes) public positions;
2021-04-07 15:28:21 +00:00
mapping(address => mapping(address => uint)) public deposits;
2021-04-05 00:33:23 +00:00
// InstaIndex Address.
IndexInterface public constant instaIndex = IndexInterface(0x2971AdFa57b20E5a416aE5a708A8655A9c74f723);
function spell(address _target, bytes memory _data) external {
require(msg.sender == instaIndex.master(), "not-master");
require(_target != address(0), "target-invalid");
assembly {
let succeeded := delegatecall(gas(), _target, add(_data, 0x20), mload(_data), 0, 0)
switch iszero(succeeded)
case 1 {
// throw if delegatecall failed
let size := returndatasize()
returndatacopy(0x00, 0x00, size)
revert(0x00, size)
}
}
}
2021-04-05 23:33:34 +00:00
// TODO: Deposit in Aave
2021-04-05 00:33:23 +00:00
function deposit(address[] calldata tokens, uint[] calldata amts) external {
uint _length = tokens.length;
require(_length == amts.length, "invalid-length");
uint[] memory _amts = new uint[](_length);
for (uint256 i = 0; i < _length; i++) {
address _token = tokens[i];
IERC20 tokenContract = IERC20(_token);
uint _amt = amts[i] == uint(-1) ? tokenContract.balanceOf(msg.sender) : amts[i];
tokenContract.safeTransferFrom(msg.sender, address(this), _amt);
deposits[msg.sender][_token] += _amt;
_amts[i] = _amt;
}
emit LogDeposit(msg.sender, tokens, _amts);
}
2021-04-05 23:33:34 +00:00
// TODO: Withdraw from Aave
2021-04-05 00:33:23 +00:00
function withdraw(address[] calldata tokens, uint[] calldata amts) external {
uint _length = tokens.length;
require(_length == amts.length, "invalid-length");
uint[] memory _amts = new uint[](_length);
for (uint256 i = 0; i < _length; i++) {
uint _amt = amts[i];
address _token = tokens[i];
uint maxAmt = deposits[msg.sender][_token];
if (_amt > maxAmt) {
_amt = maxAmt;
}
IERC20(_token).safeTransfer(msg.sender, _amt);
deposits[msg.sender][_token] = sub(maxAmt, _amt);
_amts[i] = _amt;
}
2021-04-07 15:28:21 +00:00
isPositionSafe();
2021-04-05 00:33:23 +00:00
emit LogWithdraw(msg.sender, tokens, _amts);
}
2021-04-07 15:28:21 +00:00
// TODO: Things to factor
// If there is same token supply and borrow, then close the smaller one
// If there is ideal token then payback or deposit according to the position
// Object is the decrease the ratio and pay as less interest
function settle() external {
}
2021-04-05 00:33:23 +00:00
}
contract AaveV2Migrator is MigrateResolver {
using SafeERC20 for IERC20;
uint private lastStateId;
2021-04-05 23:33:34 +00:00
function _migratePosition(AaveData memory _data) internal {
2021-04-07 00:22:28 +00:00
AaveData memory data = remapTokens(_data); // converting L1 token addresses to L2 addresses
2021-04-05 23:33:34 +00:00
address dsa = _data.targetDsa;
2021-04-07 00:22:28 +00:00
uint[] memory supplyAmts = _data.supplyAmts;
uint[] memory borrowAmts = _data.borrowAmts;
2021-04-05 23:33:34 +00:00
address[] memory supplyTokens = _data.supplyTokens;
address[] memory borrowTokens = _data.borrowTokens;
2021-04-07 00:22:28 +00:00
transferAtokens(dsa, supplyTokens, supplyAmts);
2021-04-05 00:33:23 +00:00
2021-04-05 23:33:34 +00:00
// Have to borrow from user's account
2021-04-07 00:22:28 +00:00
borrowAndTransferSpells(dsa, borrowTokens, borrowAmts);
2021-04-05 00:33:23 +00:00
2021-04-07 15:28:21 +00:00
isPositionSafe();
2021-04-05 00:33:23 +00:00
}
2021-04-07 00:22:28 +00:00
// function getPosition(address owner) public view returns (AaveData memory data) {
// data = positions[owner];
// }
2021-04-05 00:33:23 +00:00
2021-04-07 00:22:28 +00:00
// TODO: have to add more conditions
function canMigrate(AaveData memory data) public view returns (bool can) {
// can = true;
2021-04-05 00:33:23 +00:00
2021-04-07 00:22:28 +00:00
// AaveData memory data = getPosition(owner);
2021-04-05 00:33:23 +00:00
2021-04-07 00:22:28 +00:00
// for (uint i = 0; i < data.supplyTokens.length; i++) {
// IERC20 token = IERC20(data.supplyTokens[i]);
// if (token.balanceOf(address(this)) < data.supplyAmts[i]) {
// can = false;
// }
// }
2021-04-05 00:33:23 +00:00
}
function onStateReceive(uint256 stateId, bytes calldata receivedData) external {
2021-04-05 23:33:34 +00:00
require(stateId > lastStateId, "wrong-data");
2021-04-05 00:33:23 +00:00
lastStateId = stateId;
2021-04-07 00:22:28 +00:00
// (AaveData memory data) = abi.decode(receivedData, (AaveData));
positions[stateId] = receivedData; // TODO: what's the best way to store user's data to create position later
2021-04-05 00:33:23 +00:00
}
2021-04-05 23:33:34 +00:00
function migrate(uint _id) external {
2021-04-07 00:22:28 +00:00
bytes memory _data = positions[_id];
require(_data != bytes(0), "already-migrated"); // TODO: How to resolve this
AaveData memory data = abi.decode(_data, (AaveData));
2021-04-05 00:33:23 +00:00
2021-04-05 23:33:34 +00:00
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];
}
}