File size: 3,523 Bytes
f998fcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.

// 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;
            }
        }
    }
}