Skip to content

Commit

Permalink
Merge pull request #1 from InverseFinance/dev
Browse files Browse the repository at this point in the history
FrontierV2 contracts first draft
  • Loading branch information
08xmt authored Dec 27, 2022
2 parents f356434 + 8434d27 commit cf6b0a4
Show file tree
Hide file tree
Showing 28 changed files with 4,446 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cache/
out/

lcov.info
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
# FrontierV2
/$$$$$$$$ /$$ /$$$$$$$ /$$ /$$
| $$_____/|__/| $$__ $$| $$$ /$$$
| $$ /$$| $$ \ $$| $$$$ /$$$$
| $$$$$ | $$| $$$$$$$/| $$ $$/$$ $$
| $$__/ | $$| $$__ $$| $$ $$$| $$
| $$ | $$| $$ \ $$| $$\ $ | $$
| $$ | $$| $$ | $$| $$ \/ | $$
|__/ |__/|__/ |__/|__/ |__/
# Fixed Rates Market
FiRM is an over collateralized money market protocol for borrowing DOLA at a fixed price, over an arbitrary period of time. This is accomplished with the *Dola Borrowing Rights* (**DBR**) token.
One DBR token gives the right to borrow one DOLA for one year. As time progresses, DBR will be burnt from the borrower's wallet at a rate that depends on their debt. A borrower may repay their loan at any time, and sell their DBR at a potential profit, if interest rates have gone up.

## Architecture
Simplified overview of the FiRM architecture:
<img src="SimplifiedArchitecture.png" height="600" >
### Market
The market contract is the central contract of the FiRM protocol and contains most logic pertaining to borrowing and liquidations. A DOLA Fed mints DOLA to a market, which is then available to borrow for users holding DBR, using the Borrow function.

A borrow controller contract is connected to the market, which may add additional logic to who and how much is allowed to be borrowed.

If a borrower's credit limit falls below the value of their outstanding debt, a percentage of their collateral may be liquidated on behalf of the protocol. The liquidation carries an additional fee, which will be paid out to the liquidator, and may benefit protocol governance as well.

### DBR
The DBR contract is a non-standard ERC-20 contract. As a user borrows DOLA, DBR are slowly burned from the user’s wallet to pay for the borrowing. Since the burn rate is deterministic depending on the user's debt, it's only necessary to update the accrued debt whenever the borrower increase or decrease their debt position.

If a user's DBR balance falls below 0, the burn will continue as the user accrues a deficit. The user can be forced to replenish their DBR balance through a *forced replenishment*. Force replenishments will mint fresh DBR tokens to a user, a sufficiently high price, that it's unnecessary to query an oracle about the market value of DBR tokens. To pay for the forced replenishment, additional DOLA debt is accrued to the borrower. Forced replenishments can be initiated by anyone and will immediately pay out a percentage of the debt accrued by the action to the caller.

### Escrows
Each wallet has a unique escrow contract, one for every deployed market. Their primary purpose is holding a user’s collateral, but may have additional functionality that allow borrowers to use their collateral for voting, yield farming etc.

### Fed
Feds are a class of contracts in the Inverse Finance ecosystem responsible for minting DOLA in a way that preserves the peg and can't be easily abused. In the FiRM protocol, the role of the Fed is to supply and remove DOLA to and from markets.

Binary file added SimplifiedArchitecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = 'src'
out = 'out'
libs = ['lib']

# See more config options https://github.com/foundry-rs/foundry/tree/master/config
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 27e14b
89 changes: 89 additions & 0 deletions src/BorrowController.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

/**
@title Borrow Controller
@notice Contract for limiting the contracts that are allowed to interact with markets
*/
contract BorrowController {

address public operator;
mapping(address => bool) public contractAllowlist;
mapping(address => uint) public dailyLimits;
mapping(address => mapping(uint => uint)) public dailyBorrows;

constructor(address _operator) {
operator = _operator;
}

modifier onlyOperator {
require(msg.sender == operator, "Only operator");
_;
}

/**
@notice Sets the operator of the borrow controller. Only callable by the operator.
@param _operator The address of the new operator.
*/
function setOperator(address _operator) public onlyOperator { operator = _operator; }

/**
@notice Allows a contract to use the associated market.
@param allowedContract The address of the allowed contract
*/
function allow(address allowedContract) public onlyOperator { contractAllowlist[allowedContract] = true; }

/**
@notice Denies a contract to use the associated market
@param deniedContract The addres of the denied contract
*/
function deny(address deniedContract) public onlyOperator { contractAllowlist[deniedContract] = false; }

/**
@notice Sets the daily borrow limit for a specific market
@param market The addres of the market contract
@param limit The daily borrow limit amount
*/
function setDailyLimit(address market, uint limit) public onlyOperator { dailyLimits[market] = limit; }

/**
@notice Checks if a borrow is allowed
@dev Currently the borrowController checks if contracts are part of an allow list and enforces a daily limit
@param msgSender The message sender trying to borrow
@param amount The amount to be borrowed
@return A boolean that is true if borrowing is allowed and false if not.
*/
function borrowAllowed(address msgSender, address, uint amount) public returns (bool) {
uint day = block.timestamp / 1 days;
uint dailyLimit = dailyLimits[msg.sender];
if(dailyLimit > 0) {
if(dailyBorrows[msg.sender][day] + amount > dailyLimit) {
return false;
} else {
//Safe to use unchecked, as function will revert in if statement if overflow
unchecked{
dailyBorrows[msg.sender][day] += amount;
}
}
}
if(msgSender == tx.origin) return true;
return contractAllowlist[msgSender];
}

/**
@notice Reduces the daily limit used, when a user repays debt
@dev This is necessary to prevent a DOS attack, where a user borrows the daily limit and immediately repays it again.
@param amount Amount repaid in the market
*/
function onRepay(uint amount) public {
uint day = block.timestamp / 1 days;
if(dailyBorrows[msg.sender][day] < amount) {
dailyBorrows[msg.sender][day] = 0;
} else {
//Safe to use unchecked, as dailyBorow is checked to be higher than amount
unchecked{
dailyBorrows[msg.sender][day] -= amount;
}
}
}
}
Loading

0 comments on commit cf6b0a4

Please sign in to comment.