Skip to content

Commit

Permalink
Merge pull request #51 from mysofinance/aetienne/update-events-2
Browse files Browse the repository at this point in the history
Aetienne/update events 2
  • Loading branch information
jpick713 authored Oct 6, 2022
2 parents 0850616 + 4a0106a commit f8ad55a
Show file tree
Hide file tree
Showing 17 changed files with 202 additions and 137 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ test/
-----------------------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
-----------------------------|----------|----------|----------|----------|----------------|
contracts\ | 100 | 94.52 | 100 | 100 | |
BasePool.sol | 100 | 94.52 | 100 | 100 | |
contracts\ | 100 | 94.44 | 100 | 100 | |
BasePool.sol | 100 | 94.44 | 100 | 100 | |
contracts\interfaces\ | 100 | 100 | 100 | 100 | |
IBasePool.sol | 100 | 100 | 100 | 100 | |
IPAXG.sol | 100 | 100 | 100 | 100 | |
Expand All @@ -84,15 +84,15 @@ File | % Stmts | % Branch | % Funcs | % Lines |Uncove
PoolWethDai.sol | 100 | 100 | 100 | 100 | |
contracts\pools\weth-usdc\ | 100 | 100 | 100 | 100 | |
PoolWethUsdc.sol | 100 | 100 | 100 | 100 | |
contracts\test\ | 56.86 | 0 | 72.73 | 56.86 | |
contracts\test\ | 58 | 0 | 80 | 58 | |
AddLiquidityAndBorrow.sol | 60 | 0 | 100 | 60 |... 59,60,62,63 |
Borrow.sol | 58.33 | 0 | 100 | 58.33 | 45,48,51,52,53 |
CTokenInterface.sol | 100 | 100 | 100 | 100 | |
ConstructorTest.sol | 0 | 100 | 25 | 0 | 40,46,52 |
ConstructorTest.sol | 0 | 100 | 33.33 | 0 | 42,48 |
IUSDC.sol | 100 | 100 | 100 | 100 | |
IWETH.sol | 100 | 100 | 100 | 100 | |
PeripheralTest.sol | 61.9 | 100 | 100 | 61.9 |... 3,96,99,100 |
-----------------------------|----------|----------|----------|----------|----------------|
All files | 94.12 | 90.79 | 94.92 | 94.55 | |
All files | 94.44 | 90.67 | 96.3 | 94.84 | |
-----------------------------|----------|----------|----------|----------|----------------|
```
126 changes: 78 additions & 48 deletions contracts/BasePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ abstract contract BasePool is IBasePool {
) revert InvalidRemove();
if (block.timestamp < lpInfo.earliestRemove)
revert BeforeEarliestRemove();
uint256 _totalLiquidity = getTotalLiquidity();
uint256 _totalLiquidity = totalLiquidity;
uint128 _totalLpShares = totalLpShares;
// update state of pool
uint256 liquidityRemoved = (numShares *
Expand Down Expand Up @@ -258,21 +258,29 @@ abstract contract BasePool is IBasePool {
// update pool state
totalLiquidity = _totalLiquidity - loanAmount;

uint256 _loanIdx = loanIdx;
uint128 _totalLpShares = totalLpShares;

// update loan info
loanIdxToBorrower[loanIdx] = _onBehalf;
loanIdxToBorrower[_loanIdx] = _onBehalf;
LoanInfo memory loanInfo;
loanInfo.repayment = repaymentAmount;
loanInfo.totalLpShares = totalLpShares;
loanInfo.totalLpShares = _totalLpShares;
loanInfo.expiry = expiry;
loanInfo.collateral = pledgeAmount;
loanIdxToLoanInfo[loanIdx] = loanInfo;
}
{
loanIdxToLoanInfo[_loanIdx] = loanInfo;

// update aggregations
updateAggregations(loanIdx, pledgeAmount, 0, totalLpShares, false);
updateAggregations(
_loanIdx,
pledgeAmount,
0,
_totalLpShares,
false
);

// update loan idx counter
loanIdx += 1;
loanIdx = _loanIdx + 1;
}
{
// transfer _sendAmount (not pledgeAmount) in collateral ccy
Expand All @@ -295,8 +303,8 @@ abstract contract BasePool is IBasePool {
pledgeAmount,
loanAmount,
repaymentAmount,
totalLpShares,
expiry,
_creatorFee,
_referralCode
);
}
Expand Down Expand Up @@ -324,26 +332,23 @@ abstract contract BasePool is IBasePool {
uint128 _repayment = loanInfo.repayment;

// transfer repayment amount
uint128 repaymentAmountAfterFees = _sendAmount -
getLoanCcyTransferFee(_sendAmount);
// set range in case of rounding exact repayment amount
// cannot be hit; set upper bound to prevent fat finger
if (
repaymentAmountAfterFees < _repayment ||
repaymentAmountAfterFees > (101 * _repayment) / 100
) revert InvalidSendAmount();
uint128 repaymentAmountAfterFees = checkAndGetSendAmountAfterFees(
_sendAmount,
_repayment
);
// if repaymentAmountAfterFees was larger then update loan info
// this ensures the extra repayment goes to the LPs
if (repaymentAmountAfterFees != _repayment) {
loanInfo.repayment = repaymentAmountAfterFees;
}
uint128 _collateral = loanInfo.collateral;
uint128 _totalLpShares = loanInfo.totalLpShares;
// update the aggregation mappings
updateAggregations(
_loanIdx,
_collateral,
repaymentAmountAfterFees,
loanInfo.totalLpShares,
_totalLpShares,
true
);

Expand All @@ -356,7 +361,7 @@ abstract contract BasePool is IBasePool {
// transfer directly to someone other than payer/sender)
IERC20Metadata(collCcyToken).safeTransfer(_recipient, _collateral);
// spawn event
emit Repay(_loanOwner, _loanIdx);
emit Repay(_loanOwner, _loanIdx, repaymentAmountAfterFees);
}

function rollOver(
Expand Down Expand Up @@ -391,25 +396,24 @@ abstract contract BasePool is IBasePool {
uint256 _creatorFee,
uint256 _totalLiquidity
) = _borrow(_collateral, _minLoanLimit, _maxRepayLimit, _deadline);

// overwrite _deadline memory variable in order to prevent stack too deep
// error when emitting repay event
_deadline = _loanIdx;
{
// check roll over cost
if (loanAmount >= loanInfo.repayment) revert InvalidRollOver();
uint256 rollOverCost = loanInfo.repayment - loanAmount;
// set range in case of rounding exact rollOverCost
// cannot be hit; set upper bound to prevent fat finger
if (
_sendAmount - getLoanCcyTransferFee(_sendAmount) <
rollOverCost ||
_sendAmount - getLoanCcyTransferFee(_sendAmount) >
(101 * rollOverCost) / 100
) revert InvalidSendAmount();
else {
loanInfo.repayment =
_sendAmount +
loanAmount -
getLoanCcyTransferFee(_sendAmount);
}
uint128 tmpRepayment = loanInfo.repayment;
if (loanAmount >= tmpRepayment) revert InvalidRollOver();
uint128 rollOverCost = tmpRepayment - loanAmount;
uint128 repaymentAmountAfterFees = checkAndGetSendAmountAfterFees(
_sendAmount,
rollOverCost
) + loanAmount;
loanInfo.repayment = repaymentAmountAfterFees;
// mark previous loan as repaid
loanInfo.repaid = true;
// overwrite _maxRepayLimit memory variable in order to prevent stack too deep
// error when emitting repay event
_maxRepayLimit = repaymentAmountAfterFees;
}
// update the aggregation mapping for repaid loan
updateAggregations(
Expand All @@ -419,25 +423,24 @@ abstract contract BasePool is IBasePool {
loanInfo.totalLpShares,
true
);
// mark previous loan as repaid
loanInfo.repaid = true;
{
uint256 currLoanIdx = loanIdx;
// set new loan info
loanIdxToBorrower[loanIdx] = loanOwner;
loanIdxToBorrower[currLoanIdx] = loanOwner;
LoanInfo memory loanInfoNew;
loanInfoNew.expiry = expiry;
loanInfoNew.totalLpShares = totalLpShares;
loanInfoNew.repayment = repaymentAmount;
loanInfoNew.collateral = pledgeAmount;
loanIdxToLoanInfo[loanIdx] = loanInfoNew;
loanIdxToLoanInfo[currLoanIdx] = loanInfoNew;
updateAggregations(
loanIdx,
currLoanIdx,
pledgeAmount,
0,
loanInfoNew.totalLpShares,
false
);
loanIdx += 1;
loanIdx = currLoanIdx + 1;
totalLiquidity = _totalLiquidity - loanAmount;
// transfer liquidity
IERC20Metadata(loanCcyToken).safeTransferFrom(
Expand All @@ -447,9 +450,19 @@ abstract contract BasePool is IBasePool {
);
// transfer creator fee to pool creator in collateral ccy
IERC20Metadata(collCcyToken).safeTransfer(poolCreator, _creatorFee);

// spawn event
emit Rollover(
loanOwner,
currLoanIdx,
pledgeAmount,
loanAmount,
repaymentAmount,
loanInfoNew.totalLpShares,
expiry
);
}
// spawn event
emit Roll(_loanIdx, loanIdx - 1);
emit Repay(loanOwner, _deadline, _maxRepayLimit);
}

function claim(
Expand Down Expand Up @@ -707,7 +720,7 @@ abstract contract BasePool is IBasePool {
// compute terms (as uint256)
_creatorFee = (_inAmountAfterFees * creatorFee) / BASE;
uint256 pledge = _inAmountAfterFees - _creatorFee;
_totalLiquidity = getTotalLiquidity();
_totalLiquidity = totalLiquidity;
if (_totalLiquidity <= minLiquidity) revert InsufficientLiquidity();
uint256 loan = (pledge *
maxLoanPerColl *
Expand Down Expand Up @@ -781,8 +794,6 @@ abstract contract BasePool is IBasePool {
collateral = (aggClaimsInfo.collateral * _shares) / BASE;
}

function getTotalLiquidity() internal view virtual returns (uint256);

/**
* @notice Function which updates the 3 aggegration levels when claiming
* @dev This function will subtract collateral and add to repay if _isRepay is true.
Expand Down Expand Up @@ -1000,7 +1011,7 @@ abstract contract BasePool is IBasePool {
uint32 earliestRemove
)
{
uint256 _totalLiquidity = getTotalLiquidity();
uint256 _totalLiquidity = totalLiquidity;
if (_inAmountAfterFees < minLiquidity / 1000) revert InvalidAddAmount();
// retrieve lpInfo of sender
LpInfo storage lpInfo = addrToLpInfo[_onBehalfOf];
Expand Down Expand Up @@ -1292,6 +1303,25 @@ abstract contract BasePool is IBasePool {
}
}

/**
* @notice Function which checks and returns loan ccy send amount after fees
* @param _sendAmount Amount of loanCcy to be transferred
* @param lowerBnd Minimum amount which is expected to be received at least
*/
function checkAndGetSendAmountAfterFees(
uint128 _sendAmount,
uint128 lowerBnd
) internal view returns (uint128 sendAmountAfterFees) {
sendAmountAfterFees = _sendAmount - getLoanCcyTransferFee(_sendAmount);
// check range in case of rounding exact lowerBnd amount
// cannot be hit; set upper bound to prevent fat finger
if (
sendAmountAfterFees < lowerBnd ||
sendAmountAfterFees > (101 * lowerBnd) / 100
) revert InvalidSendAmount();
return sendAmountAfterFees;
}

/**
* @notice Function which gets fees (if any) on the collCcy
* @param _transferAmount Amount of collCcy to be transferred
Expand Down
19 changes: 15 additions & 4 deletions contracts/interfaces/IBasePool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ interface IBasePool {
uint256 collateral,
uint256 loanAmount,
uint256 repaymentAmount,
uint256 totalLpShares,
uint256 indexed expiry,
uint256 fee,
uint256 indexed referralCode
);
event Roll(uint256 oldLoanIdx, uint256 newLoanIdx);

event Rollover(
address indexed borrower,
uint256 loanIdx,
uint256 collateral,
uint256 loanAmount,
uint256 repaymentAmount,
uint256 totalLpShares,
uint256 indexed expiry
);
event ClaimFromAggregated(
address indexed lp,
uint256 fromLoanIdx,
Expand All @@ -58,7 +65,11 @@ interface IBasePool {
uint256 repayments,
uint256 collateral
);
event Repay(address indexed borrower, uint256 loanIdx);
event Repay(
address indexed borrower,
uint256 loanIdx,
uint256 repaymentAmountAfterFees
);
event Reinvest(
address indexed lp,
uint256 repayments,
Expand Down
4 changes: 0 additions & 4 deletions contracts/pools/paxg-usdc/PoolPaxgUsdc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ contract PoolPaxgUsdc is BasePool {
)
{}

function getTotalLiquidity() internal view override returns (uint256) {
return totalLiquidity;
}

function getCollCcyTransferFee(uint128 _transferAmount)
internal
view
Expand Down
4 changes: 0 additions & 4 deletions contracts/pools/usdc-weth/PoolUsdcWeth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ contract PoolUsdcWeth is BasePool {
)
{}

function getTotalLiquidity() internal view override returns (uint256) {
return totalLiquidity;
}

function getCollCcyTransferFee(
uint128 /*_transferAmount*/
) internal pure override returns (uint128 transferFee) {
Expand Down
4 changes: 0 additions & 4 deletions contracts/pools/weth-cusdc/PoolWethCusdc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ contract PoolWethCusdc is BasePool {
)
{}

function getTotalLiquidity() internal view override returns (uint256) {
return totalLiquidity;
}

function getCollCcyTransferFee(
uint128 /*_transferAmount*/
) internal pure override returns (uint128 transferFee) {
Expand Down
4 changes: 0 additions & 4 deletions contracts/pools/weth-dai/PoolWethDai.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ contract PoolWethDai is BasePool {
)
{}

function getTotalLiquidity() internal view override returns (uint256) {
return totalLiquidity;
}

function getCollCcyTransferFee(
uint128 /*_transferAmount*/
) internal pure override returns (uint128 transferFee) {
Expand Down
4 changes: 0 additions & 4 deletions contracts/pools/weth-usdc/PoolWethUsdc.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ contract PoolWethUsdc is BasePool {
)
{}

function getTotalLiquidity() internal view override returns (uint256) {
return totalLiquidity;
}

function getCollCcyTransferFee(
uint128 /*_transferAmount*/
) internal pure override returns (uint128 transferFee) {
Expand Down
4 changes: 0 additions & 4 deletions contracts/test/ConstructorTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ contract ConstructorTest is BasePool {
)
{}

function getTotalLiquidity() internal view override returns (uint256) {
return totalLiquidity;
}

function getCollCcyTransferFee(
uint128 /*_transferAmount*/
) internal pure override returns (uint128 transferFee) {
Expand Down
Loading

0 comments on commit f8ad55a

Please sign in to comment.