Skip to main content

Cooler

Git Source

Inherits: Clone

A Cooler is a smart contract escrow that facilitates fixed-duration, peer-to-peer loans for a user-defined debt-collateral pair.

This contract uses Clones (https://github.com/wighawag/clones-with-immutable-args) to save gas on deployment.

State Variables

DECIMALS_INTEREST

uint256 private constant DECIMALS_INTEREST = 1e18;

requests

Arrays stores all the loan requests.

Request[] public requests;

loans

Arrays stores all the granted loans.

Loan[] public loans;

approvals

Facilitates transfer of lender ownership to new addresses

mapping(uint256 => address) public approvals;

Functions

owner

This address owns the collateral in escrow.

function owner() public pure returns (address _owner);

collateral

This token is borrowed against.

function collateral() public pure returns (ERC20 _collateral);

debt

This token is lent.

function debt() public pure returns (ERC20 _debt);

factory

This contract created the Cooler

function factory() public pure returns (CoolerFactory _factory);

requestLoan

Request a loan with given parameters. Collateral is taken at time of request.

function requestLoan(uint256 amount_, uint256 interest_, uint256 loanToCollateral_, uint256 duration_)
external
returns (uint256 reqID);

Parameters

NameTypeDescription
amount_uint256of debt tokens to borrow.
interest_uint256to pay (annualized % of 'amount_'). Expressed in DECIMALS_INTEREST.
loanToCollateral_uint256debt tokens per collateral token pledged. Expressed in 10**collateral().decimals().
duration_uint256of loan tenure in seconds.

Returns

NameTypeDescription
reqIDuint256of the created request. Equivalent to the index of request in requests[].

rescindRequest

Cancel a loan request and get the collateral back.

function rescindRequest(uint256 reqID_) external;

Parameters

NameTypeDescription
reqID_uint256index of request in requests[].

repayLoan

Repay a loan to get the collateral back.

Despite a malicious lender could reenter with the callback, the usage of msg.sender prevents any economical benefit to the attacker, since they would be repaying the loan themselves.

function repayLoan(uint256 loanID_, uint256 repayment_) external returns (uint256);

Parameters

NameTypeDescription
loanID_uint256index of loan in loans[].
repayment_uint256debt tokens to be repaid.

Returns

NameTypeDescription
<none>uint256collateral given back to the borrower.

delegateVoting

Delegate voting power on collateral.

function delegateVoting(address to_) external;

Parameters

NameTypeDescription
to_addressaddress to delegate.

clearRequest

Fill a requested loan as a lender.

function clearRequest(uint256 reqID_, address recipient_, bool isCallback_) external returns (uint256 loanID);

Parameters

NameTypeDescription
reqID_uint256index of request in requests[].
recipient_addressaddress to repay the loan to.
isCallback_booltrue if the lender implements the CoolerCallback abstract. False otherwise.

Returns

NameTypeDescription
loanIDuint256of the granted loan. Equivalent to the index of loan in loans[].

extendLoanTerms

Allow lender to extend a loan for the borrower. Doesn't require borrower permission because it doesn't have a negative impact for them.

Since this function solely impacts the expiration day, the lender should ensure that extension interest payments are done beforehand.

function extendLoanTerms(uint256 loanID_, uint8 times_) external;

Parameters

NameTypeDescription
loanID_uint256index of loan in loans[].
times_uint8that the fixed-term loan duration is extended.

claimDefaulted

Claim collateral upon loan default.

function claimDefaulted(uint256 loanID_) external returns (uint256, uint256, uint256, uint256);

Parameters

NameTypeDescription
loanID_uint256index of loan in loans[].

Returns

NameTypeDescription
<none>uint256defaulted debt by the borrower, collateral kept by the lender, elapsed time since expiry.
<none>uint256
<none>uint256
<none>uint256

approveTransfer

Approve transfer of loan ownership rights to a new address.

function approveTransfer(address to_, uint256 loanID_) external;

Parameters

NameTypeDescription
to_addressaddress to be approved.
loanID_uint256index of loan in loans[].

transferOwnership

Execute loan ownership transfer. Must be previously approved by the lender.

function transferOwnership(uint256 loanID_) external;

Parameters

NameTypeDescription
loanID_uint256index of loan in loans[].

setRepaymentAddress

Allow lender to set repayment recipient of a given loan.

function setRepaymentAddress(uint256 loanID_, address recipient_) external;

Parameters

NameTypeDescription
loanID_uint256of lender's loan.
recipient_addressreciever of repayments

collateralFor

Compute collateral needed for a desired loan amount at given loan to collateral ratio.

function collateralFor(uint256 principal_, uint256 loanToCollateral_) public view returns (uint256);

Parameters

NameTypeDescription
principal_uint256amount of debt tokens.
loanToCollateral_uint256ratio for loan. Expressed in 10**collateral().decimals().

interestFor

Compute interest cost on amount for duration at given annualized rate.

function interestFor(uint256 principal_, uint256 rate_, uint256 duration_) public pure returns (uint256);

Parameters

NameTypeDescription
principal_uint256amount of debt tokens.
rate_uint256of interest (annualized).
duration_uint256of the loan in seconds.

Returns

NameTypeDescription
<none>uint256Interest in debt token terms.

hasExpired

Check if given loan has expired.

function hasExpired(uint256 loanID_) external view returns (bool);

Parameters

NameTypeDescription
loanID_uint256index of loan in loans[].

Returns

NameTypeDescription
<none>boolExpiration status.

isActive

Check if a given request is active.

function isActive(uint256 reqID_) external view returns (bool);

Parameters

NameTypeDescription
reqID_uint256index of request in requests[].

Returns

NameTypeDescription
<none>boolActive status.

getRequest

Getter for Request data as a struct.

function getRequest(uint256 reqID_) external view returns (Request memory);

Parameters

NameTypeDescription
reqID_uint256index of request in requests[].

Returns

NameTypeDescription
<none>RequestRequest struct.

getLoan

Getter for Loan data as a struct.

function getLoan(uint256 loanID_) external view returns (Loan memory);

Parameters

NameTypeDescription
loanID_uint256index of loan in loans[].

Returns

NameTypeDescription
<none>LoanLoan struct.

Errors

OnlyApproved

error OnlyApproved();

Deactivated

error Deactivated();

Default

error Default();

NotExpired

error NotExpired();

NotCoolerCallback

error NotCoolerCallback();

Structs

Request

A loan begins with a borrow request.

struct Request {
uint256 amount;
uint256 interest;
uint256 loanToCollateral;
uint256 duration;
bool active;
address requester;
}

Loan

A request is converted to a loan when a lender clears it.

struct Loan {
Request request;
uint256 principal;
uint256 interestDue;
uint256 collateral;
uint256 expiry;
address lender;
address recipient;
bool callback;
}