-
Notifications
You must be signed in to change notification settings - Fork 1
/
CryptoBureau.sol
189 lines (148 loc) · 5.96 KB
/
CryptoBureau.sol
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import { Bureau } from "./base/Bureau.sol";
import { IWorldID } from "./interfaces/IWorldID.sol";
import { ByteHasher } from "./utils/ByteHasher.sol";
// import "hardhat/console.sol";
contract CryptoBureau is Bureau {
using ByteHasher for bytes;
struct HelperConfig {
uint128 multiplier;
}
event Registered(uint256 indexed nullifierHash, address indexed account);
/// @dev nullifierHash (world ID) => data
mapping (uint256 => ScoreData) private _scoreData;
/// @dev nullifierHash => helper
mapping (uint256 => mapping (address => bool)) private _usedHelpers;
uint256 private _maxBaseScore = 10 ether;
/// @dev address => nullifierHash
mapping (address => uint256) private _accounts;
/// @dev lenders => true
mapping (address => bool) private _lenders;
/// @dev helpers => config
mapping (address => HelperConfig) private _helpers;
/// @dev the following are world-id related variables
IWorldID private immutable _worldId;
uint256 private immutable _externalNullifier;
modifier onlyHelper() {
HelperConfig storage config = _helpers[msg.sender];
require(config.multiplier > 0, "Caller is not a helper");
_;
}
modifier onlyLender() {
require(_lenders[msg.sender], "Caller is not a lender");
_;
}
constructor(
IWorldID worldId_,
string memory _appId,
string memory _actionId
) Bureau("CryptoBuerau") {
_worldId = worldId_;
_externalNullifier = abi
.encodePacked(abi.encodePacked(_appId).hashToField(), _actionId)
.hashToField();
}
function _scoreDataForAccount(address account) internal view returns (ScoreData storage) {
uint256 nullifier = _accounts[account];
require(nullifier > 0, "CryptoBureau: Account not found");
ScoreData storage data = _scoreData[nullifier];
require(data.verified > 0, "CryptoBureau: Not verified");
return data;
}
function register(
uint256 root,
uint256 nullifierHash,
uint256[8] calldata proof
) external {
require(_scoreData[nullifierHash].verified == 0, "CryptoBureau: Already registered");
// verify the proof
// Confirmed with World ID dev team that verify doesn't work with
// staging simulator at the moment. Consider the proof verified.
// TODO: uncomment for mainnet deployment
// _worldId.verifyProof(
// root,
// 1,
// abi.encodePacked(_msgSender()).hashToField(),
// nullifierHash,
// _externalNullifier,
// proof
// );
// TODO: uncomment for mainnet deployment
_scoreData[nullifierHash] = ScoreData({
verified: uint64(block.timestamp),
base: 2 ether,
totalBorrowed: 0,
totalRepaid: 0,
totalCollateral: 0
});
_accounts[_msgSender()] = nullifierHash;
}
function isHelperUsed(address account, address helper) external view returns (bool) {
return _usedHelpers[_accounts[account]][helper];
}
function scoreData(address account) external view returns (ScoreData memory) {
return _scoreDataForAccount(account);
}
function score(address account) external view override returns (Score memory) {
ScoreData storage data = _scoreDataForAccount(account);
uint256 maxK = 1.5 ether;
uint256 minK = 0.5 ether;
uint256 base;
if (data.totalBorrowed == 0) {
// first-timer, will pass on the base calculated k
base = data.base;
} else if (data.totalBorrowed > data.totalRepaid) {
base = data.base / 2 ether; // NOTE: very basic
} else {
// console.log("repaid", data.totalRepaid);
uint256 overage = data.totalRepaid * 1 ether / data.totalBorrowed;
// console.log("overage", data.totalRepaid);
base = data.base * overage * 1.1 ether / 1 ether / 1 ether;
}
if (base > _maxBaseScore) {
base = _maxBaseScore;
}
// console.log(base);
// 0.5 + 1 * (1 - 2/10) = 1.3
uint256 k = minK + (maxK - minK) * (1 ether - base * 1 ether / _maxBaseScore) / 1 ether;
return Score({
collateralCoef: k
});
}
// Hooks
function onBorrow(address account, uint256 value) external override onlyLender {
ScoreData storage data = _scoreDataForAccount(account);
data.totalBorrowed += value;
}
function onRepay(address account, uint256 value) external override onlyLender {
ScoreData storage data = _scoreDataForAccount(account);
data.totalRepaid += value;
}
function onIncreaseCollateral(address account, uint256 value) external override onlyLender {
ScoreData storage data = _scoreDataForAccount(account);
data.totalCollateral += value;
}
function onDecreaseCollateral(address account, uint256 value) external override onlyLender {
ScoreData storage data = _scoreDataForAccount(account);
data.totalCollateral -= value;
}
// Helper functions
function verify(address account) external override onlyHelper {
HelperConfig storage config = _helpers[msg.sender];
ScoreData storage data = _scoreDataForAccount(account);
require(!_usedHelpers[_accounts[account]][msg.sender], "CryptoBureau: Already verified");
_usedHelpers[_accounts[account]][msg.sender] = true;
data.base = data.base * config.multiplier / 1 ether;
if (data.base > _maxBaseScore) {
_maxBaseScore = data.base;
}
}
// Management functions
function setLender(address lender, bool status) external onlyAdmin {
_lenders[lender] = status;
}
function setHelper(address helper, HelperConfig calldata config) external onlyAdmin {
_helpers[helper] = config;
}
}