mirror of
https://github.com/Instadapp/dsa-polygon-migration.git
synced 2024-07-29 22:27:58 +00:00
contracts done
This commit is contained in:
parent
449f8aeefe
commit
07c5f58c83
|
@ -96,8 +96,11 @@ abstract contract Helpers is DSMath, Stores, Variables {
|
|||
}
|
||||
|
||||
function isPositionSafe() internal returns (bool isOk) {
|
||||
// TODO: Check the final position health
|
||||
// @mubaris we need to add the function from Aave to check the health should be "safeRatioGap"(currently set to 20%) below liquidation
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
(,,,,,uint healthFactor) = aave.getUserAccountData(address(this));
|
||||
// TODO: Check throughly minLimit = 100%/80% = 125% (20% gap initially)
|
||||
uint minLimit = wdiv(1e18, safeRatioGap);
|
||||
isOk = healthFactor > minLimit;
|
||||
require(isOk, "position-at-risk");
|
||||
}
|
||||
|
||||
|
|
|
@ -78,4 +78,8 @@ interface StateSenderInterface {
|
|||
|
||||
interface IndexInterface {
|
||||
function master() external view returns (address);
|
||||
}
|
||||
|
||||
interface FlashloanInterface {
|
||||
function initiateFlashLoan(bytes memory data, uint ethAmt) external;
|
||||
}
|
|
@ -97,13 +97,12 @@ contract LiquidityResolver is Helpers, Events {
|
|||
|
||||
deposits[msg.sender][_token] = sub(maxAmt, _amt);
|
||||
|
||||
// TODO: @everyone check this throughly. Saving 1000 WEI for flashloan WETH. Also, should we make a different contract to handle 2 WEI dydx gas, I think this would be better.
|
||||
if (_token == ethAddr) {
|
||||
TokenInterface _tokenContract = TokenInterface(wethAddr);
|
||||
uint _ethBal = address(this).balance;
|
||||
uint _tknBal = _tokenContract.balanceOf(address(this));
|
||||
if ((_ethBal + _tknBal + 1000) < _amt) {
|
||||
aave.withdraw(wethAddr, sub((_amt + 1000), (_tknBal + _ethBal)), address(this));
|
||||
if ((_ethBal + _tknBal) < _amt) {
|
||||
aave.withdraw(wethAddr, sub(_amt, (_tknBal + _ethBal)), address(this));
|
||||
}
|
||||
_tokenContract.withdraw((sub(_amt, _ethBal)));
|
||||
msg.sender.call{value: _amt}("");
|
||||
|
@ -163,7 +162,7 @@ contract LiquidityResolver is Helpers, Events {
|
|||
}
|
||||
for (uint i = 0; i < _tokens.length; i++) {
|
||||
aave.withdraw(_tokens[i], _amts[i], address(this));
|
||||
// TODO: transfer in polygon's receiver address "polygonReceiver"
|
||||
// TODO: transfer to polygon's receiver address "polygonReceiver"
|
||||
isPositionSafe();
|
||||
}
|
||||
}
|
||||
|
@ -173,7 +172,12 @@ contract LiquidityResolver is Helpers, Events {
|
|||
contract MigrateResolver is LiquidityResolver {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
function _migrate(AaveDataRaw memory _data, address sourceDsa) internal {
|
||||
function _migrate(
|
||||
AaveInterface aave,
|
||||
AaveDataRaw memory _data,
|
||||
address sourceDsa,
|
||||
uint ethAmt
|
||||
) internal {
|
||||
require(_data.supplyTokens.length > 0, "0-length-not-allowed");
|
||||
require(_data.targetDsa != address(0), "invalid-address");
|
||||
require(_data.supplyTokens.length == _data.supplyAmts.length, "invalid-length");
|
||||
|
@ -183,10 +187,9 @@ contract MigrateResolver is LiquidityResolver {
|
|||
"invalid-length"
|
||||
);
|
||||
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
(,,,,,uint healthFactor) = aave.getUserAccountData(sourceDsa);
|
||||
require(healthFactor > 1e18, "position-not-safe");
|
||||
if (ethAmt > 0) {
|
||||
aave.deposit(wethAddr, ethAmt, address(this), 3288);
|
||||
}
|
||||
|
||||
(uint[] memory stableBorrows, uint[] memory variableBorrows, uint[] memory totalBorrows) = _PaybackCalculate(aave, _data, sourceDsa);
|
||||
|
||||
|
@ -209,6 +212,10 @@ contract MigrateResolver is LiquidityResolver {
|
|||
bool isOk = _checkRatio(data);
|
||||
require(isOk, "position-risky-to-migrate");
|
||||
|
||||
if (ethAmt > 0) {
|
||||
aave.withdraw(wethAddr, ethAmt, address(this));
|
||||
}
|
||||
|
||||
isPositionSafe();
|
||||
|
||||
stateSender.syncState(polygonReceiver, abi.encode(data));
|
||||
|
@ -225,40 +232,24 @@ contract MigrateResolver is LiquidityResolver {
|
|||
}
|
||||
|
||||
function migrate(AaveDataRaw calldata _data) external {
|
||||
_migrate(_data, msg.sender);
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
_migrate(aave, _data, msg.sender, 0);
|
||||
}
|
||||
|
||||
function migrateFlashCallback(AaveDataRaw calldata _data, address dsa, uint ethAmt) external {
|
||||
require(false); // TODO: flash loan contract
|
||||
// TODO: deposit ETH in Aave
|
||||
_migrate(_data, dsa);
|
||||
// TODO: withdraw ETH from Aave
|
||||
require(msg.sender == address(flashloanContract), "not-flashloan-contract"); // TODO: flash loan contract
|
||||
AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
|
||||
|
||||
TokenInterface wethContract = TokenInterface(wethAddr);
|
||||
wethContract.approve(address(aave), ethAmt);
|
||||
_migrate(aave, _data, dsa, ethAmt);
|
||||
wethContract.transfer(address(flashloanContract), ethAmt);
|
||||
}
|
||||
|
||||
function migrateWithFlash(AaveDataRaw calldata _data, uint ethAmt) external {
|
||||
bytes memory data = abi.encode(_data, msg.sender, ethAmt);
|
||||
|
||||
// TODO: @everyone integrate dydx flashloan and borrow "ethAmt" and transfer ETH to this address
|
||||
// Should we create a new contract for flashloan or interact directly from this contract? Mainly to resolve 2 WEI bug of dydx flashloan
|
||||
|
||||
flashloanContract.initiateFlashLoan(data, ethAmt);
|
||||
}
|
||||
|
||||
// TODO: @everyone
|
||||
// function callFunction(
|
||||
// address sender,
|
||||
// Account.Info memory account,
|
||||
// bytes memory data
|
||||
// ) public override {
|
||||
// require(sender == address(this), "wrong-sender");
|
||||
// // require(msg.sender == dydxFlash, "wrong-sender"); // TODO: msg.sender should be flashloan contract
|
||||
// (address l2DSA, AaveDataRaw memory _data, address sourceDsa, uint ethAmt) = abi.decode(
|
||||
// data,
|
||||
// (address, AaveDataRaw, address, uint)
|
||||
// );
|
||||
// // TODO: deposit WETH "ethAmt" in Aave
|
||||
// _migrate(l2DSA, _data, sourceDsa);
|
||||
// // TODO: withdraw WETH "ethAmt" from Aave
|
||||
// // TODO: approve WETH "ethAmt + 2" to dydx
|
||||
// }
|
||||
|
||||
}
|
|
@ -4,7 +4,8 @@ import {
|
|||
AaveLendingPoolProviderInterface,
|
||||
AaveDataProviderInterface,
|
||||
StateSenderInterface,
|
||||
IndexInterface
|
||||
IndexInterface,
|
||||
FlashloanInterface
|
||||
} from "./interfaces.sol";
|
||||
|
||||
contract Variables {
|
||||
|
@ -32,11 +33,11 @@ contract Variables {
|
|||
uint16 constant internal referralCode = 3228;
|
||||
|
||||
address constant internal polygonReceiver = address(0); // TODO: Replace this
|
||||
FlashloanInterface constant internal flashloanContract = FlashloanInterface(address(0)); // TODO: Replace this
|
||||
|
||||
// This will be used to have debt/collateral ratio always 20% less than liquidation
|
||||
// TODO: Is this number correct for it?
|
||||
// TODO: Add update function for this
|
||||
uint public safeRatioGap = 20000000000000000; // 20%?
|
||||
uint public safeRatioGap = 80000000000000000; // 20%?
|
||||
uint public fee = 998000000000000000; // 0.2% (99.8%) on collateral? TODO: Is this right?
|
||||
// TODO: Set by construtor?
|
||||
mapping(address => bool) public isSupportedToken;
|
||||
|
|
Loading…
Reference in New Issue
Block a user