Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VAL-102 Return additional pool statistics to help with yield calculations #125

Merged
merged 7 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions contracts/Pool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,12 @@ contract Pool is IPool, ERC20 {
/**
* @dev The pool accounting variables;
*/
function accountings() external view returns (IPoolAccountings memory) {
function accountings()
external
view
override
returns (IPoolAccountings memory)
{
return _accountings;
}

Expand Down Expand Up @@ -275,17 +280,30 @@ contract Pool is IPool, ERC20 {
require(_fundedLoans[msg.sender], "Pool: not funded loan");

ILoanLifeCycleState loanState = ILoan(msg.sender).state();
if (loanState == ILoanLifeCycleState.Matured) {
_activeLoans.remove(msg.sender);
if (
loanState == ILoanLifeCycleState.Matured ||
loanState == ILoanLifeCycleState.Defaulted
) {
require(_activeLoans.remove(msg.sender), "Pool: not active loan");
} else if (loanState == ILoanLifeCycleState.Active) {
_activeLoans.add(msg.sender);
} else if (loanState == ILoanLifeCycleState.Defaulted) {
Copy link
Contributor Author

@ams9198 ams9198 Dec 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved these accounting updates on a default to the more explicit onLoanDefaulted method that I added, which I like more. That way this method is only responsible for adding and removing loans to _activeLoans, which is nice.

_activeLoans.remove(msg.sender);
_accountings.outstandingLoanPrincipals -= ILoan(msg.sender)
.outstandingPrincipal();
}
}

/**
* @inheritdoc IPool
*/
function onLoanDefaulted(address loan, uint256 firstLossApplied)
external
override
onlyPoolController
{
uint256 outstandingPrincipal = ILoan(loan).outstandingPrincipal();
_accountings.outstandingLoanPrincipals -= outstandingPrincipal;
_accountings.totalDefaults += outstandingPrincipal;
_accountings.totalFirstLossApplied += firstLossApplied;
}

/**
* @inheritdoc IPool
*/
Expand Down Expand Up @@ -313,6 +331,18 @@ contract Pool is IPool, ERC20 {
);
}

/**
* @inheritdoc IPool
*/
function currentExpectedInterest()
external
view
override
returns (uint256)
{
return PoolLib.calculateExpectedInterest(_activeLoans);
}

/**
* @inheritdoc IPool
*/
Expand Down Expand Up @@ -668,7 +698,8 @@ contract Pool is IPool, ERC20 {
assets,
previewDeposit(assets),
maxDeposit(receiver),
_mint
_mint,
_accountings
);
}

Expand Down Expand Up @@ -727,7 +758,8 @@ contract Pool is IPool, ERC20 {
assets,
previewDeposit(assets),
maxDeposit(receiver),
_mint
_mint,
_accountings
);
}

Expand Down Expand Up @@ -853,6 +885,9 @@ contract Pool is IPool, ERC20 {
// Burn the shares
_burn(owner, shares);

// Updating accountings
_accountings.totalAssetsWithdrawn += assets;

emit Withdraw(owner, owner, owner, assets, shares);
}

Expand Down
15 changes: 14 additions & 1 deletion contracts/interfaces/IPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet
* @title Data type storing collected accounting statistics
*/
struct IPoolAccountings {
uint256 defaultsTotal;
uint256 outstandingLoanPrincipals;
uint256 fixedFeeDueDate;
uint256 totalAssetsDeposited;
uint256 totalAssetsWithdrawn;
uint256 totalDefaults;
uint256 totalFirstLossApplied;
}

/**
Expand Down Expand Up @@ -172,6 +175,11 @@ interface IPool is IERC4626 {
*/
function onLoanStateTransitioned() external;

/**
* @dev Called by the PoolController, notifies the Pool that a loan has been defaulted.
*/
function onLoanDefaulted(address loan, uint256 firstLossApplied) external;

/**
* @dev Called by the Pool Controller, it transfers the fixed fee
*/
Expand All @@ -191,4 +199,9 @@ interface IPool is IERC4626 {
* @dev The total available supply that is not marked for withdrawal
*/
function totalAvailableSupply() external view returns (uint256);

/**
* @dev The accrued interest at the current block.
*/
function currentExpectedInterest() external view returns (uint256 interest);
}
5 changes: 4 additions & 1 deletion contracts/libraries/PoolLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ library PoolLib {
uint256 assets,
uint256 shares,
uint256 maxDeposit,
function(address, uint256) mint
function(address, uint256) mint,
IPoolAccountings storage accountings
) internal returns (uint256) {
require(shares > 0, "Pool: 0 deposit not allowed");
require(assets <= maxDeposit, "Pool: Exceeds max deposit");
Expand All @@ -332,6 +333,7 @@ library PoolLib {
mint(sharesReceiver, shares);

emit Deposit(msg.sender, sharesReceiver, assets, shares);
accountings.totalAssetsDeposited += assets;
return shares;
}

Expand Down Expand Up @@ -360,6 +362,7 @@ library PoolLib {
: firstLossBalance;

FirstLossVault(firstLossVault).withdraw(firstLossRequired, pool);
IPool(pool).onLoanDefaulted(loan, firstLossRequired);

emit LoanDefaulted(loan);
emit FirstLossApplied(loan, firstLossRequired);
Expand Down
4 changes: 3 additions & 1 deletion contracts/mocks/PoolLibTestWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ contract PoolLibTestWrapper is ERC20("PoolLibTest", "PLT") {
using EnumerableSet for EnumerableSet.AddressSet;

EnumerableSet.AddressSet private _activeLoans;
IPoolAccountings private _accountings;

event LifeCycleStateTransition(IPoolLifeCycleState state);
event FirstLossDeposited(
Expand Down Expand Up @@ -157,7 +158,8 @@ contract PoolLibTestWrapper is ERC20("PoolLibTest", "PLT") {
assets,
shares,
maxDeposit,
_mint
_mint,
_accountings
);
}

Expand Down
Loading