Skip to content

Commit

Permalink
VAL-102 Return additional pool statistics to help with yield calculat…
Browse files Browse the repository at this point in the history
…ions (#125)
  • Loading branch information
ams9198 authored Dec 2, 2022
1 parent 7b0e227 commit 93ce540
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 13 deletions.
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) {
_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

0 comments on commit 93ce540

Please sign in to comment.