restructuring & updated function till repay

This commit is contained in:
Samyak Jain 2021-06-26 01:40:45 +05:30
parent 03a1f64398
commit 0a2735cad2
3 changed files with 113 additions and 78 deletions

View File

@ -8,14 +8,14 @@ contract Events {
uint maxFeePercentage, uint maxFeePercentage,
uint depositAmount, uint depositAmount,
uint borrowAmount, uint borrowAmount,
uint getId, uint[] getIds,
uint setId uint[] setIds
); );
event LogClose(address indexed borrower, uint setId); event LogClose(address indexed borrower, uint setId);
event LogDeposit(address indexed borrower, uint amount, uint getId); event LogDeposit(address indexed borrower, uint amount, uint getId, uint setId);
event LogWithdraw(address indexed borrower, uint amount, uint setId); event LogWithdraw(address indexed borrower, uint amount, uint getId, uint setId);
event LogBorrow(address indexed borrower, uint amount, uint setId); event LogBorrow(address indexed borrower, uint amount, uint getId, uint setId);
event LogRepay(address indexed borrower, uint amount, uint getId); event LogRepay(address indexed borrower, uint amount, uint getId, uint setId);
event LogAdjust( event LogAdjust(
address indexed borrower, address indexed borrower,
uint maxFeePercentage, uint maxFeePercentage,

View File

@ -3,4 +3,35 @@ pragma solidity ^0.7.6;
import { DSMath } from "../../common/math.sol"; import { DSMath } from "../../common/math.sol";
import { Basic } from "../../common/basic.sol"; import { Basic } from "../../common/basic.sol";
abstract contract Helpers is DSMath, Basic {} import { TokenInterface } from "../../common/interfaces.sol";
import {
BorrowerOperationsLike,
TroveManagerLike,
StabilityPoolLike,
StakingLike,
CollateralSurplusLike,
LqtyTokenLike
} from "./interface.sol";
abstract contract Helpers is DSMath, Basic {
BorrowerOperationsLike internal constant borrowerOperations = BorrowerOperationsLike(0x24179CD81c9e782A4096035f7eC97fB8B783e007);
TroveManagerLike internal constant troveManager = TroveManagerLike(0xA39739EF8b0231DbFA0DcdA07d7e29faAbCf4bb2);
StabilityPoolLike internal constant stabilityPool = StabilityPoolLike(0x66017D22b0f8556afDd19FC67041899Eb65a21bb);
StakingLike internal constant staking = StakingLike(0x4f9Fbb3f1E99B56e0Fe2892e623Ed36A76Fc605d);
CollateralSurplusLike internal constant collateralSurplus = CollateralSurplusLike(0x3D32e8b97Ed5881324241Cf03b2DA5E2EBcE5521);
LqtyTokenLike internal constant lqtyToken = LqtyTokenLike(0x6DEA81C8171D0bA574754EF6F8b412F2Ed88c54D);
TokenInterface internal constant lusdToken = TokenInterface(0x5f98805A4E8be255a32880FDeC7F6728C6568bA0);
// Prevents stack-too-deep error
struct AdjustTrove {
uint maxFeePercentage;
uint withdrawAmount;
uint depositAmount;
uint borrowAmount;
uint repayAmount;
bool isBorrow;
}
}

View File

@ -17,28 +17,7 @@ import { Helpers } from "./helpers.sol";
import { Events } from "./events.sol"; import { Events } from "./events.sol";
abstract contract LiquityResolver is Events, Helpers { abstract contract LiquityResolver is Events, Helpers {
BorrowerOperationsLike internal constant borrowerOperations =
BorrowerOperationsLike(0x24179CD81c9e782A4096035f7eC97fB8B783e007);
TroveManagerLike internal constant troveManager =
TroveManagerLike(0xA39739EF8b0231DbFA0DcdA07d7e29faAbCf4bb2);
StabilityPoolLike internal constant stabilityPool =
StabilityPoolLike(0x66017D22b0f8556afDd19FC67041899Eb65a21bb);
StakingLike internal constant staking =
StakingLike(0x4f9Fbb3f1E99B56e0Fe2892e623Ed36A76Fc605d);
CollateralSurplusLike internal constant collateralSurplus =
CollateralSurplusLike(0x3D32e8b97Ed5881324241Cf03b2DA5E2EBcE5521);
LqtyTokenLike internal constant lqtyToken =
LqtyTokenLike(0x6DEA81C8171D0bA574754EF6F8b412F2Ed88c54D);
// Prevents stack-too-deep error
struct AdjustTrove {
uint maxFeePercentage;
uint withdrawAmount;
uint depositAmount;
uint borrowAmount;
uint repayAmount;
bool isBorrow;
}
/* Begin: Trove */ /* Begin: Trove */
@ -50,8 +29,8 @@ abstract contract LiquityResolver is Events, Helpers {
* @param borrowAmount The amount of LUSD to borrow * @param borrowAmount The amount of LUSD to borrow
* @param upperHint Address of the Trove near the upper bound of where the user's Trove should now sit in the ordered Trove list * @param upperHint Address of the Trove near the upper bound of where the user's Trove should now sit in the ordered Trove list
* @param lowerHint Address of the Trove near the lower bound of where the user's Trove should now sit in the ordered Trove list * @param lowerHint Address of the Trove near the lower bound of where the user's Trove should now sit in the ordered Trove list
* @param getId Optional storage slot to retrieve ETH from * @param getIds Optional (default: 0) Optional storage slot to get deposit & borrow amounts stored using other spells
* @param setId Optional storage slot to store the LUSD borrowed against * @param setIds Optional (default: 0) Optional storage slot to set deposit & borrow amounts to be used in future spells
*/ */
function open( function open(
uint depositAmount, uint depositAmount,
@ -59,26 +38,27 @@ abstract contract LiquityResolver is Events, Helpers {
uint borrowAmount, uint borrowAmount,
address upperHint, address upperHint,
address lowerHint, address lowerHint,
uint getId, uint[] getIds,
uint setId uint[] setIds
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
if (getId != 0 && depositAmount != 0) {
revert("open(): Cannot supply a depositAmount if a non-zero getId is supplied");
}
depositAmount = getUint(getId, depositAmount); uint _depositAmount = getUint(getIds[0], depositAmount);
uint _borrowAmount = getUint(getIds[1], borrowAmount);
borrowerOperations.openTrove{value: depositAmount}( _depositAmount = _depositAmount == uint(-1) ? address(this).balance : _depositAmount;
borrowerOperations.openTrove{value: _depositAmount}(
maxFeePercentage, maxFeePercentage,
borrowAmount, _borrowAmount,
upperHint, upperHint,
lowerHint lowerHint
); );
// Allow other spells to use the borrowed amount setUint(setIds[0], _depositAmount);
setUint(setId, borrowAmount); setUint(setIds[1], _borrowAmount);
_eventName = "LogOpen(address,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, maxFeePercentage, depositAmount, borrowAmount, getId, setId); _eventName = "LogOpen(address,uint256,uint256,uint256,uint256[],uint256[])";
_eventParam = abi.encode(address(this), maxFeePercentage, _depositAmount, _borrowAmount, getIds, setIds);
} }
/** /**
@ -86,14 +66,14 @@ abstract contract LiquityResolver is Events, Helpers {
* @notice Closes a Trove by repaying LUSD debt * @notice Closes a Trove by repaying LUSD debt
* @param setId Optional storage slot to store the ETH withdrawn from the Trove * @param setId Optional storage slot to store the ETH withdrawn from the Trove
*/ */
function close(uint setId) external returns (string memory _eventName, bytes memory _eventParam) { function close(uint setId) external payable returns (string memory _eventName, bytes memory _eventParam) {
uint collateral = troveManager.getTroveColl(address(this)); uint collateral = troveManager.getTroveColl(address(this));
borrowerOperations.closeTrove(); borrowerOperations.closeTrove();
// Allow other spells to use the collateral released from the Trove // Allow other spells to use the collateral released from the Trove
setUint(setId, collateral); setUint(setId, collateral);
_eventName = "LogClose(address,uint256)"; _eventName = "LogClose(address,uint256)";
_eventParam = abi.encode(msg.sender, setId); _eventParam = abi.encode(address(this), setId);
} }
/** /**
@ -103,20 +83,26 @@ abstract contract LiquityResolver is Events, Helpers {
* @param upperHint Address of the Trove near the upper bound of where the user's Trove should now sit in the ordered Trove list * @param upperHint Address of the Trove near the upper bound of where the user's Trove should now sit in the ordered Trove list
* @param lowerHint Address of the Trove near the lower bound of where the user's Trove should now sit in the ordered Trove list * @param lowerHint Address of the Trove near the lower bound of where the user's Trove should now sit in the ordered Trove list
* @param getId Optional storage slot to retrieve the ETH from * @param getId Optional storage slot to retrieve the ETH from
* @param setId Optional storage slot to set the ETH deposited
*/ */
function deposit( function deposit(
uint amount, uint amount,
address upperHint, address upperHint,
address lowerHint, address lowerHint,
uint getId uint getId,
uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
if (getId != 0 && amount != 0) {
revert("deposit(): Cannot supply an amount if a non-zero getId is supplied"); uint _amount = getUint(getId, amount);
}
amount = getUint(getId, amount); _amount = _amount == uint(-1) ? address(this).balance : _amount;
borrowerOperations.addColl{value: amount}(upperHint, lowerHint);
_eventName = "LogDeposit(address,uint256,uint256)"; borrowerOperations.addColl{value: _amount}(upperHint, lowerHint);
_eventParam = abi.encode(msg.sender, amount, getId);
setUint(setId, _amount);
_eventName = "LogDeposit(address,uint256,uint256,uint256)";
_eventParam = abi.encode(address(this), _amount, getId, setId);
} }
/** /**
@ -125,19 +111,25 @@ abstract contract LiquityResolver is Events, Helpers {
* @param amount Amount of ETH to move from Trove to DSA * @param amount Amount of ETH to move from Trove to DSA
* @param upperHint Address of the Trove near the upper bound of where the user's Trove should now sit in the ordered Trove list * @param upperHint Address of the Trove near the upper bound of where the user's Trove should now sit in the ordered Trove list
* @param lowerHint Address of the Trove near the lower bound of where the user's Trove should now sit in the ordered Trove list * @param lowerHint Address of the Trove near the lower bound of where the user's Trove should now sit in the ordered Trove list
* @param getId Optional storage slot to get the amount of ETH to withdraw
* @param setId Optional storage slot to store the withdrawn ETH in * @param setId Optional storage slot to store the withdrawn ETH in
*/ */
function withdraw( function withdraw(
uint amount, uint amount,
address upperHint, address upperHint,
address lowerHint, address lowerHint,
uint getId,
uint setId uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
borrowerOperations.withdrawColl(amount, upperHint, lowerHint); uint _amount = getUint(getId, amount);
setUint(setId, amount); _amount = _amount == uint(-1) ? troveManager.getTroveColl(address(this)) : _amount;
_eventName = "LogWithdraw(address,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, setId); borrowerOperations.withdrawColl(_amount, upperHint, lowerHint);
setUint(setId, _amount);
_eventName = "LogWithdraw(address,uint256,uint256,uint256)";
_eventParam = abi.encode(address(this), _amount, getId, setId);
} }
/** /**
@ -154,13 +146,17 @@ abstract contract LiquityResolver is Events, Helpers {
uint amount, uint amount,
address upperHint, address upperHint,
address lowerHint, address lowerHint,
uint getId,
uint setId uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
borrowerOperations.withdrawLUSD(maxFeePercentage, amount, upperHint, lowerHint); uint _amount = getUint(getId, amount);
setUint(setId, amount); borrowerOperations.withdrawLUSD(maxFeePercentage, _amount, upperHint, lowerHint);
_eventName = "LogBorrow(address,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, setId); setUint(setId, _amount);
_eventName = "LogBorrow(address,uint256,uint256,uint256)";
_eventParam = abi.encode(address(this), _amount, getId, setId);
} }
/** /**
@ -175,15 +171,23 @@ abstract contract LiquityResolver is Events, Helpers {
uint amount, uint amount,
address upperHint, address upperHint,
address lowerHint, address lowerHint,
uint getId uint getId,
uint setId
) external payable returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
if (getId != 0 && amount != 0) { uint _amount = getUint(getId, amount);
revert("repay(): Cannot supply an amount if a non-zero getId is supplied");
if (_amount == uint(-1)) {
uint _lusdBal = lusdToken.balanceOf(address(this));
uint _totalDebt = troveManager.getTroveDebt(address(this));
_amount = _lusdBal > _totalDebt ? _totalDebt : _lusdBal;
} }
amount = getUint(getId, amount);
borrowerOperations.repayLUSD(amount, upperHint, lowerHint); borrowerOperations.repayLUSD(_amount, upperHint, lowerHint);
_eventName = "LogRepay(address,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, getId); setUint(setId, _amount);
_eventName = "LogRepay(address,uint256,uint256,uint256)";
_eventParam = abi.encode(address(this), _amount, getId, setId);
} }
/** /**
@ -245,7 +249,7 @@ abstract contract LiquityResolver is Events, Helpers {
setUint(setBorrowId, borrowAmount); setUint(setBorrowId, borrowAmount);
_eventName = "LogAdjust(address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventName = "LogAdjust(address,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, maxFeePercentage, depositAmount, withdrawAmount, borrowAmount, repayAmount, getDepositId, setWithdrawId, getRepayId, setBorrowId); _eventParam = abi.encode(address(this), maxFeePercentage, depositAmount, withdrawAmount, borrowAmount, repayAmount, getDepositId, setWithdrawId, getRepayId, setBorrowId);
} }
/** /**
@ -253,13 +257,13 @@ abstract contract LiquityResolver is Events, Helpers {
* @param setId Optional storage slot to store the ETH claimed * @param setId Optional storage slot to store the ETH claimed
* @notice Claim remaining collateral from Trove * @notice Claim remaining collateral from Trove
*/ */
function claimCollateralFromRedemption(uint setId) external returns(string memory _eventName, bytes memory _eventParam) { function claimCollateralFromRedemption(uint setId) external payable returns(string memory _eventName, bytes memory _eventParam) {
uint amount = collateralSurplus.getCollateral(address(this)); uint amount = collateralSurplus.getCollateral(address(this));
borrowerOperations.claimCollateral(); borrowerOperations.claimCollateral();
setUint(setId, amount); setUint(setId, amount);
_eventName = "LogClaimCollateralFromRedemption(address,uint256,uint256)"; _eventName = "LogClaimCollateralFromRedemption(address,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, setId); _eventParam = abi.encode(address(this), amount, setId);
} }
/* End: Trove */ /* End: Trove */
@ -280,7 +284,7 @@ abstract contract LiquityResolver is Events, Helpers {
uint getDepositId, uint getDepositId,
uint setEthGainId, uint setEthGainId,
uint setLqtyGainId uint setLqtyGainId
) external returns (string memory _eventName, bytes memory _eventParam) { ) external payable returns (string memory _eventName, bytes memory _eventParam) {
amount = getUint(getDepositId, amount); amount = getUint(getDepositId, amount);
uint ethGain = stabilityPool.getDepositorETHGain(address(this)); uint ethGain = stabilityPool.getDepositorETHGain(address(this));
@ -295,7 +299,7 @@ abstract contract LiquityResolver is Events, Helpers {
setUint(setLqtyGainId, lqtyGain); setUint(setLqtyGainId, lqtyGain);
_eventName = "LogStabilityDeposit(address,uint256,uint256,uint256,address,uint256,uint256,uint256)"; _eventName = "LogStabilityDeposit(address,uint256,uint256,uint256,address,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, ethGain, lqtyGain, frontendTag, getDepositId, setEthGainId, setLqtyGainId); _eventParam = abi.encode(address(this), amount, ethGain, lqtyGain, frontendTag, getDepositId, setEthGainId, setLqtyGainId);
} }
/** /**
@ -325,7 +329,7 @@ abstract contract LiquityResolver is Events, Helpers {
setUint(setLqtyGainId, lqtyGain); setUint(setLqtyGainId, lqtyGain);
_eventName = "LogStabilityWithdraw(address,uint256,uint256,uint256,uint256,uint256,uint256)"; _eventName = "LogStabilityWithdraw(address,uint256,uint256,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, ethGain, lqtyGain, setWithdrawId, setEthGainId, setLqtyGainId); _eventParam = abi.encode(address(this), amount, ethGain, lqtyGain, setWithdrawId, setEthGainId, setLqtyGainId);
} }
/** /**
@ -341,7 +345,7 @@ abstract contract LiquityResolver is Events, Helpers {
uint amount = stabilityPool.getDepositorETHGain(address(this)); uint amount = stabilityPool.getDepositorETHGain(address(this));
stabilityPool.withdrawETHGainToTrove(upperHint, lowerHint); stabilityPool.withdrawETHGainToTrove(upperHint, lowerHint);
_eventName = "LogStabilityMoveEthGainToTrove(address,uint256)"; _eventName = "LogStabilityMoveEthGainToTrove(address,uint256)";
_eventParam = abi.encode(msg.sender, amount); _eventParam = abi.encode(address(this), amount);
} }
/* End: Stability Pool */ /* End: Stability Pool */
@ -370,7 +374,7 @@ abstract contract LiquityResolver is Events, Helpers {
setUint(setLusdGainId, lusdGain); setUint(setLusdGainId, lusdGain);
_eventName = "LogStake(address,uint256,uint256,uint256,uint256)"; _eventName = "LogStake(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, getStakeId, setEthGainId, setLusdGainId); _eventParam = abi.encode(address(this), amount, getStakeId, setEthGainId, setLusdGainId);
} }
/** /**
@ -396,7 +400,7 @@ abstract contract LiquityResolver is Events, Helpers {
setUint(setLusdGainId, lusdGain); setUint(setLusdGainId, lusdGain);
_eventName = "LogUnstake(address,uint256,uint256,uint256,uint256)"; _eventName = "LogUnstake(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, amount, setStakeId, setEthGainId, setLusdGainId); _eventParam = abi.encode(address(this), amount, setStakeId, setEthGainId, setLusdGainId);
} }
/** /**
@ -418,7 +422,7 @@ abstract contract LiquityResolver is Events, Helpers {
setUint(setLusdGainId, lusdGain); setUint(setLusdGainId, lusdGain);
_eventName = "LogClaimStakingGains(address,uint256,uint256,uint256,uint256)"; _eventName = "LogClaimStakingGains(address,uint256,uint256,uint256,uint256)";
_eventParam = abi.encode(msg.sender, ethGain, lusdGain, setEthGainId, setLusdGainId); _eventParam = abi.encode(address(this), ethGain, lusdGain, setEthGainId, setLusdGainId);
} }
/* End: Staking */ /* End: Staking */