View on GitHub

Sapling contracts

Sapling Contracts

Solidity API

LoanDesk

Provides loan lifecycle.

config

struct ILoanDesk.LoanDeskConfig config

LoanDesk configuration parameters

loanTemplate

struct ILoanDesk.LoanTemplate loanTemplate

Default loan parameter values

nextApplicationId

uint256 nextApplicationId

Loan application id generator counter

loanApplications

mapping(uint256 => struct ILoanDesk.LoanApplication) loanApplications

Loan applications by applicationId

loanOffers

mapping(uint256 => struct ILoanDesk.LoanOffer) loanOffers

Loan offers by applicationId

recentApplicationIdOf

mapping(address => uint256) recentApplicationIdOf

Recent application id by address

nextLoanId

uint256 nextLoanId

Loan id generator counter

loans

mapping(uint256 => struct ILoanDesk.Loan) loans

Loans by loan ID

loanDetails

mapping(uint256 => struct ILoanDesk.LoanDetail) loanDetails

LoanDetails by loan ID

lentFunds

uint256 lentFunds

Accessor

Total funds lent at this time, accounts only for loan principals

weightedAvgAPR

uint32 weightedAvgAPR

Weighted average loan APR on the borrowed funds

applicationInStatus

modifier applicationInStatus(uint256 applicationId, enum ILoanDesk.LoanApplicationStatus status)

A modifier to limit access only to when the application exists and has the specified status

loanInStatus

modifier loanInStatus(uint256 loanId, enum ILoanDesk.LoanStatus status)

A modifier to limit access only to when the loan exists and has the specified status

updatedState

modifier updatedState()

Modifier to update pool accounting state before function execution

constructor

constructor() public

initialize

function initialize(address _pool, address _liquidityToken, address _accessControl, address _stakerAddress, bytes32 _lenderGovernanceRole) public

Initializer a new LoanDesk.

Addresses must not be 0.

Name Type Description
_pool address Lending pool address
_liquidityToken address ERC20 token contract address to be used as pool liquidity currency.
_accessControl address Access control contract
_stakerAddress address Staker address
_lenderGovernanceRole bytes32 Role held by the timelock control that executed passed lender votes

setMinLoanAmount

function setMinLoanAmount(uint256 minAmount) external

Set a minimum loan amount.

minAmount must be greater than or equal to safeMinAmount. Caller must be the staker.

Name Type Description
minAmount uint256 Minimum loan amount to be enforced on new loan requests and offers

setMinLoanDuration

function setMinLoanDuration(uint256 duration) external

Set the minimum loan duration

Duration must be in seconds and inclusively between SAFE_MIN_DURATION and maxDuration. Caller must be the staker.

Name Type Description
duration uint256 Minimum loan duration to be enforced on new loan requests and offers

setMaxLoanDuration

function setMaxLoanDuration(uint256 duration) external

Set the maximum loan duration.

Duration must be in seconds and inclusively between minDuration and SAFE_MAX_DURATION. Caller must be the staker.

Name Type Description
duration uint256 Maximum loan duration to be enforced on new loan requests and offers

setTemplateLoanGracePeriod

function setTemplateLoanGracePeriod(uint256 gracePeriod) external

Set the template loan payment grace period.

Grace period must be in seconds and inclusively between MIN_LOAN_GRACE_PERIOD and MAX_LOAN_GRACE_PERIOD. Caller must be the staker.

Name Type Description
gracePeriod uint256 Loan payment grace period for new loan offers

setTemplateLoanAPR

function setTemplateLoanAPR(uint32 apr) external

Set a template loan APR

APR must be inclusively between SAFE_MIN_APR and 100%. Caller must be the staker.

Name Type Description
apr uint32 Loan APR to be enforced on the new loan offers.

requestLoan

function requestLoan(uint256 _amount, uint256 _duration, string _profileId, string _profileDigest) external

Request a new loan.

Requested amount must be greater or equal to minLoanAmount(). Loan duration must be between minDuration() and maxDuration(). Multiple pending applications from the same address are not allowed. _profileId and _profileDigest are optional - provide nill values when not applicable.

Name Type Description
_amount uint256 Liquidity token amount to be borrowed
_duration uint256 Loan duration in seconds
_profileId string Borrower metadata profile id obtained from the borrower service
_profileDigest string Borrower metadata digest obtained from the borrower service

denyLoan

function denyLoan(uint256 appId) external

Deny a loan.

Loan must be in APPLIED status. Caller must be the staker.

draftOffer

function draftOffer(uint256 appId, uint256 _amount, uint256 _duration, uint256 _gracePeriod, uint256 _installmentAmount, uint16 _installments, uint32 _apr) external

Draft a loan offer for an application.

Loan application must be in APPLIED status. Caller must be the staker. Loan amount must not exceed available liquidity.

Name Type Description
appId uint256 Loan application id
_amount uint256 Loan amount in liquidity tokens
_duration uint256 Loan term in seconds
_gracePeriod uint256 Loan payment grace period in seconds
_installmentAmount uint256 Minimum payment amount on each instalment in liquidity tokens
_installments uint16 The number of payment installments
_apr uint32 Annual percentage rate of this loan

updateDraftOffer

function updateDraftOffer(uint256 appId, uint256 _amount, uint256 _duration, uint256 _gracePeriod, uint256 _installmentAmount, uint16 _installments, uint32 _apr) external

Update an existing draft loan offer.

Loan application must be in OFFER_DRAFTED status. Caller must be the staker. Loan amount must not exceed available liquidity.

Name Type Description
appId uint256 Loan application id
_amount uint256 Loan amount in liquidity tokens
_duration uint256 Loan term in seconds
_gracePeriod uint256 Loan payment grace period in seconds
_installmentAmount uint256 Minimum payment amount on each instalment in liquidity tokens
_installments uint16 The number of payment installments
_apr uint32 Annual percentage rate of this loan

lockDraftOffer

function lockDraftOffer(uint256 appId) external

Lock a draft loan offer.

Locking an offer makes it cancellable by a lender vote. Loan application must be in OFFER_DRAFTED status. Caller must be the staker.

Name Type Description
appId uint256 Loan application id

offerLoan

function offerLoan(uint256 appId) external

Make a loan offer.

Loan application must be in OFFER_DRAFT_LOCKED status. Caller must be the staker. Voting lock period must have expired.

Name Type Description
appId uint256 Loan application id

cancelLoan

function cancelLoan(uint256 appId) external

Cancel a loan.

Loan application must be in one of OFFER_MADE, OFFER_DRAFT_LOCKED, OFFER_MADE statuses. Caller must be the staker or the lender governance within the voting window.

borrow

function borrow(uint256 appId) external

Accept a loan offer and withdraw funds

Caller must be the borrower of the loan in question. The loan must be in OFFER_MADE status.

Name Type Description
appId uint256 ID of the loan application to accept the offer of

repay

function repay(uint256 loanId, uint256 amount) external

Make a payment towards a loan.

Caller must be the borrower. Loan must be in OUTSTANDING status. Only the necessary sum is charged if amount exceeds amount due. Amount charged will not exceed the amount parameter.

Name Type Description
loanId uint256 ID of the loan to make a payment towards.
amount uint256 Payment amount

repayOnBehalf

function repayOnBehalf(uint256 loanId, uint256 amount, address borrower) external

Make a payment towards a loan on behalf of a borrower.

Loan must be in OUTSTANDING status. Only the necessary sum is charged if amount exceeds amount due. Amount charged will not exceed the amount parameter.

Name Type Description
loanId uint256 ID of the loan to make a payment towards.
amount uint256 Payment amount
borrower address address of the borrower to make a payment on behalf of.

defaultLoan

function defaultLoan(uint256 loanId) external

Default a loan.

Loan must be in OUTSTANDING status. Caller must be the staker. canDefault(loanId) must be true.

Name Type Description
loanId uint256 ID of the loan to default

repayBase

function repayBase(uint256 loanId, uint256 amount) internal

Make a payment towards a loan.

Loan must be in OUTSTANDING status. Only the necessary sum is charged if amount exceeds amount due. Amount charged will not exceed the amount parameter.

Name Type Description
loanId uint256 ID of the loan to make a payment towards
amount uint256 Payment amount in tokens

updateAvgApr

function updateAvgApr(uint256 amountReducedBy, uint32 apr) internal

Internal method to update the weighted average loan apr based on the amount reduced by and an apr.

Name Type Description
amountReducedBy uint256 amount by which the funds committed into strategy were reduced, due to repayment or loss
apr uint32 annual percentage rate of the strategy

applicationsCount

function applicationsCount() external view returns (uint256)

Count of all loan requests in this pool.

Name Type Description
[0] uint256 LoanApplication count.

loansCount

function loansCount() external view returns (uint256)

Count of all loans in this pool.

Name Type Description
[0] uint256 Loan count.

loanBalanceDue

function loanBalanceDue(uint256 loanId) external view returns (uint256)

Loan balance due including interest if paid in full at this time.

Loan must be in OUTSTANDING status.

Name Type Description
loanId uint256 ID of the loan to check the balance of
Name Type Description
[0] uint256 Total amount due with interest on this loan

hasOpenApplication

function hasOpenApplication(address account) public view returns (bool)

canDefault

function canDefault(uint256 loanId) public view returns (bool)

View indicating whether or not a given loan qualifies to be defaulted

Name Type Description
loanId uint256 ID of the loan to check
Name Type Description
[0] bool True if the given loan can be defaulted, false otherwise

validateLoanParams

function validateLoanParams(uint256 _amount, uint256 _duration, uint256 _gracePeriod, uint256 _installmentAmount, uint16 _installments, uint32 _apr) private view

Validates loan offer parameters

Throws a require-type exception on invalid loan parameter

Name Type Description
_amount uint256 Loan amount in liquidity tokens
_duration uint256 Loan term in seconds
_gracePeriod uint256 Loan payment grace period in seconds
_installmentAmount uint256 Minimum payment amount on each instalment in liquidity tokens
_installments uint16 The number of payment installments
_apr uint32 Annual percentage rate of this loan

loanBalanceDueWithInterest

function loanBalanceDueWithInterest(uint256 loanId) private view returns (uint256, uint256, uint256)

Loan balances due if paid in full at this time.

Name Type Description
loanId uint256 ID of the loan to check the balance of
Name Type Description
[0] uint256 Principal outstanding, interest outstanding, and the number of interest acquired days
[1] uint256  
[2] uint256  

payableLoanBalance

function payableLoanBalance(uint256 loanId, uint256 maxPaymentAmount) private view returns (uint256, uint256, uint256)

Loan balances payable given a max payment amount.

Name Type Description
loanId uint256 ID of the loan to check the balance of
maxPaymentAmount uint256 Maximum liquidity token amount user has agreed to pay towards the loan
Name Type Description
[0] uint256 Total transfer amount, interest payable, and the number of payable interest days, and the current loan balance
[1] uint256  
[2] uint256  

countInterestDays

function countInterestDays(uint256 borrowedTime, uint256 interestPaidTillTime) private view returns (uint256)

Get the number of days in a time period to witch an interest can be applied.

Returns the floor of the unix day count, but not less than 1.

Name Type Description
borrowedTime uint256 Block timestamp of the loan borrowed time.
interestPaidTillTime uint256 Block timestamp up to which the interest is paid for.
Name Type Description
[0] uint256 Floor count of unix day in a time period to witch an interest can be applied.

canClose

function canClose() internal view returns (bool)

Indicates whether or not the contract can be closed in it’s current state.

Overrides a hook in SaplingStakerContext.

Name Type Description
[0] bool True if the contract is closed, false otherwise.

canOpen

function canOpen() internal view returns (bool)

Indicates whether or not the contract can be opened in it’s current state.

Overrides a hook in SaplingStakerContext.

Name Type Description
[0] bool True if the conditions to open are met, false otherwise.

percentDecimals

function percentDecimals() external pure returns (uint8)

External accessor for library level percent decimals.