Added settle function in L1

This commit is contained in:
Samyak Jain 2021-04-09 04:35:20 +05:30
parent 47d669b666
commit 1a85a15551
2 changed files with 40 additions and 10 deletions

View File

@ -124,15 +124,45 @@ contract LiquidityResolver is Helpers, Events {
emit LogWithdraw(msg.sender, tokens, _amts); emit LogWithdraw(msg.sender, tokens, _amts);
} }
// TODO: @mubaris Things to factor /**
// use supportedTokens array to loop through * @param _tokens - array of tokens to transfer to L2 receiver's contract
// If there is same token supply and borrow, then close the smaller one * @param _amts - array of token amounts to transfer to L2 receiver's contract
// 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(address[] calldata _tokens, uint[] _amts) external {
// If ratio is safe then transfer excess collateral to L2 migration contract AaveInterface aave = AaveInterface(aaveProvider.getLendingPool());
// Always, keep 1000 wei WETH ideal for flashloan for (uint i = 0; i < supportedTokens.length; i++) {
function settle() external { address _token = supportedTokens[i];
if (_token == ethAddr) {
_token = wethAddr;
if (address(this).balance > 0) {
TokenInterface(wethAddr).deposit(address(this).balance);
}
}
IERC20 _tokenContract = IERC20(_token);
uint _tokenBal = _tokenContract.balanceOf(address(this));
(
uint supplyBal,,
uint borrowBal,
,,,,,
) = aaveData.getUserReserveData(_token, address(this));
if (supplyBal != 0 && borrowBal != 0) {
uint _withdrawAmt;
if (supplyBal > borrowBal) {
aave.withdraw(_token, borrowBal, address(this)); // TODO: fail because of not enough withdrawing capacity?
IERC20(_token).approve(address(aave), borrowBal);
aave.repay(_token, borrowBal, 2, address(this));
} else {
aave.withdraw(_token, supplyBal, address(this)); // TODO: fail because of not enough withdrawing capacity?
IERC20(_token).approve(address(aave), borrowBal);
aave.repay(_token, supplyBal, 2, address(this));
}
}
}
for (uint i = 0; i < _tokens.length; i++) {
aave.withdraw(_tokens[i], _amts[i], address(this));
// TODO: transfer in polygon's receiver address "polygonReceiver"
isPositionSafe();
}
} }
} }

View File

@ -31,7 +31,7 @@ contract Variables {
*/ */
uint16 constant internal referralCode = 3228; uint16 constant internal referralCode = 3228;
address constant internal polygonReceiver = address(2); // Replace this address constant internal polygonReceiver = address(0); // Replace this
// This will be used to have debt/collateral ratio always 20% less than liquidation // This will be used to have debt/collateral ratio always 20% less than liquidation
// TODO: Is this number correct for it? // TODO: Is this number correct for it?